Search with multiple keywords

Hi everyone,

I have a search feature that allows users to search by keyword or other parameters (drop down menus). I would like them to be able to search for keywords, plural. According to other answers, I need to use the explode/implode function, but I’m not quite sure when and where to implement it.

If possible, I would also like there to be no glitch if the user decides to use commas…but getting multiple keywords is definitely first priority.

The second to last “if” statement is for the keywords in question.

Any help would be invaluable. Thank you in advance!


	$select = 'SELECT DISTINCT joke.id, joke.joketext, joke.jokedate, 
	            author.id AS author_id, author.name AS author_name, 
	            jokecategory.jokeid AS cat_jokeid, jokecategory.categoryid AS joke_catid, 
	            category.id AS cat_id, category.name as cat_name, 
	            joketheme.jokeid AS theme_jokeid, joketheme.themeid AS joke_themeid, theme.id
	            AS theme_id, theme.name AS theme_name,
	            jokegeofocus.jokeid AS geofocus_jokeid, jokegeofocus.geofocusid AS joke_geofocusid,
	            geofocus.id AS geofocus_id, geofocus.name AS geofocus_name';
	$from   = ' FROM joke
	            inner join author on (joke.authorid = author.id)
	            inner join jokecategory on (joke.id = jokecategory.jokeid)
	            inner join category on (jokecategory.categoryid = category.id)
	            inner join joketheme on (joke.id = joketheme.jokeid)
	            inner join theme on (joketheme.themeid = theme.id)
	            inner join jokegeofocus on (joke.id = jokegeofocus.jokeid)
	            inner join geofocus on (jokegeofocus.geofocusid = geofocus.id)'; 
	$first_where = ' where ';
	$where = '';
	$in = ' ORDER BY jokedate DESC';

	if (is_numeric($_POST['aid']))
	{   // An author is selected
	    $where.= $first_where.' authorid='.$_POST['aid'];
	    $first_where = ' and ';
	}

	if (is_numeric($_POST['cid']))
	{   // A category is selected
	    $where.= $first_where.' categoryid='.$_POST['cid'];
	    $first_where = ' and ';
	}

	if (is_numeric($_POST['tid']))
	{   // A theme is selected
	    $where.= $first_where.' themeid='.$_POST['tid'];
	    $first_where = ' and ';
	}

	if (is_numeric($_POST['gfid']))
	{   // A region is selected
	    $where.= $first_where.' geofocusid='.$_POST['gfid'];
	    $first_where = ' and ';
	}

	if (isset($_POST['searchtext']) and $_POST['searchtext'] != '')
	{   // Some search text was specified
	    $where.= $first_where.' keywords LIKE "%'.(mysql_real_escape_string($_POST['searchtext'], $dbcnx)).'%"';
	}

	if($where == '')
	{   // prevents returning the whole database, if form is empty
	    $where = ' limit 20';
	}
?>	

You can use implode() to join together your array of keywords into a string to be included in the IN list of your WHERE clause of your sql query.