简体   繁体   English

自动完成建议框无法正常工作?

[英]Auto complete suggestion box are not working properly?

code: 码:

<?php
  include('config.php');
  $return_arr = array();
  $term = $_GET['term'];
  $term = str_replace('.','',$term);
  $sql = "SELECT * FROM submission where keyword like '%".$term."%' or companyname like '%".$term."%' ORDER BY CASE WHEN keyword LIKE '%".$term."%' THEN 1
  ELSE 2 END";
  $r = mysqli_query($link,$sql);
  while($row = mysqli_fetch_assoc($r))
  {
    $key = explode(",", $row['keyword']);
    foreach ($key as $keyword) 
    {
      $return_arr[] = $keyword;
    }  
  }
  echo json_encode($return_arr);
?>

在此处输入图片说明

In my code I have created a auto complete suggestion box and its working but it always showing wrong result if I write (i) it always show result with (a) alphabet why not (i) and also want to search with short name. 在我的代码中,我创建了一个自动完成建议框,它可以正常工作,但是如果我写(i)它总是显示错误的结果,而我总是用(a)字母显示结果,为什么不显示(i)并且还想用短名称搜索。 So, How can I do this ?Please help me. 所以,我该怎么办?请帮助我。

Thank You 谢谢

if you want to compare result start with input character then remove % from beginning 如果要比较start with输入字符开头的结果start with从开头删除%

$sql = "SELECT * FROM submission where keyword like '".$term."%' or companyname 
like '".$term."%' ORDER BY CASE WHEN keyword LIKE '".$term."%' THEN 1
ELSE 2 END";

You used %$term% in your query. 您在查询中使用了%$term% So whether the data store the searched keyword anywhere in the string, it will display to you. 因此,无论数据是否将搜索到的关键字存储在字符串中的任何位置,它都会向您显示。 So if you want to search the data start with a specific character remove % from the start of your query to make it as $term% as 因此,如果您要搜索以特定字符开头的数据,请从查询开始处删除% ,以使其作为$term%作为

$sql = "SELECT * FROM submission where keyword like '".$term."%' or companyname 
like '".$term."%' ORDER BY CASE WHEN keyword LIKE '".$term."%' THEN 1
ELSE 2 END";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM