doujiao7325 2019-01-31 04:42
浏览 65
已采纳

由于计数器PHP,结果显示两次

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

table

  • 写回答

2条回答 默认 最新

  • doujiao6507 2019-01-31 05:15
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度