I'm using the following code along with Jquery autocomplete to populate the search suggestions on my site..
$results = mysql_query('SELECT DISTINCT artist FROM mm_albums WHERE artist LIKE "'. mysql_real_escape_string($_REQUEST['term']) .'%" ORDER BY artist ASC LIMIT 0,10', $dbcon);
$data = array();
if ( $results && mysql_num_rows($results) )
{
while( $row = mysql_fetch_array($results, MYSQL_ASSOC) )
{
$data[] = array(
'label' => $row['artist'],
'value' => $row['artist']
);
}
}
echo json_encode($data);
flush();
This is working fine but I want to be able to search across multiple columns for various results e.g. -
an artist name from the artist column
a track name from the name column
a record label form the label column
I've just spent 2 hours reading tons of different ways to do it but none of them are quite right for what i'm trying to do and I'm having trouble formatting the query correctly.
Has anybody got any suggestions?
--EDITED BELOW TO ADD THE SOLUTION--
This is how I managed to get it working thanks to Ed's suggestions in case anybody else is trying to do this.
This will search 4 different columns from 2 different tables and match the search term typed by the user with anything it finds in any of them while stripping out duplicate results. This works flawlessly with JQuery Autocomplete and here's how to set it up.
1) Create the following php file and name it search.php
<?php
if ( !isset($_REQUEST['term']) )
exit;
$connect = mysql_connect('dbhost', 'dbusername', 'dbpassword') or die( mysql_error() );
mysql_select_db('dbname');
$results = mysql_query('SELECT artist AS shout FROM albums WHERE artist LIKE "'. mysql_real_escape_string($_REQUEST['term']) .'%" UNION SELECT name FROM albums WHERE name LIKE "'. mysql_real_escape_string($_REQUEST['term']) .'%" UNION SELECT label FROM albums WHERE label LIKE "'. mysql_real_escape_string($_REQUEST['term']) .'%" UNION SELECT track_name FROM tracks WHERE track_name LIKE "'. mysql_real_escape_string($_REQUEST['term']) .'%" ORDER BY 1 LIMIT 10', $connect);
$data = array();
if ( $results && mysql_num_rows($results) )
{
while( $row = mysql_fetch_array($results, MYSQL_ASSOC) )
{
$data[] = array(
'label' => $row['shout'],
'value' => $row['shout']
);
}
}
echo json_encode($data);
flush();
?>
2) In your new script replace dbhost
, dbusername
, dbpassword
& dbname
with the actual details that you use to connect to your database. Although I would suggest that you didn't have this info stored in this file. Much better to store your connection info in a separate file, locate it outside of the webroot and `include' it here instead
3) Replace the queries with your own. This is how mine are set up so you can see what you need to replace..
First it's renaming the search string to shout
(just for clarity) then it's searching the column artist
in the table albums
and then matching artist
to the search term
generated by Autocomplete.
This format repeats searching the following...
name
from albums
label
from albums
track_name
from tracks
Just replace this info with the columns & tables that you wish to search, save the file and upload it.
4) Add the following search form to your page where pathtoyoursearchenginescript.php
points to your search engine.
<form action="pathtoyoursearchenginescript.php" method="post">
Search: <input type="text" id="suggest" /> <input type="submit" value="Search" />
</form>
5) Assuming that you already have the required .js libraries loaded on your page you can just fling this chunk of code somewhere and you're done!
<script type="text/javascript">
jQuery(document).ready(function($){
$('#suggest').autocomplete({source:'search.php', minLength:2});
});</script>
Note: The code above assumes that search.php
is in your webroot, if it isn't make sure that you update this with the correct location of your file.
Hope this is useful to somebody ;)