建议性自动搜索无法正常工作


suggestive auto search not working properly

我在我的网站中使用了暗示性搜索,但不知何故它不起作用.我有一个名为客户的表:

id     customername      location
1       ram                delhi
2       monty             new delhi
3       sandeep            noida

现在我想在此表中自动搜索名称和位置,因此这是我的代码:

<?php
include("config.php"); 
$term=$_GET["term"];
$query=mysql_query("SELECT * FROM customers where customername like '%".$term."%' or location like '%".$term."%'");
$json=array();
    while($customer=mysql_fetch_array($query)){
         $json[]=array(
                    'value'=> $customer["customername "],
                    'label'=>$customer["customername "]." - ".$customer["id"],
                    'value'=> $customer["location"],
                    'label'=>$customer["location"]." - ".$customer["location"],
                        );
    }
 echo json_encode($json);
?>
在此查询的帮助下,我无法同时自动搜索客户名称和位置,这意味着如果我想搜索客户名称,那么它应该给出

客户名称,如果我在搜索字段中输入位置,它应该给出位置.目前它给了我最后一个值,如上面的代码中提到的,这次它只给我位置。

数组构造不正确。您有重复的键名称,因此它们将被覆盖。

假设您有:

$json[] = array(
    'value' => 'x',
    'label' => 'x-y',
    'value' => 'y',
    'label' => 'y-x'
);

var_dump($json);

输出为:

array (size=1)
  0 => 
    array (size=2)
      'value' => string 'y' (length=1)
      'label' => string 'y-x' (length=3)

即使[]$json,也不会自动让你拥有0=>array(x,y),1=>array(y,x),你需要指定键。

因为我不知道 MySQL 返回了多少行,所以我的示例将使用静态 while 循环:

$max=5; //max iterations
$i = 0; //first key
$k = $max+1; //second key
while ($i<=$max) {
    $json[$i] = array(
        'value' => 'x',
        'label' => 'x-y'
    );
    $json[$k] = array(
        'value' => 'y',
        'label' => 'y-x'
    );
    $i++;
    $k++;
}
ksort($json); //this line is only to return the array sorted by keys asc, not necessary, for the testing purpose
var_dump($json);

第二个关键$k永远不应该等于$i,这就是为什么我使用$i可以达到的最大,到$k的起点。输出:

array (size=12)
  0 => 
    array (size=2)
      'value' => string 'x' (length=1)
      'label' => string 'x-y' (length=3)
  1 => 
    array (size=2)
      'value' => string 'x' (length=1)
      'label' => string 'x-y' (length=3)
  2 => 
    array (size=2)
      'value' => string 'x' (length=1)
      'label' => string 'x-y' (length=3)
  3 => 
    array (size=2)
      'value' => string 'x' (length=1)
      'label' => string 'x-y' (length=3)
  4 => 
    array (size=2)
      'value' => string 'x' (length=1)
      'label' => string 'x-y' (length=3)
  5 => 
    array (size=2)
      'value' => string 'x' (length=1)
      'label' => string 'x-y' (length=3)
  6 => 
    array (size=2)
      'value' => string 'y' (length=1)
      'label' => string 'y-x' (length=3)
  7 => 
    array (size=2)
      'value' => string 'y' (length=1)
      'label' => string 'y-x' (length=3)
  8 => 
    array (size=2)
      'value' => string 'y' (length=1)
      'label' => string 'y-x' (length=3)
  9 => 
    array (size=2)
      'value' => string 'y' (length=1)
      'label' => string 'y-x' (length=3)
  10 => 
    array (size=2)
      'value' => string 'y' (length=1)
      'label' => string 'y-x' (length=3)
  11 => 
    array (size=2)
      'value' => string 'y' (length=1)
      'label' => string 'y-x' (length=3)

试试这个

<?php
include("config.php"); 
$term=$_GET["term"];
$query=mysql_query("SELECT * FROM customers where customername like '%".$term."%' or location like '%".$term."%'");
$json=array();
    while($customer=mysql_fetch_array($query)){
         $json[]=array(                   
                  $customer["id"]=>$customer["customername "]." - ".$customer["location"]); 
        } 
        echo json_encode($json);    
?>