Author Topic: Switch Statement Help  (Read 385 times)

0 Members and 1 Guest are viewing this topic.

Offline SelfObscurityTopic starter

  • Irregular
  • Posts: 47
  • Gender: Male
  • I'm a work-in-progress
    • View Profile
Switch Statement Help
« on: March 18, 2010, 10:32:37 AM »
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?
Jon

2 Cor 12:10 "Therefore I take pleasure in infirmities, in reproaches, in necessities, in persecutions, in distresses for Christ's sake: for when I am weak, then am I strong."

Online Psycho

  • Guru
  • Freak!
  • *
  • Posts: 7,754
    • View Profile
Re: Switch Statement Help
« Reply #1 on: March 18, 2010, 10:57:12 AM »
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.
« Last Edit: March 18, 2010, 10:57:56 AM by mjdamato »
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

Offline SelfObscurityTopic starter

  • Irregular
  • Posts: 47
  • Gender: Male
  • I'm a work-in-progress
    • View Profile
Re: Switch Statement Help
« Reply #2 on: March 18, 2010, 11:37:01 AM »
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.
Jon

2 Cor 12:10 "Therefore I take pleasure in infirmities, in reproaches, in necessities, in persecutions, in distresses for Christ's sake: for when I am weak, then am I strong."