I have 2 files, buyerInterest.php
and carDetails.php
. When someone states their interest in a car, they will have to type in the plate number. I have then made it such that the plate number will be written to a txt file. I then will be able to display the total number of people interested in the car at buyerInterest.php
, by counting the number of times a certain plate has been inserted. However, the same car is being displayed twice, with the count incrementing.
buyerInterest.php
<?php
if(isset($_POST['submit']))
{
$fname =$_POST['fname'];
$lname=$_POST['lname'];
$phone=$_POST['phone'];
$platenum=$_POST['platenum'];
$price=$_POST['price'];
$file = fopen("BuyerInterest.txt","a+");
$countfile = fopen("counter.txt","a+");
fwrite($file,$fname . ',');
fwrite($file,$lname . ',');
fwrite($file,$phone . ',');
fwrite($file,$platenum . ',');
fwrite($file,$price . PHP_EOL);
fwrite($countfile,$platenum . PHP_EOL);
print_r(error_get_last());
fclose($file);
fclose($countfile);
}
?>
carDetails.php
<table border="2">
<th>First Name</th>
<th>Last Name</th>
<th>Phone</th>
<th>Email</th>
<th>Plate Number</th>
<th>Model</th>
<th>Year of Manufacture</th>
<th>Description</th>
<th>No. of Kilometers Travelled</th>
<th>No. of Previous Owners</th>
<th>Characteristics of Recent Repairs</th>
<th>Number of people interested</th>
<?php
if(isset($_POST['submit']))
{
$search = $_POST['search'];
$lines = file('CarDirectory.txt');
$interest = file('counter.txt');
// Store true when the text is found
$found = false;
$counted = array_count_values(file('counter.txt'));
foreach($counted as $platenum => $count)
//echo "{$platenum} : {$count}";
foreach($lines as $line)
{
if(strpos($line, $search) !== false)
{
$found = true;
list($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k) = explode(',', $line);
print "<tr>
<td width=40>$a</td>
<td width=40>$b</td>
<td width=40>$c</td>
<td width=40>$d</td>
<td width=40>$e</td>
<td width=40>$f</td>
<td width=40>$g</td>
<td width=40>$h</td>
<td width=40>$i</td>
<td width=40>$j</td>
<td width=40>$k</td>
<td width=40>$count</td>
</tr>";
}
}
// If the text was not found, show a message
if(!$found)
{
echo 'No match found';
}
}
?>
counter.txt
SFR6543G
SFR1234H
SFR1234H
There's no need to put foreach ($lines as $line)
inside foreach ($counted as $platenum => $count)
. Because of the nested loops, you're printing a line for each matching car for every plate in $counted
, and using counts for those unrelated cars. You don't need the outer loop at all.
Since $counted
is an associative array, you can simply look up $counted[$e]
.
You also need to remove the newlines when you create $counted
.
$found = false;
$counted = array_count_values(file('counter.txt', FILE_IGNORE_NEW_LINES));
foreach($lines as $line)
{
if(strpos($line, $search) !== false)
{
$found = true;
list($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k) = explode(',', $line);
$count = $counted[$e]
print "<tr>
<td width=40>$a</td>
<td width=40>$b</td>
<td width=40>$c</td>
<td width=40>$d</td>
<td width=40>$e</td>
<td width=40>$f</td>
<td width=40>$g</td>
<td width=40>$h</td>
<td width=40>$i</td>
<td width=40>$j</td>
<td width=40>$k</td>
<td width=40>$count</td>
</tr>";
}
}
BTW, I suggest you use better variables than $a
, $b
, etc. You can also use fputcsv()
and fgetcsv()
to write and read CSV files, instead of writing commas by hand.
The mistake in the program is that you display data as you read it. Because of this, it is difficult to verify if you already displayed the info for a plate number or not.
I would first read all the data in a table (in memory), make sure that the lines are unique (the same plate number does not appear twice), maintain any counters in the table according to the info read from the file. At the end, when everything is fine, just print the table.
Now, if you have a really big file, you may as well use a database - even a SQLite is better than a text file. - as @Barmar already suggested.