Jump to content

Need help with PHP Switch Case problem

Go to solution Solved by LukeTim,

 

nope still doesn't work dude this is the code


<?php function menu($day, $meal){ switch ($day) {  case 'Saturday':   switch($meal) {     case "B"     {       echo "MENU for Saturday: Breakfast";       break;     }     case "L":     {       echo "MENU for Saturday: Lunch";       break;     }     case "D"     {       echo "MENU for Saturday: Dinner";       break;     }     default:     {           echo "<p  class='Paragraph'>Not a valid MENU for Saturday</p>";           break;        }   }    break;  case 'Sunday':   switch($meal)   {         case "B":     {       echo "MENU for Sunday: Breakfast";       break;     }     case "L":     {       echo "MENU for Sunday: Lunch";       break;     }     case "D":     {       echo "MENU for Sunday: Dinner";       break;     }     default:     {           echo "<p  class='Paragraph'>Not a valid MENU for Saturday</p>";           break;        }   }   break;  default:   echo "No MENU";   break; }}/*end of function*/ // Get data from the form$day = 'Sunday';//$_POST['day'];$meal = 'B';//$_POST['meal'];// Call functionmenu($day, $meal); //These should not be strings anyway?>

 

 

There are some missing colons in the first switch on $meal.

Seriously, what happens when you try to run it? I could see a problem here this time but it's not easy to identify the problem when you don't tell me what php is saying about the script.

need help replacing the If statements from this code with another Case statement not sure how to do a Case within a Case 

 

 <?php function menu($day, $meal){switch ($day){case 'Saturday':if ($meal == "B"){echo "MENU for Saturday: Breakfast";break;}if ($meal == "L"){echo "MENU for Saturday: Lunch";break;}if ($meal == "D"){echo "MENU for Saturday: Dinner";break;}else{ echo "<p  class='Paragraph'>Not a valid MENU for Saturday</p>"; break; }case 'Sunday':if ($meal == "B"){echo "MENU for Sunday: Breakfast";break;}if ($meal == "L"){echo "MENU for Sunday: Lunch";break;}if ($meal == "D"){echo "MENU for Sunday: Dinner";break;}else{ echo "<p  class='Paragraph'>Not a valid MENU for Saturday</p>"; break; }default:echo "No MENU";break;}}/*end of function*/ // Get data from the form$day = $_POST['day'];$meal = $_POST['meal'];// Call functionmenu("$day", "$meal"); ?> 
Link to comment
https://linustechtips.com/topic/191126-need-help-with-php-switch-case-problem/
Share on other sites

Link to post
Share on other sites

---

you can just do it like this

switch($day){	case 'Saturday':		switch($meal){			case 'B':				break;			case 'L':				break;			case 'D':				break;			default:		}		break;	case 'Sunday':		switch($meal){			case 'B':				break;			case 'L':				break;			case 'D':				break;			default:		}		break;	default:}

but, depending on what you're trying to achieve there could be other ways to do this, for instance you can use an associative array somehow like this

$menuArray = array();$menuArray['Saturday'] = array();$menuArray['Saturday']['B'] = 'Menu for Saturday: Breakfast';$menuArray['Saturday']['L'] = 'Menu for Saturday: Lunch';$menuArray['Saturday']['D'] = 'Menu for Saturday: Dinner';$menuArray['Sunday'] = array();$menuArray['Sunday']['B'] = 'Menu for Sunday: Breakfast';$menuArray['Sunday']['L'] = 'Menu for Sunday: Lunch';$menuArray['Sunday']['D'] = 'Menu for Sunday: Dinner';function menu($day, $meal){     if(isset($menuArray[$day]))          if(isset($menuArray[$day][$meal]))               return $menuArray[$day][$meal];     return "no menu";}echo menu($day, $meal);
Link to post
Share on other sites

 

you can just do it like this

switch($day){	case 'Saturday':		switch($meal){			case 'B':				break;			case 'L':				break;			case 'D':				break;			default:		}		break;	case 'Sunday':		switch($meal){			case 'B':				break;			case 'L':				break;			case 'D':				break;			default:		}		break;	default:}

but, depending on what you're trying to achieve there could be other ways to do this, for instance you can use an associative array somehow like this

