Jump to content

Hel with Multiple Search Criteria....


coogan

Recommended Posts

Hi,

 

I'm fairy new to PHP, having worked with asp in the past.

 

I'm creating a search feature that will allow users to search for items. The search has 3 criteria - Designer, Style, Colour. Each are presented in their own drop down box, all within one form.

 

Problem:

 

I've created code that works perfectly when users select something from all three criteria although my problem arises if they choose just one criteria e.g. (All Red items). At present they would be informed that 'no items are found', so depending on the various criteria they've chosen the info needs to be directed to different sql statements..

 

Any suggestions on the best, and least complex, method to solve this would be great. I do want to keep it to one 'search' button.

Link to comment
https://forums.phpfreaks.com/topic/55554-hel-with-multiple-search-criteria/
Share on other sites

I think this will do it

<?php
$sql="";
$search_query=array();
if(trim($_POST['Designer'])!=="")$search_query[]=" Desiner='$_POST[Designer]' ";
if(trim($_POST['Style'])!=="")$search_query[]=" Style ='$_POST[style]' ";
if(trim($_POST['Colour'])!=="")$search_query[]="Colour = '$_POST[Colour]' ";
$boxes=count($search_query);
if($boxes==1)
{
$detail=implode(" ",$search_query);
$sql="Where $detail";
}
if($boxes>1)
{
$sql="Where ";
$detail=implode(" and ",$search_query);
$sql.=$detail;
}
//put $sql in the mysql query
?>

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.