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

里亚·苏珊娜

我使用PHP和jQuery创建了一个自动建议搜索框。提示用户在名为的表中插入名字和姓氏以查找数据库中存在的某人customerscustomers包含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();
米林德·帕特尔(Milind Patel)

像这样拆分$ 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();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Java中按姓氏和名字对对象的ArrayList进行排序

拆分姓氏和名字Netezza

搜索名字,中间名和姓氏的所有组合

保持订单结算的名字和姓氏从Woocommerce中的用户数据自动更新

mongoDB中的姓氏和名字组合搜索

PHP搜索名字和姓氏

正则表达式模式,用于搜索名字和姓氏的首字母

如何按姓氏对名字进行排序

在SQL Server中使用Like语句搜索名字和姓氏

使用搜索名字和姓氏混合php查询

从LINQ C#中包含全名的字段中搜索名字和姓氏

如果单个字段“名称”同时包含名字和姓氏,如何按姓氏排序

无法使用搜索参数的名字和姓氏显示搜索结果

尝试按名字和姓氏搜索

将名字和姓氏与姓氏全部大写

MongoDB和Spring Data-按名字/姓氏搜索用户

在PostgreSQL中对串联的名字和姓氏的搜索的优化

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

在Angular 2中搜索名字,姓氏和电子邮件

搜索名字和姓氏

在 Jquery 自动完成建议中显示额外的属性,例如在搜索过程中显示名字和姓氏

sql - 拆分名字和姓氏

为什么我不能使用 searchBar 搜索名字和姓氏?

如何在mongoDB中搜索用户的名字和姓氏并返回完整对象

如何在不同列的名字和姓氏的不同行上搜索多个名称?

SearchString 搜索操作与名字和姓氏

将名字和姓氏翻转为姓氏名字

Django 查詢集使用開始搜索名字和姓氏

如何使用 C# 在 Active Directory 中搜索用户的姓氏和名字