$menuArray = array();$menuArray['Saturday'] = array();$menuArray['Saturday']['B'] = 'Menu for Saturday: Breakfast';$menuArray['Saturday']['L'] = 'Menu for Saturday: Lunch';$menuArray['Saturday']['D'] = 'Menu for Saturday: Dinner';$menuArray['Sunday'] = array();$menuArray['Sunday']['B'] = 'Menu for Sunday: Breakfast';$menuArray['Sunday']['L'] = 'Menu for Sunday: Lunch';$menuArray['Sunday']['D'] = 'Menu for Sunday: Dinner';function menu($day, $meal){     if(isset($menuArray[$day]))          if(isset($menuArray[$day][$meal]))               return $menuArray[$day][$meal];     return "no menu";}echo menu($day, $meal);

its still has to be a function though 

Link to post
Share on other sites

Why not doing two separate Switch Statements and then combining the strings??

 

$result = "Menu for ".$day.":".$meal;echo $result;
 

Business Management Student @ University St. Gallen (Switzerland)

HomeServer: i7 4930k - GTX 1070ti - ASUS Rampage IV Gene - 32Gb Ram

Laptop: MacBook Pro Retina 15" 2018

Operating Systems (Virtualised using VMware): Windows Pro 10, Cent OS 7

Occupation: Software Engineer

Link to post
Share on other sites

it doesn't: If you don't want it to be in a function, just take the code out of the function

it has to be in a function because the question says that it has to be i would prefer it if you just modified the code that i posed please if that's not to much trouble thanks :)

Link to post
Share on other sites

it has to be in a function because the question says that it has to be i would prefer it if you just modified the code that i posed please if that's not to much trouble thanks :)

the problem is to find a method that works, and once you found it it shouldn't be a problem to take it in or out of a function

if you're not sure how to use functions, then you should take a step back and give a read at the "functions" page of your book before diving into this exercise

first learn how to use the tool, then use it

 

Why not doing two separate Switch Statements and then combining the strings??

i think that every day has a different breakfast, lunch and dinner menu

if that's not the case, then the problem can be solved with just one switch

Link to post
Share on other sites

 

i think that every day has a different breakfast, lunch and dinner menu

if that's not the case, then the problem can be solved with just one switch

Yes, didn't think about that ;)

Business Management Student @ University St. Gallen (Switzerland)

HomeServer: i7 4930k - GTX 1070ti - ASUS Rampage IV Gene - 32Gb Ram

Laptop: MacBook Pro Retina 15" 2018

Operating Systems (Virtualised using VMware): Windows Pro 10, Cent OS 7

Occupation: Software Engineer

Link to post
Share on other sites

its still has to be a function though 

 

Ciccioo's code was in a function...

I think perhaps you need to go back and read up on what a function is again like Ciccioo said. Any code may be either in or out of a function.

I mean, the top-level code in your php script is executed by the runtime as though it was one big function. There is really no difference.

Link to post
Share on other sites

the problem is to find a method that works, and once you found it it shouldn't be a problem to take it in or out of a function

if you're not sure how to use functions, then you should take a step back and give a read at the "functions" page of your book before diving into this exercise

first learn how to use the tool, then use it

 

i think that every day has a different breakfast, lunch and dinner menu

if that's not the case, then the problem can be solved with just one switch

 

our teacher only gave us one page of information on functions and just gave us the code and told us to do the activity  

 

i tried this it didnt work

 

<?php function menu($day, $meal){ switch ($day) {  case 'Saturday':   case "B"   {    echo "MENU for Saturday: Breakfast";       }   case "L"   {    echo "MENU for Saturday: Lunch";       }   case "D"   {    echo "MENU for Saturday: Dinner";       }   else   {    echo "<p  class='Paragraph'>Not a valid MENU for Saturday</p>";    break;   }  case 'Sunday':   case "B"   {    echo "MENU for Sunday: Breakfast";    break;   }   case "L"   {    echo "MENU for Sunday: Lunch";    break;   }   case "D"   {    echo "MENU for Sunday: Dinner";    break;   }   else   {    echo "<p  class='Paragraph'>Not a valid MENU for Saturday</p>";    break;   }  default:   echo "No MENU";   break; }}/*end of function*/// Get data from the form$day = $_POST['day'];$meal = $_POST['meal'];// Call functionmenu("$day", "$meal");?>
Link to post
Share on other sites

 

our teacher only gave us one page of information on functions and just gave us the code and told us to do the activity  

 

i tried this it didnt work

