SelfObscurity Posted October 13, 2010 Share Posted October 13, 2010 Morning, I'm working on a web site using a switch to separate different calls from a MySQL database. My style looks like this: <?php switch ($_GET['page']) { case 'news': echo ("NEWS HERE"); } break; case 'schedule'; echo ("SCHEDULE HERE"); } break; default: echo ("MAIN PAGE HERE"); break; } What I am trying to do, is specify that if the page is news, there can be an additional variable for id. I attempted before and after the break; for the news case, to make a new switch, but it isn't taking. I've looked around online, but can't find any information about this. Can anyone offer a hand? Quote Link to comment https://forums.phpfreaks.com/topic/215782-a-switch-inside-a-switch/ Share on other sites More sharing options...
trq Posted October 13, 2010 Share Posted October 13, 2010 How about you post the code that "isn't taking"? and we go from there. Quote Link to comment https://forums.phpfreaks.com/topic/215782-a-switch-inside-a-switch/#findComment-1121776 Share on other sites More sharing options...
premiso Posted October 13, 2010 Share Posted October 13, 2010 switch ($_GET['page']) { case 'news': echo ("NEWS HERE"); $id2 = "extra id"; break; case 'schedule'; echo ("SCHEDULE HERE"); break; default: echo ("MAIN PAGE HERE"); break; } You had some syntax issues there, removed the extra } from it. Also showed how / where you would put the second "id". If you need a better example, you need to provide us with more thorough information and examples. Quote Link to comment https://forums.phpfreaks.com/topic/215782-a-switch-inside-a-switch/#findComment-1121779 Share on other sites More sharing options...
mikosiko Posted October 13, 2010 Share Posted October 13, 2010 <?php switch ($_GET['page']) { case 'news': echo ("NEWS HERE"); } break; case 'schedule'; echo ("SCHEDULE HERE"); } break; default: echo ("MAIN PAGE HERE"); break; } first... you have 2 incorrect placed }'s in the first and second echo... if I understood correctly your question you want to implement a nested switch ... in that case it could look like this: <?php switch ($_GET['page']) { case 'news': echo ("NEWS HERE"); switch ($_GET['id']) { case 1: echo "ID is 1"; break; case 2: echo 'ID is 2'; break; default: echo 'ID is unknown'; break; } break; case 'schedule'; echo ("SCHEDULE HERE"); break; default: echo ("MAIN PAGE HERE"); break; } Quote Link to comment https://forums.phpfreaks.com/topic/215782-a-switch-inside-a-switch/#findComment-1121783 Share on other sites More sharing options...
SelfObscurity Posted October 13, 2010 Author Share Posted October 13, 2010 I've made attempts to implement this nested switch, but it's not working. The code works until I add this nested switch. Am I overlooking something? <?php switch ($_GET['page']) { // NEWS CASE case 'news'; $link = mysql_connect ($host, $user, $pass); mysql_select_db ($db, $link); $query = "SELECT * from news ORDER BY date DESC LIMIT 5"; $result = mysql_db_query ($db, $query, $link); while ($row = mysql_fetch_array($result)) { echo ("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"> <tr> <td colspan=\"2\" class=\"Main-Header\"><a href=\"index.php?page=news&id=".$row['id'] . "\">$row[title]</a></td> </tr> <tr> <td colspan=\"2\" class=\"Main-PostInfo\">Posted By $row[author] on $row[date]</td> </tr> <tr> <td colspan=\"2\" class=\"Main-Content\">$row[author]</td> </tr> <tr> <td> </td> <td class=\"Main-Comment\">Comment | (0) Comments</td> </tr> </table><br /> \n"); switch ($_GET['id']) { $link = mysql_connect ($host, $user, $pass); mysql_select_db ($db, $link); $query = "SELECT * from news WHERE $id = id"; $result = mysql_db_query ($db, $query, $link); while ($row = mysql_fetch_array($result)) { $id = $_GET['id']; case '$id'; echo ("NEWS POST $row['id']"); break; default: $link = mysql_connect ($host, $user, $pass); mysql_select_db ($db, $link); $query = "SELECT * from news ORDER BY date DESC LIMIT 5"; $result = mysql_db_query ($db, $query, $link); while ($row = mysql_fetch_array($result)) { echo ("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"> <tr> <td colspan=\"2\" class=\"Main-Header\"><a href=\"index.php?page=news&id=".$row['id'] . "\">$row[title]</a></td> </tr> <tr> <td colspan=\"2\" class=\"Main-PostInfo\">Posted By $row[author] on $row[date]</td> </tr> <tr> <td colspan=\"2\" class=\"Main-Content\">$row[author]</td> </tr> <tr> <td> </td> <td class=\"Main-Comment\">Comment | (0) Comments</td> </tr> </table><br /> \n"); } break; case 'about'; $link = mysql_connect ($host, $user, $pass); mysql_select_db ($db, $link); $query = "SELECT * from aboutvow"; $result = mysql_db_query ($db, $query, $link); while ($row = mysql_fetch_array($result)) { echo ("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"> <tr> <td class=\"Main-Header\">$row[title]</td> </tr> <tr> <td class=\"Main-Content\">$row[content]</td> </tr> </table><br /> \n"); } break; default: $link = mysql_connect ($host, $user, $pass); mysql_select_db ($db, $link); $query = "SELECT * from news ORDER BY date DESC LIMIT 5"; $result = mysql_db_query ($db, $query, $link); while ($row = mysql_fetch_array($result)) { echo ("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"> <tr> <td colspan=\"2\" class=\"Main-Header\"><a href=\"index.php?page=news&id=".$row['id'] . "\">$row[title]</a></td> </tr> <tr> <td colspan=\"2\" class=\"Main-PostInfo\">Posted By $row[author] on $row[date]</td> </tr> <tr> <td colspan=\"2\" class=\"Main-Content\">$row[author]</td> </tr> <tr> <td> </td> <td class=\"Main-Comment\"><a href=\"index.php?page=news&id=".$row['id'] . "&action=comment\">Comment</a> | <a href=\"index.php?page=news&id=".$row['id'] . "\"> View Comments</a></td> </tr> </table><br /> \n"); } break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/215782-a-switch-inside-a-switch/#findComment-1121787 Share on other sites More sharing options...
mikosiko Posted October 13, 2010 Share Posted October 13, 2010 compare this lines that you have : switch ($_GET['id']) { $link = mysql_connect ($host, $user, $pass); mysql_select_db ($db, $link); $query = "SELECT * from news WHERE $id = id"; $result = mysql_db_query ($db, $query, $link); while ($row = mysql_fetch_array($result)) { $id = $_GET['id']; case '$id'; echo ("NEWS POST $row['id']"); break; default: with the first switch in your code and try to catch the differences... if not... read about the switch proper format here http://php.net/manual/en/control-structures.switch.php Quote Link to comment https://forums.phpfreaks.com/topic/215782-a-switch-inside-a-switch/#findComment-1121837 Share on other sites More sharing options...
btherl Posted October 13, 2010 Share Posted October 13, 2010 The "case" have to go directly inside the switch. You can't have code inbetween the switch and the case, and that especially applies to "while" loops. Also the syntax for case requires ":", not ";" I recommend you start with writing simple switches, which do something basic like printing out "This is case 4". Once you have mastered those, try adding more code and building up to something bigger. Quote Link to comment https://forums.phpfreaks.com/topic/215782-a-switch-inside-a-switch/#findComment-1121938 Share on other sites More sharing options...
salathe Posted October 13, 2010 Share Posted October 13, 2010 Am I overlooking something? The basics of clean and tidy code, perhaps. I don't mean that in any condescending way; it is a valuable skill to learn, to craft code that you (and others) can work with (and that gets the job done). Why not make life easier, for yourself in the long run, if you stand back and refactor your code into something usable? There are repeated elements which don't need to be repeated: why use the same code in multiple places to, for example, connect to the database, to issue a query and fetch the results, and even the HTML snippets are fairly repetitive. It would be much nicer if you could get the [samp]switch[/samp] itself down to a similar size and complexity as that in the first post here. Quote Link to comment https://forums.phpfreaks.com/topic/215782-a-switch-inside-a-switch/#findComment-1121945 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.