在客户端使用PHP或JavaScript从大量数据中通过大量关键字一一搜索

在客户端使用PHP或JavaScript从大量数据中通过大量关键字一一搜索


表 1 - 包含以十万为单位的数据(列:id、title、description)


表 2 - 包含 25k 个关键字。(列:id,关键字)


现在我想在 Table1 中逐个关键字搜索数据,但我只想从 Table1 中的 2-3k 数据中搜索(仅在标题、描述列中)。


所以我计划,首先在一些对象(如数据表、数组等)中获取那些 2-3k 数据,然后在对象中逐个搜索关键字,并且当任何关键字在 Table1 中匹配时(仅在标题、描述列中)。MATCH[] 数组中这些数据的对象存储 ID 与 NOTMATCH[] 数组中存储的数据 ID 不匹配。


示例:Table1(100 000 行)


id  title                           description

1   dell laptop                     laptop i3 5000 xyz

2   hp machine                      hp xyz abc utr

3   supply motor 1500               Decorative Watches

4   Deep Groove Drill Ball Bearing  Deep Hole Drill

示例:Table2(26 000 行)


id             keyword

1              dell

2              Drill

这是我的代码。我希望对其进行一些改进以实现快速..或其他逻辑。当任何关键字输入搜索输入并显示结果时,就像引导数据表客户端搜索过程一样工作。


<html>

    <body>

        <?php

        //this data from table1

        $data = array(

            "0"=>array(

                "ID" => "1234",

                "title" => "dell laptop",

                "description" => "dell laptop i3 5000 xyz",

                ),

            "1"=>array(

                "ID" => "1238",

                "title" => "hp machine",

                "description" => "hp xyz abc utr",

            ),

            "2"=>array(

                "ID" => "1240",

                "title" => "supply motor 1500",

                "description" => "Decorative Watches",

            ),

            "3"=>array(

                "ID" => "1245",

                "title" => "Deep Groove Drill Ball Bearing",

                "description" => "Deep Hole Drill",

            ),

        );


        $MATCH =array();

        $NOTMATCH =array();


        $keywords =array('dell','watches'); //this data from table2

        echo "<pre>";


    </body>

</html>

那么我怎样才能按照我的计划编写代码。


侃侃尔雅
浏览 209回答 2
2回答

慕斯王

根据您的示例(obv 您将适应您的代码):function myFunction() {&nbsp; // Declare variables&nbsp; var input, filter, table, tr, td, i, txtValue;&nbsp; input = document.getElementById("myInput");&nbsp; filter = input.value.toUpperCase();&nbsp; table = document.getElementById("myTable");&nbsp; tr = table.getElementsByTagName("tr");&nbsp; // Loop through all table rows, and hide those who don't match the search query&nbsp; for (i = 0; i < tr.length; i++) {&nbsp; &nbsp; td = tr[i].getElementsByTagName("td")[0];&nbsp; &nbsp; if (td) {&nbsp; &nbsp; &nbsp; txtValue = td.textContent || td.innerText;&nbsp; &nbsp; &nbsp; if (txtValue.toUpperCase().indexOf(filter) > -1) {&nbsp; &nbsp; &nbsp; &nbsp; tr[i].style.display = "";&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; tr[i].style.display = "none";&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }}#myInput {&nbsp; background-image: url('/css/searchicon.png'); /* Add a search icon to input */&nbsp; background-position: 10px 12px; /* Position the search icon */&nbsp; background-repeat: no-repeat; /* Do not repeat the icon image */&nbsp; width: 100%; /* Full-width */&nbsp; font-size: 16px; /* Increase font-size */&nbsp; padding: 12px 20px 12px 40px; /* Add some padding */&nbsp; border: 1px solid #ddd; /* Add a grey border */&nbsp; margin-bottom: 12px; /* Add some space below the input */}#myTable {&nbsp; border-collapse: collapse; /* Collapse borders */&nbsp; width: 100%; /* Full-width */&nbsp; border: 1px solid #ddd; /* Add a grey border */&nbsp; font-size: 18px; /* Increase font-size */}#myTable th, #myTable td {&nbsp; text-align: left; /* Left-align text */&nbsp; padding: 12px; /* Add padding */}#myTable tr {&nbsp; /* Add a bottom border to all table rows */&nbsp; border-bottom: 1px solid #ddd;}#myTable tr.header, #myTable tr:hover {&nbsp; /* Add a grey background color to the table header and on hover */&nbsp; background-color: #f1f1f1;}<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.."><table id="myTable">&nbsp; <tr class="header">&nbsp; &nbsp; <th style="width:60%;">ROW 1</th>&nbsp; &nbsp; <th style="width:40%;">ROW 2</th>&nbsp; </tr>&nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>RESULT</td>&nbsp; &nbsp; <td>RESULT 2</td>&nbsp; </tr>&nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>RESULT 3</td>&nbsp; &nbsp; <td>RESULT 4</td>&nbsp; </tr>&nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>RESULT 5</td>&nbsp; &nbsp; <td>RESULT 6</td>&nbsp; </tr>&nbsp; <tr>&nbsp; &nbsp; <td>RESULT 7</td>&nbsp; &nbsp; <td>RESULT 8</td>&nbsp; </tr></table>提示:如果要执行区分大小写的搜索,请删除 toUpperCase()。提示:如果要搜索“国家”(索引 1)而不是“名称”(索引 0),请将 tr[i].getElementsByTagName('td')[0] 更改为 [1]。

桃花长相依

这是我想要的最好的代码,它将使用 PHP Array 在大约 2000 个数据中一一搜索大约 26000 个关键字。它比通过查询在 SQL 表中搜索要快得多。<?php$data = array(); // Array value added from mysql database table 2000 rows (columns : ID, title, description)$keywords = array(); // Array value added from mysql database table 26000 rows (column : keyword)if (!$data){&nbsp; &nbsp; echo "<script> alert('No Data available for Search...!');</script>";&nbsp; &nbsp; exit();}$MATCHID =array();$NOTMATCHID =array();set_time_limit(0);// 0 = NOLIMITfor($i = 0; $i < count($data); $i++){&nbsp; &nbsp; $ID = $data[$i]['ID'];&nbsp; &nbsp; foreach($keywords as $value)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $pattern = "/ ".$value." /i";&nbsp; //contains pattern&nbsp; &nbsp; &nbsp; &nbsp; $dd = preg_grep($pattern, $data[$i]);&nbsp; &nbsp; &nbsp; &nbsp; if($dd)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (in_array($ID, $NOTMATCHID))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $NOTMATCHID = array_diff($NOTMATCHID, array($ID));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $MATCHID[] = $ID;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break 1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!in_array($ID, $NOTMATCHID))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $NOTMATCHID[] = $ID;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}echo "<pre>";echo "****Found IDs****<BR>";print_r($MATCHID);echo "<BR>****NOT Found IDs****<BR>";print_r($NOTMATCHID);?>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript