using switches

Discussion in 'PHP' started by Dirty-Rockstar, Feb 18, 2007.

  1. #1
    What im trying to do is have a user put in a number from 1-9 into a box. when this happens a color associated with that number will fill in the table.

    I accomplished it with IF statements here

    www.torncitynoobs.com/test2.php

    im trying to get the EXACT situation using switches but get weird results. I cant fix it. :S

    the page is

    www.torncitynoobs.com/test.php

    the source to the original one with the if statements can be found here for reference to see what i changed. its not my thread, but i posted the code there

    http://forums.digitalpoint.com/showthread.php?t=247109

    Thanks in advance you people are awesome!
    ps, colors dont have to match from the old project on test2.php i just need it to work :p

    <?
    $number=$_POST[numberentered];
    $text_before="<b>Pick a number between 1 and 9</b>";
    $text_after="<b>YAY, You picked the number<br>$number</b>";
    $bgcolor='';
    $text='';
    
    
    switch ($bgcolor){
    
    case ($number==0):
    $text=print  "<b>You picked $number<br> Theres no color for $number yet!</b>";
    break;
    case ($number==1):
    $bgcolor="bgcolor='black'";
    break;
    case ($number==2):
    $bgcolor="bgcolor='white'";
    break;
    case ($number==3):
    $bgcolor="bgcolor='yellow'";
    break;
    case ($number==4):
    $bgcolor="bgcolor='blue'";
    break;
    case ($number==5):
    $bgcolor="bgcolor='red'";
    break;
    case ($number==6):
    $bgcolor="bgcolor='orange'";
    break;
    case ($number==7):
    $bgcolor="bgcolor='grey'";
    break;
    case ($number==8):
    $bgcolor="bgcolor='purple'";
    break;
    case ($number==9):
    $bgcolor="bgcolor='green'";
    break;
    
    
    default: $text=print "<b>Pick a number</b>";
    $text_before=$text_after;
    }
    
    
    
    
    
    print " <center><TABLE BORDER=1>
    <TR>
      <TD $bgcolor width='50' height='50' value='value' name='name'>$text</TD>
     
    
    </TABLE>
    $text_before
    <form method='post' action=test.php>
    
    
    <input type='text' name='numberentered' value='$number' maxlength='1'><br>
    <input type='submit' name='submit' value='Click'>
    </form></center>";
    
    
    
    
    ?>
    
    
    PHP:
     
    Dirty-Rockstar, Feb 18, 2007 IP
  2. picouli

    picouli Peon

    Messages:
    760
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You are using the 'switch' in a weird way, that's why you get weird results... ;)

    Here's how you should use it:
    switch ($number) {
    case 0:
      $text=print  "<b>You picked $number<br> Theres no color for $number yet!</b>";
      # I don't know what you are trying do to here but I think you've got it wrong...
      break;
    case 1:
      $bgcolor="bgcolor='black'";
      break;
    . . . .
    default:
      $text=print "<b>Pick a number</b>";
    }
    
    PHP:
    http://php.net/switch

    HTH, cheers! :)
     
    picouli, Feb 18, 2007 IP
  3. Dirty-Rockstar

    Dirty-Rockstar Guest

    Messages:
    252
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3

    Thanks alot, it solved my issue. the output wasnt exactly what i wanted. however since the cases were put in correctly i can configure it the way i want it. thank you
     
    Dirty-Rockstar, Feb 18, 2007 IP