douduiyun6125 2016-06-15 22:53
浏览 32
已采纳

集成嵌入式CSV文件 - PHP

I am trying to integrate a embedded CSV file via PHP using a form search.

My CSV looks like this:

ACCTNO        PRCLID    NOTICES
00109033-01   234962    2056, 3005, 1033
00104120-04   200921    2006, 3002, 3004, 2056, 1033
00170172-01   299573    2056, 3001, 3003, 3004, 1033
00106284-02   208709    1026, 2056, 3004, 1033
00150372-01   222376    2056, 3001, 2006, 1033

Basically I am trying to have a search box where the user enters 00109033-01, and a the three divs that correspond to 2056, 3005 and 1033 (from the 'NOTICES' column) are presented. If the user searched for 00104120-04 the five divs that correspond to 2006, 3002, 3004, 2056, 1033 (from the 'NOTICES' column) are presented.

I will have corresponding divs for each of the 'NOTICE' codes, some relate to an account, others don't.

I know how to embed CSV data but I am struggling to workout the interrogation part and presenting the corresponding divs.

Any hints, code help or even just the right terminology for what I am trying to achieve would be a great help.

  • 写回答

1条回答 默认 最新

  • douyan1613 2016-06-15 23:20
    关注

    Here's one way of doing it using fgetcsv.

    <?php
    // will hold account number to search for
    $acctno = isset($_GET['acctno']) ? $_GET['acctno'] : null; 
    // will hold the notices that are found
    $notices = null; 
    // check if account number was given
    if ($acctno) {
        // open the CSV file for reading
        if ($file = fopen('file.csv', 'r')) {
            // loop through rows of CSV file searching for account number
            while (($data = fgetcsv($file)) !== false) {
                // if there is a match, save its corresponding notices and stop looping
                if ($data[0] === $acctno) {
                    $notices = $data[2];
                    break;
                }
            }
            // close the CSV file
            fclose($file);
        }
    }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
        <form action="" method="get">
            <label>Search</label>
            <input type="text" name="acctno" value="<?= $acctno ?>">
        </form>
        <p><?= $notices ?></p>
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程