dongsetan3216 2016-11-21 21:00
浏览 62

搜索时,搜索输入未显示与csv的完全匹配

Following on from a previous question i have my form which is but at the moment whenever i have tried to enter a keyword the whole table is just displayed. Is there a way to get it showing only matches. For example if i searched Housing leeds. it would show the entire row in a table?

Housing Leeds,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

Help from another user:

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; ?>

I am struggling to get it to work so that if I enter either a Landlord or Landlord group data relating to it is shown in the table. Below is a line of CSV data. The bold are the headings.

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

1条回答 默认 最新

  • dpxkkhu1812 2016-11-22 01:00
    关注

    You have several options here.

    Make an input for every search-item you wanna use:

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

    then you can keep the php as is.
    This of course is not very neat for the user.

    Another option is to add a select item, which lets the user selct what to search for:

    <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>
    

    then you would do something like that in your php script:

    <?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.
    ?>
    

    I haven't tested this, since I don't have your csv, but I hope you get the idea!

    A third option would be to search in every item per default.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分