实时数据搜索问题,如何通知用户在搜索中更加具体

时间:2019-04-13 23:30:35

标签: json ajax

我正在尝试利用通过php,ajax和json在网上找到的实时搜索演示,并希望对代码进行一些小的修改以要求用户在查询中“更具体”,以便不查询整个数据库。

我想将返回查询限制为不超过50条记录,并且如果查询的内容超出了该条以通知用户。

这是我要修改的来源(https://howtodecode.com/php/how-to-create-php-live-search-with-ajax-for-website/)。

在下面的代码中,我试图修改json,但是我不知道它的实现,因此我正在努力解决代码中的错误。

我在响应“ {” F_NAME“:” ...中看到以下内容:请缩小您的位置,太多的记录无法显示。“,” L_NAME“:”“}”。这似乎是正确的,但我无法正确解析。

我收到“找不到数据”。

    <?php
require_once('../TestSearch/Connections/Shop.php');

//fetch.php

$query = '';

if(isset($_POST["query"]))
{
 $search = str_replace(",", "|", $_POST["query"]);
 $query = "
 SELECT F_NAME, L_NAME, JOB_NO, CUST_NO, DESCRIPTIN, JOB_TYPE FROM line_job 
 WHERE F_NAME REGEXP '".$search."' 
 OR L_NAME REGEXP '".$search."' 
 OR JOB_NO REGEXP '".$search."' 
 OR CUST_NO REGEXP '".$search."' 
 OR DESCRIPTIN REGEXP '".$search."'
  OR JOB_TYPE REGEXP '".$search."'
 ";
}
else
{
 $query = "
 SELECT F_NAME, L_NAME, JOB_NO, CUST_NO, DESCRIPTIN, JOB_TYPE FROM line_job WHERE line_job = 47779
 ";
}

$result = $mysqli -> query($query);

if(mysqli_num_rows($result) > 50)
{
    $data = ['F_NAME' => '...please narrow your seach, too many records to display.','L_NAME' => ''];
    echo json_encode($data);
}else{
    while($row = mysqli_fetch_array($result))   
    {
     $data[] = $row;
    }
    echo json_encode($data);
}
?>

这是脚本

<script>
$(document).ready(function(){

 load_data();

 function load_data(query)
 {
  $.ajax({
   url:"fetch.php",
   method:"POST",
   data:{query:query},
   dataType:"json",
   success:function(data)
   {
    $('#total_records').text(data.length);
    var html = '';
    if(data.length > 0)
    {
     for(var count = 0; count < data.length; count++)
     {
      html += '<tr>';
      html += '<td>'+data[count].F_NAME+'</td>';
      html += '<td>'+data[count].L_NAME+'</td>';
      html += '<td>'+data[count].JOB_NO+'</td>';
      html += '<td>'+data[count].CUST_NO+'</td>';
      html += '<td>'+data[count].DESCRIPTIN+'</td>';
      html += '<td>'+data[count].JOB_TYPE+'</td></tr>';
     }
    }
    else
    {
     html = '<tr><td colspan="5">No Data Found</td></tr>';
    }
    $('tbody').html(html);
   }
  })
 }

 $('#search').click(function(){
  var query = $('#tags').val();
  load_data(query);
 });

});
</script>

1 个答案:

答案 0 :(得分:0)

通过稍微重新编码php并将查询结果限制为50,我能够解决我的问题。

<?php
require_once('../TestSearch/Connections/Shop.php');

//fetch.php

$query = '';

if(isset($_POST["query"]) and !empty($_POST["query"]))
{
 $search = str_replace(",", "|", $_POST["query"]);
 $query = "
 SELECT F_NAME, L_NAME, JOB_NO, CUST_NO, DESCRIPTIN, JOB_TYPE FROM line_job 
 WHERE F_NAME REGEXP '".$search."' 
 OR L_NAME REGEXP '".$search."' 
 OR JOB_NO REGEXP '".$search."' 
 OR CUST_NO REGEXP '".$search."' 
 OR DESCRIPTIN REGEXP '".$search."'
  OR JOB_TYPE REGEXP '".$search."'
 LIMIT 50
 ";
}
else
{
// if the form is submitted with no search criteria ""
$data = "";
echo json_encode($data);    
exit;
}


$result = $mysqli -> query($query);
//determine if the query returned a result or are there no records that match
$rowcount=mysqli_num_rows($result); 

if($rowcount > 0){

while($row = mysqli_fetch_array($result))   
{
 $data[] = $row;
}
echo json_encode($data);

}
else
{
$data = "";
echo json_encode($data);
}


?>