<?php function menu($day, $meal){ switch ($day) {  case 'Saturday':   case "B"   {    echo "MENU for Saturday: Breakfast";       }   case "L"   {    echo "MENU for Saturday: Lunch";       }   case "D"   {    echo "MENU for Saturday: Dinner";       }   else   {    echo "<p  class='Paragraph'>Not a valid MENU for Saturday</p>";    break;   }  case 'Sunday':   case "B"   {    echo "MENU for Sunday: Breakfast";    break;   }   case "L"   {    echo "MENU for Sunday: Lunch";    break;   }   case "D"   {    echo "MENU for Sunday: Dinner";    break;   }   else   {    echo "<p  class='Paragraph'>Not a valid MENU for Saturday</p>";    break;   }  default:   echo "No MENU";   break; }}/*end of function*/// Get data from the form$day = $_POST['day'];$meal = $_POST['meal'];// Call functionmenu("$day", "$meal");?>

 

You're switching on the day but not switching on the meal... You need nested switches (meaning switches inside a switch), here:

<?php function menu($day, $meal){ switch ($day) {  case 'Saturday':   switch($meal) {     case "B"     {       echo "MENU for Saturday: Breakfast";       break;     }     case "L":     {       echo "MENU for Saturday: Lunch";       break;     }     case "D"     {       echo "MENU for Saturday: Dinner";       break;     }     default:     {           echo "<p  class='Paragraph'>Not a valid MENU for Saturday</p>";           break;        }   }    break;  case 'Sunday':   switch($meal)   {         case "B":     {       echo "MENU for Sunday: Breakfast";       break;     }     case "L":     {       echo "MENU for Sunday: Lunch";       break;     }     case "D":     {       echo "MENU for Sunday: Dinner";       break;     }     default:     {           echo "<p  class='Paragraph'>Not a valid MENU for Saturday</p>";           break;        }   }   break;  default:   echo "No MENU";   break; }}/*end of function*/// Get data from the form$day = $_POST['day'];$meal = $_POST['meal'];// Call functionmenu("$day", "$meal");?>

Still, Ciccioo's dictionary solution is far simpler, more readable and just plain nicer. I would recommend going with it unless your teacher insisted you use a switch (though it looks like s/he hasn't even covered switch statements yet - apologies if you have covered it but that's what it seems like by the mistakes you're making).

Link to post
Share on other sites

You're switching on the day but not switching on the meal... You need nested switches (meaning switches inside a switch), here:

<?php function menu($day, $meal){ switch ($day) {  case 'Saturday':   switch($meal) {     case "B"     {       echo "MENU for Saturday: Breakfast";       break;     }     case "L":     {       echo "MENU for Saturday: Lunch";       break;     }     case "D"     {       echo "MENU for Saturday: Dinner";       break;     }     default:     {           echo "<p  class='Paragraph'>Not a valid MENU for Saturday</p>";           break;        }   }    break;  case 'Sunday':   switch($meal)   {         case "B":     {       echo "MENU for Sunday: Breakfast";       break;     }     case "L":     {       echo "MENU for Sunday: Lunch";       break;     }     case "D":     {       echo "MENU for Sunday: Dinner";       break;     }     default:     {           echo "<p  class='Paragraph'>Not a valid MENU for Saturday</p>";           break;        }   }   break;  default:   echo "No MENU";   break; }}/*end of function*/// Get data from the form$day = $_POST['day'];$meal = $_POST['meal'];// Call functionmenu("$day", "$meal");?>

Still, Ciccioo's dictionary solution is far simpler, more readable and just plain nicer. I would recommend going with it unless your teacher insisted you use a switch (though it looks like s/he hasn't even covered switch statements yet - apologies if you have covered it but that's what it seems like by the mistakes you're making).

yeah i have a pretty crappy teacher for my software design class dude i copied and passed your code into notepad++ it doesn't work i cant see anything wrong with it although its pretty clear that I'm a amateur 

Link to post
Share on other sites

yeah i have a pretty crappy teacher for my software design class dude i copied and passed your code into notepad++ it doesn't work i cant see anything wrong with it although its pretty clear that I'm a amateur 

 

Just to test it out, change those last few lines... just for the sake of testing.

// Get data from the form$day = 'Sunday';//$_POST['day'];$meal = 'B';//$_POST['meal'];// Call functionmenu($day, $meal); //These should not be strings anyway

