为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

侃侃尔雅 2022-01-20 20:30:22
在客户端使用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                           description1   dell laptop                     laptop i3 5000 xyz2   hp machine                      hp xyz abc utr3   supply motor 1500               Decorative Watches4   Deep Groove Drill Ball Bearing  Deep Hole Drill示例:Table2(26 000 行)id             keyword1              dell2              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>那么我怎样才能按照我的计划编写代码。
查看完整描述

2 回答

?
慕斯王

TA贡献1864条经验 获得超2个赞

根据您的示例(obv 您将适应您的代码):


function myFunction() {

  // Declare variables

  var input, filter, table, tr, td, i, txtValue;

  input = document.getElementById("myInput");

  filter = input.value.toUpperCase();

  table = document.getElementById("myTable");

  tr = table.getElementsByTagName("tr");


  // Loop through all table rows, and hide those who don't match the search query

  for (i = 0; i < tr.length; i++) {

    td = tr[i].getElementsByTagName("td")[0];

    if (td) {

      txtValue = td.textContent || td.innerText;

      if (txtValue.toUpperCase().indexOf(filter) > -1) {

        tr[i].style.display = "";

      } else {

        tr[i].style.display = "none";

      }

    }

  }

}

#myInput {

  background-image: url('/css/searchicon.png'); /* Add a search icon to input */

  background-position: 10px 12px; /* Position the search icon */

  background-repeat: no-repeat; /* Do not repeat the icon image */

  width: 100%; /* Full-width */

  font-size: 16px; /* Increase font-size */

  padding: 12px 20px 12px 40px; /* Add some padding */

  border: 1px solid #ddd; /* Add a grey border */

  margin-bottom: 12px; /* Add some space below the input */

}


#myTable {

  border-collapse: collapse; /* Collapse borders */

  width: 100%; /* Full-width */

  border: 1px solid #ddd; /* Add a grey border */

  font-size: 18px; /* Increase font-size */

}


#myTable th, #myTable td {

  text-align: left; /* Left-align text */

  padding: 12px; /* Add padding */

}


#myTable tr {

  /* Add a bottom border to all table rows */

  border-bottom: 1px solid #ddd;

}


#myTable tr.header, #myTable tr:hover {

  /* Add a grey background color to the table header and on hover */

  background-color: #f1f1f1;

}

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">


<table id="myTable">

  <tr class="header">

    <th style="width:60%;">ROW 1</th>

    <th style="width:40%;">ROW 2</th>

  </tr>

  <tr>

        <td>RESULT</td>

    <td>RESULT 2</td>

  </tr>

  <tr>

        <td>RESULT 3</td>

    <td>RESULT 4</td>

  </tr>

  <tr>

      <td>RESULT 5</td>

    <td>RESULT 6</td>

  </tr>

  <tr>

    <td>RESULT 7</td>

    <td>RESULT 8</td>

  </tr>

</table>

提示:如果要执行区分大小写的搜索,请删除 toUpperCase()。

提示:如果要搜索“国家”(索引 1)而不是“名称”(索引 0),请将 tr[i].getElementsByTagName('td')[0] 更改为 [1]。


查看完整回答
反对 回复 2022-01-20
?
桃花长相依

TA贡献1860条经验 获得超8个赞

这是我想要的最好的代码,它将使用 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)

{

    echo "<script> alert('No Data available for Search...!');</script>";

    exit();

}

$MATCHID =array();

$NOTMATCHID =array();


set_time_limit(0);// 0 = NOLIMIT

for($i = 0; $i < count($data); $i++)

{

    $ID = $data[$i]['ID'];

    foreach($keywords as $value)

    {

        $pattern = "/ ".$value." /i";  //contains pattern

        $dd = preg_grep($pattern, $data[$i]);

        if($dd)

        {

            if (in_array($ID, $NOTMATCHID))

            {

                $NOTMATCHID = array_diff($NOTMATCHID, array($ID));

            }

            $MATCHID[] = $ID;

            break 1;

        }

        else

        {

            if (!in_array($ID, $NOTMATCHID))

            {

                $NOTMATCHID[] = $ID;

            }

        }

    }

}

echo "<pre>";

echo "****Found IDs****<BR>";

print_r($MATCHID);

echo "<BR>****NOT Found IDs****<BR>";

print_r($NOTMATCHID);

?>


查看完整回答
反对 回复 2022-01-20
  • 2 回答
  • 0 关注
  • 208 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号