coogan Posted June 14, 2007 Share Posted June 14, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/55554-hel-with-multiple-search-criteria/ Share on other sites More sharing options...
harristweed Posted June 14, 2007 Share Posted June 14, 2007 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/55554-hel-with-multiple-search-criteria/#findComment-274527 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.