galvin Posted January 11, 2011 Share Posted January 11, 2011 I know this is stupid newbie stuff, but I can't think about this the right way. And this question might very well make NO sense. If so, I apologize in advance. I'm trying to consolidate code. I basically have lots of quizzes, all with their own answers, which I compare using AJAX. I had individual pages for each quizzes answers to do that comparison and everything was working fine. It would do the following... <?php $userinput = strval($_GET['userinput']); $userinput= trim($userinput); $userinput = strtolower($userinput); switch ($userinput) { case 'heat': case 'miami heat': echo 'Miami Heat'; exit; case 'magic': case 'orlando magic': echo 'Orlando Magic'; exit; case 'jazz': case 'utah jazz': echo 'Utah Jazz'; exit; default: echo 'incorrectguess'; } ?> But now I am storing the answers in a MySQL database (for example, one answer set is in the database as "Miami Heat/heat/miami"...each allowed answer is separated by a "/", with the first answer being the "main" correct answer). So now, I want to just have ONE "comparison" page and populate the "cases" into that page dynamically, from the database. This is what I came up with so far, but it's not working... <?php $userinput = strval($_GET['userinput']); $userinput= trim($userinput); $userinput = strtolower($userinput); if ($_GET['quizid'] == "") { redirect_to('index.php'); } else { $quizid=$_GET['quizid']; } $sql = "SELECT * FROM answers WHERE quizid = $quizid"; $result = mysql_query($sql, $connection); if (!$result) { die("Database query failed: " . mysql_error()); } else { switch ($userinput) { while ($results = mysql_fetch_array($result)) { $answers = explode("/", $results['answer']); $n = count($answers); for ($i=1; $i < $n; $i++) { case '$answers[$i]': } echo '$answers[0]'; exit; default: echo 'incorrectguess'; } } ?> What I'm having trouble grasping is where I need to use "echo" or NOT use "echo". For example, where I have "switch ($userinput) {" in the NEW code, does that need to actually be "echo 'switch ($userinput) {';"? I'm trying different combos and failing, so just curious if someone can help me understand what, if anything, needs to be echoed when doing it the 2nd way (dynamic info) rather than the 1st way (static info) Quote Link to comment https://forums.phpfreaks.com/topic/224046-help-figuring-out-when-to-use-echo-if-at-all/ Share on other sites More sharing options...
Zurev Posted January 11, 2011 Share Posted January 11, 2011 First off, you don't need to echo switch, however at the end of each case, you need to break; Not exit. Look at the php documentation on php.net, it will help you a TON. exit kills the whole script, alias of die if you weren't sure. Also, your default case doesn't have a break or exit, it needs a break. Think of each case as, what happens when the case is this. Whatever happens in between case 'heat' and the next break, is the entire page for when the case is heat. Some resources: http://php.net/manual/en/control-structures.switch.php http://www.php.net/manual/en/control-structures.break.php Quote Link to comment https://forums.phpfreaks.com/topic/224046-help-figuring-out-when-to-use-echo-if-at-all/#findComment-1157750 Share on other sites More sharing options...
Simon Mayer Posted January 23, 2011 Share Posted January 23, 2011 Also, your default case doesn't have a break or exit, it needs a break. Not true. A break is only required to prevent the switch rolling onto the next case. As default is at the end, a break is of no use. It's not used in two of the examples at http://php.net/manual/en/control-structures.switch.php A break is needed earlier on, because it prevents the following deliberate behaviour. switch($var) { case 'headlinedmessage': echo 'here is the headline'; //will only say this when $var = headlinedmessage case 'message': echo 'here is the message'; //will say this when $var is headlinedmessage or message break; } Quote Link to comment https://forums.phpfreaks.com/topic/224046-help-figuring-out-when-to-use-echo-if-at-all/#findComment-1164080 Share on other sites More sharing options...
DavidAM Posted January 23, 2011 Share Posted January 23, 2011 ... As default is at the end ... The default does NOT have to be at the end of the switch. I sometimes put it at the top of the switch because it more clearly "documents" what that particular switch is doing. The more correct statement would be: "the LAST case (or default) does not require a break". I have also gotten into the habit of ALWAYS including a break, so if I come back later and add a case at the end of the switch, I don't forget to add a break to the preceding case. back to the OP It looks like you are trying to create a switch dynamically, and I really don't think you can do that. However, the solution, I think, is much simpler: $answers = explode("/", $results['answer']); if (in_array($userinput, $answers)) { echo $answers[0]; } else { echo 'incorrectguess'; } Quote Link to comment https://forums.phpfreaks.com/topic/224046-help-figuring-out-when-to-use-echo-if-at-all/#findComment-1164099 Share on other sites More sharing options...
Simon Mayer Posted January 23, 2011 Share Posted January 23, 2011 "the LAST case (or default) does not require a break". Eloquently put. I was simply stating that because default was at the end of that example, a break wasn't needed. I agree it's good practice to include it though, for the reasons you've specified. Quote Link to comment https://forums.phpfreaks.com/topic/224046-help-figuring-out-when-to-use-echo-if-at-all/#findComment-1164116 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.