Switch problem?!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Matt Phelps
Forum Commoner
Posts: 82
Joined: Fri Jun 14, 2002 2:05 pm

Switch problem?!

Post by Matt Phelps »

The following code doesn't work - and I don't know why?! The switch should check through and find out which types of car exist and if they do exist then assign a variable the value 'Checked'. The switch works except for the first case. $chassis_rules does not contain the work 'Ferrari' and yet when the switch looks through for the word it mysteriously finds it and assigns the variable $chassis1 the value of 'Checked' - which it should not do. In this case the value of $chassis1 should be NULL.

Can anyone work out why this isn't working?

Code: Select all

<?php
$chassis_rules = "Eagle, Lotus, Honda, Cooper, BRM, Brabham"; 

		$chassis_elements = explode(", ",$chassis_rules);
		while ($num<'7')
		{
		$chassis_type = "$chassis_elementsї$num]";
		switch ($chassis_type){
				case $chassis_type == "Ferrari":
				$chassis1 = "Checked";
				break;
				case $chassis_type == "Eagle":
				$chassis2 = "Checked";
				break;
				case $chassis_type == "Lotus":
				$chassis3 = "Checked";
				break;
				case $chassis_type == "Honda":
				$chassis4 = "Checked";
				break;
				case $chassis_type == "Cooper":
				$chassis5 = "Checked";
				break;
				case $chassis_type == "BRM":
				$chassis6 = "Checked";
				break;
				case $chassis_type == "Brabham":
				$chassis7 = "Checked";
				break;
				}
		$num++;
		}

		echo "$chassis1";
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try this:

Code: Select all

<?php 
$chassis_rules = "Eagle, Lotus, Honda, Cooper, BRM, Brabham";  

$chassis_elements = explode(', ', $chassis_rules);

/* Get the size of the array so that the for loop can be told how long to run for */
$num_elements = sizeof($chassis_elements);

/* Using a for loop instead of an incrementing while loop brings things together a bit better */
for ($i=0; $i < $num_elements; ++$i) { 
	switch ($chassis_elementsї$i]) { 
		case 'Ferrari': 
			$chassis1 = 'Checked';
			break; 
		case 'Eagle': 
			$chassis2 = 'Checked'; 
			break; 
		case 'Lotus': 
			$chassis3 = 'Checked'; 
			break; 
		case 'Honda': 
			$chassis4 = 'Checked'; 
			break; 
		case 'Cooper':
			$chassis5 = 'Checked'; 
			break; 
		case 'BRM': 
			$chassis6 = 'Checked'; 
			break; 
		case 'Brabham': 
			$chassis7 = 'Checked'; 
			break; 
	} 
} 
?>
I think that the problem was down to the while ($num < '7') statement since (in the code you posted at least) $num had no initial value and thus it was all going a bit screwy.

I edited the code because I blatantly wasn't paying as much attention as I should have first time round.

Mac
Last edited by twigletmac on Tue Sep 17, 2002 7:55 am, edited 1 time in total.
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post by dusty »

Code: Select all

&lt;?
// no need for the list of possibles and then explode to make an array, just make them an array from the start
$chassis_type = array("Eagle","Lotus","Honda","Cooper","BRM","Brabham");
// get the number of elements in an array
$total = count($chassis_type);

// keep all of your expressions in 1 statement using for instead of while!
for($num=0; $num &lt; $total; $num++) {
  // the switch var is taking the current $num from the loop and selecting that key from the chassis_type array.
  switch($chassis_type&#1111;$num]) {
    // no need for the $var == "" in the case. or you'd might as well be using if statements.
    case "Ferrari":
      $chassis1 = "Checked";
      break;
    case "Eagle":
      $chassis2 = "Checked";
      break;
    case "Lotus":
      $chassis3 = "Checked";
      break;
    case "Honda":
      $chassis4 = "Checked";
      break;
    case "Cooper":
      $chassis5 = "Checked";
      break;
    case "BRM":
      $chassis6 = "Checked";
      break;
    case "Brabham":
      $chassis7 = "Checked";
      break;
  }
}
?&gt;
Last edited by dusty on Tue Sep 17, 2002 8:03 am, edited 1 time in total.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Not exactly much difference between that and what I posted :roll:.

(FYI: I've edited my code now so the following comments are related to it's original incarnation)
You see sometimes it's helpful to post a bit of information on what makes your code different, for example, since I was paying more attention to the problem at hand I forgot that the '$chassis_type == ' bits in the case statements were unneccessary and didn't pick up on the completely pointless action of setting $chassis_elements[$i] to another variable.

It might be better for the person seeking help if they get even just a couple of sentences attached to a block of posted code to explain why that code is better or whatever than that posted by someone else. What may seem completely obvious to you may not be to someone else.

That said I think I may edit my first post to take into account these things and to add a bit more information :) (done that now).

Mac
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post by dusty »

I probably wouldn't have even posted mine if i had noticed your post first.. between eating cereal and watching cartoons it took me awhile to get to finishing it.

yes matt, twigletmac's code works fine. my post is another solution that shows you can cut down on the code a bit.
Matt Phelps
Forum Commoner
Posts: 82
Joined: Fri Jun 14, 2002 2:05 pm

Post by Matt Phelps »

Thanks all - you solved the problem. I'm not sure why my own code didn't work out because I felt that the logic was sound but there you go!

I went with twigletmac's code in the end because the $chassis_rules variable is taken from a database and is already in that imploded format.
Post Reply