简体   繁体   中英

Integrate embedded CSV file - 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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM