暗示自动搜索无法正常工作

时间:2013-09-16 05:42:36

标签: php jquery mysql autocomplete autosuggest

我在我的网站上使用暗示性搜索,但不知何故,它不是workign.i有一个名为customer的表:

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);

?>

在此查询的帮助下,我无法同时自动搜索客户名称和位置,这意味着如果我想搜索客户名称,那么它应该提供客户名称,如果我将位置放在搜索字段中,则应该给出location.currently它给了我上面代码中提到的最后一个值,这次只给我一个位置。

2 个答案:

答案 0 :(得分:1)

您的阵列构造不正确。您有重复的密钥名称,因此它们将被覆盖。

假设你有:

$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,这就是为什么我使用我可以达到的最大$ $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)

答案 1 :(得分:0)

试试这个

<?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);    
?>