kb9yjg Posted June 1, 2008 Share Posted June 1, 2008 Hello can someone tell me why this is working out the way it is? I have three sets of switches. One for thickness, one for height and the last for date. when I comment two of the three out the one that isnt commented out works perfectly fine. It is when I uncomment all three that i have issues with the switches. Heres the code with the top two commented out and the date uncommented. If someone can explain to me why its not working and how to fix it I would be eternally grateful! /*if(isset($_GET['thick1']) || isset($_GET['thick2'])){ switch(1) { case ($_GET['thick1']!="" && $_GET['thick2']==""): $q="select * from $table where pthick = $_GET[thick1] order by pthick ASC LIMIT $start,$limit"; break; case ($_GET['thick1']=="" && $_GET['thick2']!=""): $q="select * from $table where pthick = $_GET[thick2] order by pthick ASC LIMIT $start,$limit"; break; default: $q="select * from $table where pthick between $_GET[thick1] and $_GET[thick2] order by pthick ASC LIMIT $start,$limit"; break; } } if(isset($_GET['height1']) || isset($_GET['height2'])){ switch(1) { case ($_GET['height1']!="" && $_GET['height2']==""): $q="select * from $table where pwidth = $_GET[height1] order by pwidth ASC LIMIT $start,$limit"; break; case ($_GET['height1']=="" && $_GET['height2']!=""): $q="select * from $table where pwidth = $_GET[height2] order by pwidth ASC LIMIT $start,$limit"; break; default: $q="select * from $table where pwidth between $_GET[height1] and $_GET[height2] order by pwidth ASC LIMIT $start,$limit"; break; } } */ if(isset($_GET['date1']) || isset($_GET['date2'])){ switch(1) { case ($_GET['date1']!="" && $_GET['date2']==""): $q="select * from $table where pdate = $_GET[date1] order by pdate ASC LIMIT $start,$limit"; break; case ($_GET['date1']=="" && $_GET['date2']!=""): $q="select * from $table where pdate = $_GET[date2] order by pdate ASC LIMIT $start,$limit"; break; default: $q="select * from $table where pdate between $_GET[date1] and $_GET[date2] order by pdate ASC LIMIT $start,$limit"; break; } } Link to comment https://forums.phpfreaks.com/topic/108215-switches-work-fine-separately-but-together-they-dont/ Share on other sites More sharing options...
chronister Posted June 1, 2008 Share Posted June 1, 2008 The way it is set up it looks like the var $q is over written. The first switch executes and sets $q = 'something', the second executes and sets $q = 'something else' then the 3 one runs and again sets it to something else... for shits and giggles, uncomment all 3 echo $q at the bottom of the page and then run the code.. I think you will find $q equals whatever the last switch sets it as. Nate Link to comment https://forums.phpfreaks.com/topic/108215-switches-work-fine-separately-but-together-they-dont/#findComment-554690 Share on other sites More sharing options...
kb9yjg Posted June 1, 2008 Author Share Posted June 1, 2008 HAHAHA!! You hit it right on the head! It is set to the last statement in the switch for date! So now how would you rewrite this so that it takes each one into consideration. The funny part is that using if else and if elseif statements has not worked. Link to comment https://forums.phpfreaks.com/topic/108215-switches-work-fine-separately-but-together-they-dont/#findComment-554698 Share on other sites More sharing options...
DarkerAngel Posted June 1, 2008 Share Posted June 1, 2008 wait... so your trying to narrow down your query the best you can? I think you need to step back and thick about the logic, it seems to me that your just trying to jump in a get 'r done. I see that your having $q set for all of them, but maybe this was initially intentional, but with the conditional statements and the fact that PHP run mostly from the top down, then the last true is being used... so maybe you should have it set where the user sets the listing order, and then have the conditional statements help build the query down to properly reduce the results. That's my feedback... Link to comment https://forums.phpfreaks.com/topic/108215-switches-work-fine-separately-but-together-they-dont/#findComment-554702 Share on other sites More sharing options...
DarkerAngel Posted June 1, 2008 Share Posted June 1, 2008 Here is something I came up with, you can modify it as you wish, or if there is errors, (since it's untested) <?php /* Starts the Query String */ $q = "SELECY * FROM $table WHERE" if(isset($_GET['thick1']) || isset($_GET['thick2'])){ switch(1) { // Adds to the Query as Necessary case ($_GET['thick1']!="" && $_GET['thick2']==""): $q.=" pthick = $_GET[thick1]"; break; case ($_GET['thick1']=="" && $_GET['thick2']!=""): $q.=" pthick = $_GET[thick2]"; break; default: $q.=" pthick between $_GET[thick1] and $_GET[thick2]"; break; } $cond1 = true; } if(isset($_GET['height1']) || isset($_GET['height2'])){ if($cond1) // Adds the "AND" Statment to the Query if there was something was added from the previous condition $q.=" AND"; switch(1) { //Adds to the Query as Necessary case ($_GET['height1']!="" && $_GET['height2']==""): $q.=" pwidth = $_GET[height1]"; break; case ($_GET['height1']=="" && $_GET['height2']!=""): $q.=" pwidth = $_GET[height2]"; break; default: $q.=" between $_GET[height1] and $_GET[height2]"; break; } $cond2 = true; } if(isset($_GET['date1']) || isset($_GET['date2'])){ if($cond1 || $cond2) $q.= " AND"; switch(1) { case ($_GET['date1']!="" && $_GET['date2']==""): $q.=" pdate = $_GET[date1]"; break; case ($_GET['date1']=="" && $_GET['date2']!=""): $q.=" pdate = $_GET[date2]"; break; default: $q.=" between $_GET[date1] and $_GET[date2]"; break; } } switch($_GET['sortby']) { // Sets the sorting based on user input (Select Drop-Down recommended) case "thick": $q.=" ORDER BY pthick"; break; case "height": $q.=" ORDER BY pwidth"; break; default: $q.=" ORDER BY pdate"; } $q.=" ASC LIMIT $start,$limit"; //Finishes the Query ?> Link to comment https://forums.phpfreaks.com/topic/108215-switches-work-fine-separately-but-together-they-dont/#findComment-554707 Share on other sites More sharing options...
kb9yjg Posted June 2, 2008 Author Share Posted June 2, 2008 Thanks! Your suggestion helped to build the query step by step. Made some alterations to the code and it works AWESOME!! Link to comment https://forums.phpfreaks.com/topic/108215-switches-work-fine-separately-but-together-they-dont/#findComment-555379 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.