iceblox Posted July 27, 2007 Share Posted July 27, 2007 Hi Guys, Im going to be doing a search page with loads of drops downs but my question is if they leave one blank how can i get php not to pick it up. So my query will look like this $query = "SELECT * FROM Makes WHERE MakeName ='$Postblah' MakeSpud = '$Postblah'"; How would i make it work so if someone doesnt enter a MakeSpud php shows all MakeSpuds? Phil Quote Link to comment Share on other sites More sharing options...
jvrothjr Posted July 27, 2007 Share Posted July 27, 2007 $query = "SELECT * FROM Makes WHERE MakeName ='$Postblah' and MakeName != Null; Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 27, 2007 Share Posted July 27, 2007 Well, if you name all of your form inputs with the same name as their corresponding field names in the database, then you can do this: <?php $fields = array('field1','field2','field3');//an array containing all of the possible fields to search by $where = '';//define the variable foreach($fields as $v){ if(!empty($_POST[$v])){ $where .= $v.' = '.$_POST[$v].' AND '; } } $where = substr($where,0,strlen($where)-5);//stip out the last 'and' $query = "SELECT * FROM Makes WHERE $where"; ?> The idea is that you build the WHERE part of your query based on the input, and you only add a particular field if there is some data to search by. Quote Link to comment Share on other sites More sharing options...
iceblox Posted July 27, 2007 Author Share Posted July 27, 2007 Thanks GingerRobot Ive never useed arrays before so what would i put here? $where = '';//define the variable is this where i put $variable1, $variable2? Quote Link to comment Share on other sites More sharing options...
Barand Posted July 27, 2007 Share Posted July 27, 2007 this illustrates my preferred method http://www.phpfreaks.com/forums/index.php/topic,89842.msg360739.html#msg360739 Quote Link to comment 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.