Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/195687-switch-statement-help/
Share on other sites

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.