kb9yjg Posted May 30, 2008 Share Posted May 30, 2008 Hello I have been working on an advanced search script. I have two bits of code which I will post. I need one or the other to work. I need it to where thick1 and thick2 can be both filled in or left blank or one or the other filled in. The same goes for height1, height2 and date1 and date2. If someone can show me an easier way to get this all completed and working great that would be appreciated. switch code (pardon comments trying to work through to get it solved.) switch(1) { case (empty($_GET['thick1']) && empty($_GET['thick2'])): $q="select * from $table where pthick like $_GET[thick1] and pthick like $_GET[thick2]"; break; case ($_GET['thick1']!="" && $_GET['thick2']!=""): $q="select * from $table where pthick between $_GET[thick1] and $_GET[thick2]"; break; case ($_GET['thick1']!="" && $_GET['thick2']==""): $q="select * from $table where pthick = $_GET[thick1]"; break; case ($_GET['thick1']=="" && $_GET['thick2']!=""): $q="select * from $table where pthick = $_GET[thick2]"; break; case ($_GET['height1']=="" && $_GET['height2']==""): $q="select * from $table where pwidth like $_GET[height1] and pwidth like $_GET[height2]"; break; case ($_GET['height1']!="" && $_GET['height2']!=""): $q="select * from $table where pwidth between $_GET[height1] and $_GET[height2]"; break; case ($_GET['height1']!="" && $_GET['height2']==""): $q="select * from $table where pwidth = $_GET[height1]"; break; case ($_GET['height1']=="" && $_GET['height2']!=""): $q="select * from $table where pwidth = $_GET[height2]"; break; case ($_GET['item1']==""): $q="select * from $table where itemno != $_GET[item1]"; break; case ($_GET['item1']!=""): $q="select * from $table where itemno like $_GET[item1]"; break; case ($_GET['descr']==""): $q="select * from $table where itemno != $_GET[descr]"; break; case ($_GET['descr']!=""): $q="select * from $table where itemno like $_GET[descr]"; break; case ($_GET['date1']=="" && $_GET['date2']==""): $q="select * from $table where pdate like $_GET[date1] and pdate like $_GET[date2]"; break; case ($_GET['date1']!="" && $_GET['date2']!=""): $q="select * from $table where pdate between $_GET[date1] and $_GET[date2]"; break; case ($_GET['date1']!="" && $_GET['date2']==""): $q="select * from $table where pdate = $_GET[date1]"; break; case ($_GET['date1']=="" && $_GET['date2']!=""): $q="select * from $table where pdate = $_GET[date2]"; break; } and heres the if else statements if (isset($thick1) && isset($thick2)){ $q="select * from profiles where pthick between $_GET[thick1] and $_GET[thick2] order by pid LIMIT $start,$limit"; } if (isset($height1) && isset($height2)){ $q="select * from profiles where pwidth between $_GET[height1] and $_GET[height2] order by pid LIMIT $start,$limit"; } if(isset($descr)){ $q="select * from profiles where pdesc like '%$_GET[descr]%' order by pid LIMIT $start,$limit"; } if(isset($item1)){ $q="select * from profiles where itemno like '%$_GET[item1]%' order by pid LIMIT $start,$limit"; } if(isset($date1) && isset($date2)){ $q="select * from profiles where pdate between '$_GET[date1]' and '$_GET[date2]' order by pid LIMIT $start,$limit"; } if(isset($cmbCategory)){ $q="select * from profiles where ptype like '%$_GET[cmbCategory]%' order by pid LIMIT $start,$limit"; } if(isset($cmbNewCategory)){ $q="select * from profiles where ptype like '%$_GET[cmbNewCategory]%' order by pid LIMIT $start,$limit"; } Your help is greatly needed and appreciated. I Thank you in advance. Quote Link to comment https://forums.phpfreaks.com/topic/107930-complex-logic-help-needed-which-is-best-switch-or-if-in-this-case/ Share on other sites More sharing options...
.josh Posted May 30, 2008 Share Posted May 30, 2008 When I make a search engine type form, instead of making a query for every single possible combo, I try to set up the form so that I can I build the query string up piece by piece. The key is to look at all of your combos and find the common denominators and reduce, just like you learned in 2nd grade math, only you're using words instead of numbers. Quote Link to comment https://forums.phpfreaks.com/topic/107930-complex-logic-help-needed-which-is-best-switch-or-if-in-this-case/#findComment-553250 Share on other sites More sharing options...
kb9yjg Posted May 30, 2008 Author Share Posted May 30, 2008 hmmm... alright thanks for the help... I think I figured it out any way. I am working on the switch portion of it. and combining a few things as well... Thanks for the tip! Quote Link to comment https://forums.phpfreaks.com/topic/107930-complex-logic-help-needed-which-is-best-switch-or-if-in-this-case/#findComment-553269 Share on other sites More sharing options...
kb9yjg Posted May 30, 2008 Author Share Posted May 30, 2008 Hey everyone thanks for the help... Posting the code for anyone that may run into this issue in the future... if(isset($_GET['thick1']) || isset($_GET['thick2'])){ switch(1) { case ($_GET['thick1']!="" && $_GET['thick2']!=""): $q="select * from $table where pthick between $_GET[thick1] and $_GET[thick2] order by pthick LIMIT $start,$limit"; break; case (empty($_GET['thick1']) && empty($_GET['thick2'])): $q="select * from $table where pthick like '%$_GET[thick1]%' order by pthick LIMIT $start,$limit"; break; case ($_GET['thick1']!="" && empty($_GET['thick2'])): $q="select * from $table where pthick = $_GET[thick1] order by pthick LIMIT $start,$limit"; break; case (empty($_GET['thick1']) && $_GET['thick2']!=""): $q="select * from $table where pthick = $_GET[thick2] order by pthick LIMIT $start,$limit"; break; case ($_GET['item1']==""): $q="select * from $table where itemno like %$_GET[item1]% order by itemno LIMIT $start,$limit"; break; case ($_GET['item1']!=""): $q="select * from $table where itemno like %$_GET[item1]% order by itemno LIMIT $start,$limit"; break; case ($_GET['descr']==""): $q="select * from $table where pdesc like %$_GET[descr]% order by pdesc LIMIT $start,$limit"; break; case ($_GET['descr']!=""): $q="select * from $table where pdesc like %$_GET[descr]% order by pdesc LIMIT $start,$limit"; break; } } if(isset($_GET['height1']) || isset($_GET['height2'])){ switch(1) { case ($_GET['height1']!="" && $_GET['height2']!=""): $q="select * from $table where pwidth between $_GET[height1] and $_GET[height2] order by pwidth LIMIT $start,$limit"; break; case (empty($_GET['height1']) && empty($_GET['height2'])): $q="select * from $table where pwidth like '%$_GET[height1]%' order by pwidth LIMIT $start,$limit"; break; case ($_GET['height1']!="" && empty($_GET['height2'])): $q="select * from $table where pwidth = $_GET[height1] order by pwidth LIMIT $start,$limit"; break; case (empty($_GET['height1']) && $_GET['height2']!=""): $q="select * from $table where pwidth = $_GET[height2] order by pwidth LIMIT $start,$limit"; break; case ($_GET['item1']==""): $q="select * from $table where itemno like %$_GET[item1]% order by itemno LIMIT $start,$limit"; break; case ($_GET['item1']!=""): $q="select * from $table where itemno like %$_GET[item1]% order by itemno LIMIT $start,$limit"; break; case ($_GET['descr']==""): $q="select * from $table where pdesc like %$_GET[descr]% order by pdesc LIMIT $start,$limit"; break; case ($_GET['descr']!=""): $q="select * from $table where pdesc like %$_GET[descr]% order by pdesc LIMIT $start,$limit"; break; } } if(isset($_GET['date1']) || isset($_GET['date2'])){ switch(1) { case ($_GET['date1']!="" && $_GET['date2']!=""): $q="select * from $table where pdate between $_GET[date1] and $_GET[date2] order by pdate LIMIT $start,$limit"; break; case (empty($_GET['date1']) && empty($_GET['date2'])): $q="select * from $table where pdate like '%$_GET[date1]%' order by pdate LIMIT $start,$limit"; break; case ($_GET['date1']!="" && empty($_GET['date2'])): $q="select * from $table where pdate = $_GET[date1] order by pdate LIMIT $start,$limit"; break; case (empty($_GET['date1']) && $_GET['date2']!=""): $q="select * from $table where pdate = $_GET[date2] order by pdate LIMIT $start,$limit"; break; case ($_GET['item1']==""): $q="select * from $table where itemno like %$_GET[item1]% order by itemno LIMIT $start,$limit"; break; case ($_GET['item1']!=""): $q="select * from $table where itemno like %$_GET[item1]% order by itemno LIMIT $start,$limit"; break; case ($_GET['descr']==""): $q="select * from $table where pdesc like %$_GET[descr]% order by pdesc LIMIT $start,$limit"; break; case ($_GET['descr']!=""): $q="select * from $table where pdesc like %$_GET[descr]% order by pdesc LIMIT $start,$limit"; break; } } Quote Link to comment https://forums.phpfreaks.com/topic/107930-complex-logic-help-needed-which-is-best-switch-or-if-in-this-case/#findComment-553374 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.