由于计数器PHP,结果显示两次
我有两个文件,由于计数器PHP,结果显示两次,php,Php,我有两个文件,buyerInterest.php和cardetals.php。当有人表示对汽车感兴趣时,他们必须输入车牌号。然后,我将车牌号写入txt文件。然后,通过计算插入某个车牌的次数,我将能够在buyerInterest.php上显示对该车感兴趣的总人数。但是,同一辆车显示了两次,计数递增 buyerInterest.php <?php if(isset($_POST['submit'])) { $fname =$_POST['fname']; $lname=$_P
buyerInterest.php
和cardetals.php
。当有人表示对汽车感兴趣时,他们必须输入车牌号。然后,我将车牌号写入txt文件。然后,通过计算插入某个车牌的次数,我将能够在buyerInterest.php
上显示对该车感兴趣的总人数。但是,同一辆车显示了两次,计数递增
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);
}
?>
<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';
}
}
?>
无需将
foreach($line作为$line)
放在foreach($platenum=>count作为$count计算)
内。由于嵌套循环,您正在为$counted
中的每个车牌的每个匹配车辆打印一行,并对那些不相关的车辆使用计数。你根本不需要外环
由于$counted
是一个关联数组,因此只需查找$counted[$e]
创建$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>";
}
}
$found=false;
$counted=数组\计数\值(文件('counter.txt',文件\忽略\新行));
foreach($line作为$line)
{
if(strpos($line,$search)!==false)
{
$found=true;
列表($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k)=爆炸(“,”,$line);
$count=$counted[$e]
“打印”
一美元
b美元
$c
美元
$e
$f
$g
$h
$i
$j
$k
美元计数
";
}
}
顺便说一句,我建议您使用比
$a
、$b
等更好的变量。您也可以使用fputcsv()
和fgetcsv()
来写入和读取CSV文件,而不是手动写入逗号。程序中的错误是在读取数据时显示数据。因此,很难验证是否已显示车牌号信息
我首先读取表中的所有数据(在内存中),确保行是唯一的(相同的车牌号不会出现两次),根据从文件中读取的信息维护表中的任何计数器。最后,当一切正常时,只需打印表格
现在,如果你有一个非常大的文件,你也可以使用一个数据库——即使是SQLite也比文本文件好正如@Barmar已经建议的那样。为什么不使用数据库?您永远不会检查
$platenum
是否与搜索字符串匹配。所以你正在打印完全不相关的汽车数量。@Barmar不幸的是,我不允许使用数据库进行此操作。我尝试了你的代码。我搜索车牌号“SFR1234H”,得到的错误是第86行的未定义索引:SFR1234H
。第86行是$count=$counted[$e]
。创建$counted
时需要删除换行符。使用FILE()
时,请使用FILE\u IGNORE\u NEW\u行
标志。
$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>";
}
}