SO I have Jquery autocomplete working with mysql using mysql_fetch_array, I know I know,
I am having a little trouble returning every column in a row, so far a request on the on the name column of a table takes place, and returns the name column from the row.
My "events" Table looks like this,
name event price date color drinks
what I want to do is search, "name", "event" "date" and "color" columns but when a match is found in any column, to display results from them columns in a certain an order.
So if my "events" table contained the following data
name event price date color drinks
Helen Topmark 31 03/02/2013 red brandy
Gregg maxpower 27 01/02/2013 red whiskey
and somebody searches for "red helen" the autocomplete would fill like this
Helen Topmark red 03/02/2013
Also if somebody searches "red" I would like to prioritize the date
Gregg maxpower red 01/02/2013
Helen Topmark red 03/02/2013
Here is the code I currently have
$req = "SELECT name "
."FROM events "
."WHERE name LIKE '%".$_REQUEST['term']."%' ";
$query = mysql_query($req);
while($row = mysql_fetch_array($query))
{
$results[] = array('label' => $row['name']);
}
echo json_encode($results);
Thank you very much for your`e time.
You can do:
$req = "SELECT name, event, color, date FROM events WHERE (name LIKE '%".$_REQUEST['term']."%' OR event LIKE '%".$_REQUEST['term']."%' OR date LIKE '%".$_REQUEST['term']."%' OR color LIKE '%".$_REQUEST['term']."%')";
And your while loop:
while($row = mysql_fetch_array($query))
{
echo $row['name'] . ' ' . $row['event'] . ' ' . $row['color'] . ' ' . $row['date'];
}
Just giving some direction, still open to sql injection.
Update you query like below:
$search = $_REQUEST['term'];
$pieces = explode(" ", $search);
$in = "";
foreach($pieces as $val) {
$in = "'".$val."' IN (name,color) AND ";
if(end($pieces) === $val)
$in = "'".$val."' IN (name,color)";
}
NOTE: mysql_query() these functions are deprecated from PHP5.3.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.