Reputation: 647
i'm using suggestive search in my site but somehow its not workign.i have a table named as customers :
id customername location
1 ram delhi
2 monty new delhi
3 sandeep noida
now i want to auto-search in this table for name and location so here is my code for that:
<?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);
?>
with the help of this query i'm not able to auto search for customername and location both at one time means if i want to search customer name then it should give customer name and if i put location in search field it should give location.currently its giving me the last value as mentioned in above code this time its only giving me location.
Upvotes: 0
Views: 107
Reputation: 73
Just try this
<?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);
?>
Upvotes: 0
Reputation: 7034
Your array is not constructed properly. You have duplicate key names, so they will be overwritted.
Let's say you have:
$json[] = array(
'value' => 'x',
'label' => 'x-y',
'value' => 'y',
'label' => 'y-x'
);
var_dump($json);
The output is:
array (size=1)
0 =>
array (size=2)
'value' => string 'y' (length=1)
'label' => string 'y-x' (length=3)
Even []
in $json, doesn't automatically make you to have 0=>array(x,y),1=>array(y,x)
, you need to specify the keys.
Because I don't know how many rows the MySQL is returning, my example will be with static while loop:
$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);
The second key $k
should never be equal to $i
, that's why I use the maximum $i can reach, to starting point of $k
. Output:
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)
Upvotes: 1