multiple words search form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: multiple words search form

Post by Celauran »

Code: Select all

$query = "SELECT * FROM privatelistings WHERE make LIKE '%$searchTerm%'";
You're passing in a single search term while your description suggests you may want multiple. What if you split apart the query string on spaces to break out the individual words, then iterated over the results to construct your WHERE clause?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: multiple words search form

Post by Celauran »

User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: multiple words search form

Post by Celauran »

There may be a % missing from model, but it otherwise looks fine. What about the multiple word search you were referring to in the first post?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: multiple words search form

Post by Celauran »

A few things here. Since you're adding % before and after a search term, what's going to happen if that search term is empty? After running trim on an input, if that input is an empty string, you probably want to exclude it from the search altogether. That will address the test problem.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: multiple words search form

Post by Celauran »

That's better than returning everything, but what if I'm just interested in a make and not necessarily a specific model (ie. what if I want all results for Acura)?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: multiple words search form

Post by Celauran »

What about using something like

Code: Select all

$make  = trim($_GET['make']);
$model = trim($_GET['model']);

$where = [];
if (!empty($make)) {
	$where[] = "make LIKE '%{$make}%'";
}
if (!empty($model)) {
	$where[] = "model LIKE '%{$model}%'";
}

$where_clause = implode(' OR ', $where);

if (!empty($where_clause)) {
	$query = "SELECT * FROM privatelistings WHERE {$where_clause}";
}
Note there are definitely problems with this, like the lack of input sanitization and the use of SELECT *, but it should give you the right idea.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: multiple words search form

Post by Celauran »

If you're using a really old version of PHP, replace [] with array()
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: multiple words search form

Post by Celauran »

ianhaney wrote:

Code: Select all

//check whether the name parsed is empty
if($make == "" || $model = "")
{
	echo "Enter the make and model you are searching for.";
	exit();
}
Should this even be in your code? The query is deliberately flexible, but this snippet above still requires both fields be filled.
Post Reply