snorky Posted May 6, 2009 Share Posted May 6, 2009 After hours of aggravation, I'm looking for a fresh set of eyes. There must be some little GOTCHA that I'm missing. This script has 2 switch statements. One works, one doesn't. First I gather up the user's input: <!-- the form works as expected --> <form method=get action="script below"> <!-- pick list --> <select name="datex"> <option value="9">2009 <option value="8">2008 <option value="7">2007 <option value="6">2006 <option value="2">All Years </select> <!-- radio buttons --> <input type="radio" name="sortme" value="sortx" /> Last Name <br /> <input type="radio" name="sortme" value="sortx" /> Test Date <input type="Submit" value=" Get Report " class="jsbutton1" /> </form> Then I process the input: <?php // declare vars & describe method of populating vars $datex = $_GET["datex"]; //range of dates $sortx = $_GET["sortx"]; //sort by This is the switch statement that is making my life hell: // test the input and creates a string variable // the var created will be a "where" clause in an SQL query switch ($datex) { case 9: $adate=" where year(testdate) = 2009"; break; case 8: $adate=" where year(testdate) = 2008"; break; case 7: $adate=" where year(testdate) = 2007"; break; case 6: $adate=" where year(testdate) = 2006"; break; case 2: $adate=" "; break; // case 2 creates no "where..." clause } This switch statement works correctly: // test the input and creates a string variable // the var created will be an "order by" clause in an SQL query switch ($sortx) { case "lastname": $dord=" order by lname"; break; case "tdate": $dord=" order by testdate"; break; } // this is the "select..." clause of the SQL statement that we're building $base=" select lname, fname, bld, testdate from passed "; // this concatenates the variables into a complete SQL query $query=($base . $adate . $dord . ";"); Assume the users selects "2007" and "Last Name" The SQL query created by the above should be "select * from passed where testdate="2007" order by lname;" What I get is "select * from passed order by lname;" The bad news - the report includes all records, instead of just the year 2007 records The good news - it sorts correctly (the sort that the user requestd) I created a routine to test what's happening. This code is inserted after the switch statements. The idea is to read the variables that are passed to the concatenation routine. This code interrupts the flow of the program - just for testing. print "datex= " . $datex . "<br /><br />"; print "adate= " . $adate . "<br />"; print "sortx= " . $sortx . "<br /><br />"; print "dord= " . $dord . "<br />"; die("---"); // this test routine stops the action to see what is happening with the vars ?> This is what displays: datex= 9 adate= [it shows that the 1st switch statement gets the needed input,but nothing comes out] sortx= lastname dord= order by lname [it shows that the second switch statement gets the input, processes it, and generates the correct result] --- Quote Link to comment https://forums.phpfreaks.com/topic/157151-switch-statement-doesnt-switch-long/ Share on other sites More sharing options...
schilly Posted May 6, 2009 Share Posted May 6, 2009 pretty sure you datex var is a string so you would need to do: case '9': Quote Link to comment https://forums.phpfreaks.com/topic/157151-switch-statement-doesnt-switch-long/#findComment-828084 Share on other sites More sharing options...
Maq Posted May 6, 2009 Share Posted May 6, 2009 There's no need for all those switch statements. Use something like this: </pre> <form method="POST" action="<?php%20echo%20%24_SERVER%5B'PHP_SELF'%5D;%20?>"> 2009 2008 2007 2006 All Years Last Name Test Date </form> <br><br>if(isset($_POST['submit'])) <br>{<br> // declare vars & describe method of populating vars<br> $datex = $_POST['datex']; //range of dates<br> $sortx = $_POST['sortx']; //sort by <br> $query = "SELECT * FROM passed";<br> $query .= ($datex=='0') ? "" : " WHERE year(testdate) = $datex"; <br> $query .= (isset($sortx)) ? " ORDER BY $sortx" : "";<br> echo $query;<br>}<br Quote Link to comment https://forums.phpfreaks.com/topic/157151-switch-statement-doesnt-switch-long/#findComment-828088 Share on other sites More sharing options...
snorky Posted May 6, 2009 Author Share Posted May 6, 2009 pretty sure you datex var is a string so you would need to do: case '9': I tried it both ways - it made no difference. Quote Link to comment https://forums.phpfreaks.com/topic/157151-switch-statement-doesnt-switch-long/#findComment-828092 Share on other sites More sharing options...
schilly Posted May 7, 2009 Share Posted May 7, 2009 Maq is right as well. Quote Link to comment https://forums.phpfreaks.com/topic/157151-switch-statement-doesnt-switch-long/#findComment-828711 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.