SelfObscurity Posted March 18, 2010 Share Posted March 18, 2010 Good Morning: I'm working on a few switch statements, and can't find any examples or assistance for what I am doing. I am using switch statements to specify URL variables (e.g. a switch to show case calendar for the URL index.php?page=calendar). This is working fine, but where I come into my problem, is when I need to specify 2 variables (e.g. to specify page=calendar, but id=# also). Below is the current code I have. <?php switch ($_GET['page']) { case about: echo ("Display coding for about page"); break; case roster: echo ("Display coding for team roster"); break; case games: echo ("Display coding for games"); break; default: echo ("Display coding for main page"); } ?> What I need to do, is for roster and games, I need to have an additional variable specified which is $_GET['id']...I tried this: <?php switch ($_GET['page']) { case about: echo ("Display coding for about page"); break; case roster: $id = $_GET['id']; switch ($_GET['page'] = 'roster' && $_GET['id']) { case $id: echo ("Display info specific to ID"); break; default; echo ("Display main team page"); } case games: echo ("Display coding for games"); break; default: echo ("Display coding for main page"); } ?> When I do this and go to index.php?page=roster, it says "Display info specific to ID", instead of the main team page. Can someone help me get this fixed? Quote Link to comment https://forums.phpfreaks.com/topic/195687-switch-statement-help/ Share on other sites More sharing options...
Psycho Posted March 18, 2010 Share Posted March 18, 2010 Well, that inner switch statement doesn't look right. Specifically the switch "value" $id = $_GET['id']; switch ($_GET['page'] = 'roster' && $_GET['id']) { case $id: echo ("Display info specific to ID"); break; default; echo ("Display main team page"); } The switch value is "($_GET['page'] = 'roster' && $_GET['id'])". So, whatever the result is of that will be compared against the switch cases. I'm not even sure what that "value" would return. You have an assignment and then and AND statement of another value $_GET['id']. The first part will return true and the second part will return the value of $_GET['id']. So, I think the result is just the value of $_GET['id']. Since, your first case value is $id (which you previously set to $_GET['id']), that will always be the result. You need to rethink your logic. Not really sure what you are trying to do there. I thin an IF/ELSE might be a better option. Quote Link to comment https://forums.phpfreaks.com/topic/195687-switch-statement-help/#findComment-1028122 Share on other sites More sharing options...
SelfObscurity Posted March 18, 2010 Author Share Posted March 18, 2010 I've done what I am trying to do in the past using if statements, but I was told the switches are easier and cleaner to use. I just can't figure out how to use them to pull 2 different $_GET in the cases. Quote Link to comment https://forums.phpfreaks.com/topic/195687-switch-statement-help/#findComment-1028146 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.