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