【问题标题】:suggestive auto search not working properly提示性自动搜索无法正常工作
【发布时间】:2013-09-16 15:53:51
【问题描述】:

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

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 它给了我上面代码中提到的最后一个值,这次它只给了我位置。

【问题讨论】:

  • 这是我问题的答案吗??
  • 你检查过链接了吗,这是建议不要使用已弃用的 mysql_* 函数而且你的代码易受 SQL 注入攻击
  • 是的,我已经检查过了,它会跟随它,但现在我想要上面发布的问题的答案。

标签: php jquery mysql autocomplete autosuggest


【解决方案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=&gt;array(x,y),1=&gt;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)

【讨论】:

  • 感谢您的宝贵帮助,对不起兄弟,我是 json 的新手,因此无法获得所需的结果。你能告诉我在输入客户名称和位置后如何获得不同的值。
  • 如果这不起作用,您可能想要更改密钥?不仅是值和标签,而且是“值,标签,值1,标签1”,所以它们都会不同
  • 我已将标签更改为 label1 并将值更改为 value1,但它始终采用第二个值意味着它采用 value1 label1 即位置,我希望如果我输入客户名称,它会给出,如果我输入位置它会给我,所以我现在应该做什么。
  • 我的意思是你有 4 个键,其中 4 个的名称不同,第一个 -> value,第二个 -> label,第三个 -> value1,第四个 -> label1,因此如果你只想显示设置的内容 -> if(empty($json['value'])) { echo $json['value1']; } 你明白了 -> 如果键值为空,则打印 value1,否则打印请求的值
  • if(empty($json['value'])) { echo $json['value1'];在我的情况下,此条件不满足因为所有数据都来自一个表并且任何值都不能为空,因为当我搜索它在整行中搜索的任何术语并且在任何行中它永远不会找到空字段时。
【解决方案2】:

试试这个

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

【讨论】:

  • 感谢您的帮助,但我不希望两者一起归档盒子
猜你喜欢
  • 2016-09-23
  • 1970-01-01
  • 2019-08-04
  • 2021-11-10
  • 2017-01-28
  • 2013-12-23
  • 2015-07-27
  • 2014-07-17
  • 1970-01-01
相关资源
最近更新 更多