Sallie_ann Posted July 11, 2003 Share Posted July 11, 2003 Hi, I am trying to create a select statement that will extract data from a MySQL database when the user selects a number of options from 4 drop down list boxes. Here is my code for $where1 (there are 4 in total). switch($_POST[\'company_size\']) { case 1: $where1 = \"company_size LIKE \'Small\'\"; break; case 2: $where1 = \"company_size LIKE \'Medium\'\"; break; case 3: $where1 = \"company_size LIKE \'Large\'\"; break; default: break; } This all works fine but I am not sure how to construct the where clause so that only one or up to all four options can be selected from the options. $whereclause = $where1; if($whereclause == \"\") { $whereclause = $where2; } else { if($whereclause2 == \"\") { $whereclause = $where3; } else { if($whereclause3 == \"\") { $whereclause = $where4; } else { if($where4 != \"\") { $whereclause .= \" AND \" . $where2 .= \" AND \" . $where3 .= \" AND \" . $where4; } } if($whereclause != \"\") { $whereclause = \" WHERE \" . $whereclause; } $sql = \"SELECT name, County, sector, company_size FROM beacon $whereclause\"; Anybody able to help?! Quote Link to comment https://forums.phpfreaks.com/topic/700-select-where-if-else-statement-question/ Share on other sites More sharing options...
Avalanche Posted July 11, 2003 Share Posted July 11, 2003 Try something like... [php:1:857bb6e23d]switch($_POST[\'company_size\']) { case 1: $where1 = \"Small\"; break; case 2: $where1 = \"Medium\"; break; case 3: $where1 = \"Large\"; break; default: break; } $query = \"SELECT name, County, sector, company_size FROM beacon WHERE itemhere LIKE $where1\";[/php:1:857bb6e23d] Quote Link to comment https://forums.phpfreaks.com/topic/700-select-where-if-else-statement-question/#findComment-2325 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.