搜索输入在搜索时未显示来自csv的完全匹配

时间:2016-11-21 21:00:28

标签: php csv

继上一个问题后,我有了我的表格,但是当我试图输入一个关键字时,只显示整个表格。有没有办法让它只显示匹配。例如,如果我搜索住房利兹。它会在表格中显示整行吗?

住房利兹,Yorkshire LL,3,2013,221,235

的index.php

      <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <form action="search.php" method="get">
        <label>
            Search
            <input type="text" name="search" autocomplete="off">
        </label>

            <input type="submit" name="Search">
    </form>
</body>
</html>

的search.php

来自其他用户的帮助:

的search.php

 <?php

$search = isset($_GET['search']) ? (int) trim($_GET['search']) : null;
$search = isset($_GET['search']) ? trim($_GET['search']) : null;
$search = isset($_GET['search']) ? (int) trim($_GET['search']) : null;
$search = isset($_GET['search']) ? trim($_GET['search']) : null;
$search = isset($_GET['search']) ? trim($_GET['search']) : null;


define('CSV_INDEX_LANDLORD', 0);
define('CSV_INDEX_LANDLORD_GROUP', 1);
define('CSV_INDEX_QUARTER', 2);
define('CSV_INDEX_YEAR', 3);
define('CSV_INDEX_ESTIMATED_PROJECT_COST', 4);
define('CSV_INDEX_ACTUAL_PROJECT_COST', 5);



$row = 1;
$output = [];

if (($handle = fopen("data.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        $row++;

        // if the user tried to search on year and it doesn't match, continue skips to the next row
        // casting ensures we compare integers with integers
        if (!empty($search) && stripos($data[CSV_INDEX_LANDLORD], $search) !== false) {
            continue;
        }

        if (!empty($search) && stripos($data[CSV_INDEX_LANDLORD_GROUP], $search) !== false) {
            continue;
        }

        if (!empty($search) && stripos($data[CSV_INDEX_QUARTER], $search) !== false) {
            continue;
        }

        if (!empty($search) && stripos($data[CSV_INDEX_ESTIMATED_PROJECT_COST], $search) !== false) {
            continue;
        }

        if (!empty($search) && stripos($data[CSV_INDEX_ACTUAL_PROJECT_COST], $search) !== false) {
            continue;
        }

        $output[] = $data;
    }
    fclose($handle);
}
?>

<?php if (!empty($output)): ?>
    <table>
        <tr>
            <th><strong>Landlord</strong></th>
            <th><strong>Landlord Group</strong></th>
            <th><strong>Quarter</strong></th>
            <th><strong>Year</strong></th>
            <th><strong>Estimated Project Costs</strong></th>
            <th><strong>Actual Project Cost</strong>
        <tr>
        <?php foreach ($output as $row): ?>
        <tr>
            <td><?=$row[0]?></td>
            <td><?=$row[1]?></td>
            <td><?=$row[2]?></td>
            <td><?=$row[3]?></td>
            <td><?=$row[4]?></td>
            <td><?=$row[5]?></td>
        </tr>
        <?php endforeach; ?>
    </table>
<?php endif; ?>

我正在努力让它工作,所以如果我输入一个房东或房东组,相关的数据显示在表中。以下是一系列CSV数据。标题是大胆的。

 **Landlord,Landlord group,Quarter,Year,Estimated project costs (000s),Actual project cost (000s)**
Housing Leeds,Yorkshire LL,3,2013,221,235

1 个答案:

答案 0 :(得分:0)

这里有几个选项。

为您想要使用的每个搜索项目输入内容:

<input name="Landlord">
<input name="LandlordGroup">
<input name="Quarter">
...

然后你可以保持php原样 这当然对用户来说不是很整洁。

另一个选项是添加一个select项,让用户可以选择要搜索的内容:

<input name="search">
<select name="searchItem">
    <option value="LANDLORD">Landlord
    <option value="LANDLORD_GROUP">Landlord Group
    <option value="QUARTER">Quarter
    //.. more items here to add
</select>

然后你会在php脚本中做类似的事情:

<?php
$search = $_GET['search'];
$searchItem = $_GET['searchItem'];

// another way here to define the insexes as ass.Array, makes it easier later on.
// NOTE that the keys have to match the values of the select-options
$csvIndexes = array("LANDLORD"=>0,
                    "LANDLORD_GROUP"=>1,
                     "QUARTER"=>2,
                     //...
                   );
// $row = 1; // not needed here
$output = [];

if ($handle = fopen("data.csv", "r")) {
    while ($data = fgetcsv($handle, 1000, ",")) {
        // $num = count($data);
        // $row++;
        if (!empty($search) && stripos($csvIndexes[$searchItem], $search)) {
            $output[] = $data;
        }
    }
    fclose($handle);
}
// leave the rest as is.
?>

我还没有测试过这个,因为我没有你的csv,但我希望你明白这个想法!

第三种选择是每次默认搜索每个项目。