Also, when you say it doesn't work it would be helpful if you let us know in what way it didn't work. What output did you get. Did you just get unexpected output (known as a semantic error)? Or did it not execute at all and you got a bunch of error messages (known as a syntactic error)?

Link to post
Share on other sites

Just to test it out, change those last few lines... just for the sake of testing.

// Get data from the form$day = 'Sunday';//$_POST['day'];$meal = 'B';//$_POST['meal'];// Call functionmenu($day, $meal); //These should not be strings anyway

Also, when you say it doesn't work it would be helpful if you let us know in what way it didn't work. What output did you get. Did you just get unexpected output (known as a semantic error)? Or did it not execute at all and you got a bunch of error messages (known as a syntactic error)?

nope still doesn't work dude this is the code


<?php function menu($day, $meal){ switch ($day) {  case 'Saturday':   switch($meal) {     case "B"     {       echo "MENU for Saturday: Breakfast";       break;     }     case "L":     {       echo "MENU for Saturday: Lunch";       break;     }     case "D"     {       echo "MENU for Saturday: Dinner";       break;     }     default:     {           echo "<p  class='Paragraph'>Not a valid MENU for Saturday</p>";           break;        }   }    break;  case 'Sunday':   switch($meal)   {         case "B":     {       echo "MENU for Sunday: Breakfast";       break;     }     case "L":     {       echo "MENU for Sunday: Lunch";       break;     }     case "D":     {       echo "MENU for Sunday: Dinner";       break;     }     default:     {           echo "<p  class='Paragraph'>Not a valid MENU for Saturday</p>";           break;        }   }   break;  default:   echo "No MENU";   break; }}/*end of function*/ // Get data from the form$day = 'Sunday';//$_POST['day'];$meal = 'B';//$_POST['meal'];// Call functionmenu($day, $meal); //These should not be strings anyway?>
Link to post
Share on other sites

 

nope still doesn't work dude this is the code


<?php function menu($day, $meal){ switch ($day) {  case 'Saturday':   switch($meal) {     case "B"     {       echo "MENU for Saturday: Breakfast";       break;     }     case "L":     {       echo "MENU for Saturday: Lunch";       break;     }     case "D"     {       echo "MENU for Saturday: Dinner";       break;     }     default:     {           echo "<p  class='Paragraph'>Not a valid MENU for Saturday</p>";           break;        }   }    break;  case 'Sunday':   switch($meal)   {         case "B":     {       echo "MENU for Sunday: Breakfast";       break;     }     case "L":     {       echo "MENU for Sunday: Lunch";       break;     }     case "D":     {       echo "MENU for Sunday: Dinner";       break;     }     default:     {           echo "<p  class='Paragraph'>Not a valid MENU for Saturday</p>";           break;        }   }   break;  default:   echo "No MENU";   break; }}/*end of function*/ // Get data from the form$day = 'Sunday';//$_POST['day'];$meal = 'B';//$_POST['meal'];// Call functionmenu($day, $meal); //These should not be strings anyway?>

 

 

There are some missing colons in the first switch on $meal.

Seriously, what happens when you try to run it? I could see a problem here this time but it's not easy to identify the problem when you don't tell me what php is saying about the script.

Link to post
Share on other sites

There are some missing colons in the first switch on $meal.

Seriously, what happens when you try to run it? I could see a problem here this time but it's not easy to identify the problem when you don't tell me what php is saying about the script.

im using google chrome it just comes up with a blank page and on internet explorer it comes up internet explorer cannot display this page

Link to post
Share on other sites

im using google chrome it just comes up with a blank page and on internet explorer it comes up internet explorer cannot display this page

 

I see. Sounds like you need to enable debugging in PHP. You'll need to edit the ini file. That will give you some more useful output.

If it is just coming out with a blank screen that suggests that php cannot compile/execute the script and so gives up, and then because debugging is not enabled it doesn't output any errors it just stops.

Link to post
Share on other sites

There are some missing colons in the first switch on $meal.

Seriously, what happens when you try to run it? I could see a problem here this time but it's not easy to identify the problem when you don't tell me what php is saying about the script.

it runs now with the colans but it only displays 'MENU for Sunday: Breakfast' no matter what the fields are 

Link to post
Share on other sites

it runs now with the colans but it only displays 'MENU for Sunday: Breakfast' no matter what the fields are 

 

That's because you put some test data in. Remember? I suggested you do that to eliminate the possiblity that $_POST was nonexistent or something. Put it back the way it was and it should work.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×