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
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
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.