按名字和姓氏自动完成搜索

Posted

技术标签:

【中文标题】按名字和姓氏自动完成搜索【英文标题】:Autocomplete search by First Name and Last Name 【发布时间】:2019-09-08 12:33:47 【问题描述】:

我使用 php 和 jQuery 创建了一个自动建议搜索框。提示用户插入名字和姓氏以查找存在于我的数据库中的某人,该表位于名为customers 的表中。表 customers 包含 2 列,first_namelast_name

当您输入名字时,我的搜索工作正常,但在按空格移动并输入姓氏后没有给出任何结果。按下空格键时似乎出现了整个问题。知道如何解决吗?

$(document).ready(function($)
  $("#customers").autocomplete(
    source: "fetch_customers.php?cc=<?php echo $agencyid; ?>",
    minLength: 2,
    select: function(event, ui) 
      var code = ui.item.id;
      if (code != '#') 
        location.href = '/view-customer/' + code;
      
    ,
    open: function(event, ui) 
      $(".ui-autocomplete").css("z-index", 1000);
    
  );
);
<?php
  $countrycode1 = $_GET['cc'];
  $term = trim(strip_tags($_GET['term'])); 
  $term = preg_replace('/\s+/', ' ', $term);

  $a_json = array();
  $a_json_row = array();

  $a_json_invalid = array(array("id" => "#", "value" => $term, "label" => "Only letters and digits are permitted..."));
  $json_invalid = json_encode($a_json_invalid);

  if ($data = $conn->query("SELECT id, first_name, last_name FROM customers WHERE agency_id='$countrycode1' AND (first_name LIKE '%$term%' OR last_name LIKE '%$term%') ORDER BY first_name , last_name")) 
  
    while($row = mysqli_fetch_array($data)) 
    
      $firstname = htmlentities(stripslashes($row['first_name']));
      $lastname = htmlentities(stripslashes($row['last_name']));
      $code = htmlentities(stripslashes($row['id']));
      $a_json_row["id"] = $code;
      $a_json_row["value"] = $firstname.' '.$lastname;
      $a_json_row["label"] = $firstname.' '.$lastname;
      array_push($a_json, $a_json_row);
    
  

  /* jQuery wants JSON data */
  $json = json_encode($a_json);
  print $json;
  flush();
  $conn->close();

【问题讨论】:

用 concat 函数改变查询会有帮助吗? 您应该将 $term 拆分为 ' ' 并使用单独的值查询 first_namelast_name。或者您应该使用更复杂的查询来查询您的数据库:获取包含合并值 first_name + ' ' + last_name 的列,然后将其与 $term 进行比较。 任何示例代码可以帮助我吗? 尝试替换 $term = preg_replace('/\s+/', ' ', $term); with $term = preg_replace('/\b[\w-]+\b/', ' ', $term); @hans-konig,不,那不行。 【参考方案1】:

像这样拆分你的 $term。

$splited_term = explode(" ",$term);
$first_term = $splited_term[0];
$last_term = (isset($splited_term[1]) && !empty($splited_term)) ? $splited_term[1] : null;

根据您的查询生成查询

$query = "SELECT id, first_name, last_name FROM customers WHERE agency_id='$countrycode1' AND ";

if(!empty($first_term))
     $query.= "(first_name LIKE '%$first_term%' OR last_name LIKE '%$first_term%'" ;

if(!empty($last_term))
  $query  .= ((!empty($first_term)) ? " OR " : " ( " )." first_name LIKE '%$last_term%' OR last_name LIKE '%$last_term%')" ;


$query .= ((empty($last_term)) ? ")" : "")." ORDER BY first_name , last_name";

这将支持

    “米林德·帕特尔” “帕特尔·米林德” “温和” “帕特尔” “温和” “帕特尔”

所以你的代码应该是这样的。

<?php
  $countrycode1 = $_GET['cc'];
  $term = trim(strip_tags($_GET['term'])); 
  $term = preg_replace('/\s+/', ' ', $term);

  $splited_term = explode(" ",$term);
  $first_term = $splited_term[0];
  $last_term = (isset($splited_term[1]) && !empty($splited_term)) ? $splited_term[1] : null;

  $a_json = array();
  $a_json_row = array();

  $a_json_invalid = array(array("id" => "#", "value" => $term, "label" => "Only letters and digits are permitted..."));
  $json_invalid = json_encode($a_json_invalid);

  $query = "SELECT id, first_name, last_name FROM customers WHERE agency_id='$countrycode1' AND ";

  if(!empty($first_term))
    $query.= "(first_name LIKE '%$first_term%' OR last_name LIKE '%$first_term%'" ;
  
  if(!empty($last_term))
    $query  .= ((!empty($first_term)) ? " OR " : " ( " )." first_name LIKE '%$last_term%' OR last_name LIKE '%$last_term%')" ;
  

  $query .= ((empty($last_term)) ? ")" : "")." ORDER BY first_name , last_name";

  if ($data = $conn->query($query)) 
  
    while($row = mysqli_fetch_array($data)) 
    
      $firstname = htmlentities(stripslashes($row['first_name']));
      $lastname = htmlentities(stripslashes($row['last_name']));
      $code = htmlentities(stripslashes($row['id']));
      $a_json_row["id"] = $code;
      $a_json_row["value"] = $firstname.' '.$lastname;
      $a_json_row["label"] = $firstname.' '.$lastname;
      array_push($a_json, $a_json_row);
    
  

  /* jQuery wants JSON data */
  $json = json_encode($a_json);
  print $json;
  flush();
  $conn->close();

【讨论】:

让我检查一下@Milind。 现在在以下行中显示错误: $last_term = (isset($splited_term[1]) && !empty($splited_term)) $splited_term[1] : null; 解析错误:语法错误,意外'$splited_term' (T_VARIABLE) 这行代码还是有错误 $last_term = (isset($splited_term[1]) && !empty($splited_term)) $splited_term[1] : null;错误指出:解析错误:语法错误,意外'$splited_term 不,那行没有改变,我得到了同样的错误。

以上是关于按名字和姓氏自动完成搜索的主要内容,如果未能解决你的问题,请参考以下文章

如何在 PHP 的自动完成中根据其名字和姓氏返回 ID?

自动完成代码?

使用具有多个值的EasyAutocomplete插件自动完成搜索输入

jQuery自动完成,通过选择建议选项填充多个字段

如何关闭输入自动完成?

如何使用 MongoDB 按名字和姓氏搜索用户?