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

时间:2019-04-18 07:54:41

标签: php jquery autocomplete

我使用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();

1 个答案:

答案 0 :(得分:-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";

这将支持

  1. “ Milind Patel”
  2. “ Patel Milind”
  3. “ Milind”
  4. “帕特尔”
  5. “ Milind”
  6. “ Patel”

所以您的代码应该像这样。

<?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();
相关问题