Jump to content

A little help customising an autocomplete search box


Hyperjase

Recommended Posts

Hi all,

 

I'm trying to implement a search box that doesn't require a search box to be submitted, but the results are formatted as autocomplete, so when it's typed it shows underneath.

 

I have the basics in place, I can search and get results for one field in the table I'm searching in.  I really want to be able to search for more than one field though, currently it searches within "title" and I'd like it to be able to pull results from "artist" and "album", all within the same table.  I've had a try with the current code but can't get it to work, I've tried using OR inside WHERE syntax but not too sure I did it right.

 

Also, I've tried to make it so if there's no results it just displays "No results" but it went the two opposite ways, it did work, but for all results, and it didn't work -- at all! A little confused as I thought my code was working, I used:

 

if ($row = 0) { echo "No results"; }

 

I tried all combinations of using $row but no luck.

 

Here's the entire code I have for the full search form:

 

<?php

include('config.php'); 
$SQL_FROM = 'songlist';
$SQL_WHERE = 'title';

?>
<?php
$searchq		=	strip_tags($_GET['q']);
$getRecord_sql	=	'SELECT * FROM '.$SQL_FROM.' WHERE '.$SQL_WHERE.' LIKE "'.$searchq.'%" LIMIT 20';
$getRecord		=	mysql_query($getRecord_sql);
if(strlen($searchq)>0){

// ---------------------------------------------------------------- // 
// AJAX Response                                                         // 
// ---------------------------------------------------------------- // 

echo '<ul>';
while ($row = mysql_fetch_array($getRecord)) {?>
	<li><a href="playlist.php?search=<?php echo $row['title']; ?>&dcat=title"><?php echo $row['title']; ?></a><a href="playlist.php?search=<?php echo $row['artist']; ?>&dcat=artist"> <?php echo $row['artist']; ?></a><a href="playlist.php?search=<?php echo $row['album']; ?>&dcat=album"><?php echo $row['album']; ?></a></li>
<?php 
 } 
echo '</ul>';
?>
<?php } ?>

 

Is there any way of making it so if an album result is shown, it shows just the album title, at the moment it's pulling just the title fields .... same for artist?

 

Thanks for your help!

 

Jason

After a little figuring I've managed to search in all three rows I need to, so it can either be the track, artist or album.  Now i've done that I wasn to be able to format it slightly differently, if the track is searched, it shows track, artist and album, if you search artist or album, I only want one for each result to show, I've been testing code which effectively pulls that info, but will pull multiple times for the same row (say James Horner, will pull 100+ times), what would I need to show only the unique results, ie multiple results only once.

 

Thanks,

 

Jason

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.