0

Hi,

I am trying to print different messages based on what is selected. The selections are stored in a database as $perm, and the options are low, intermediate and high. I am using switch, however i only get the first case printed:

$low = "Low";
$int = "Intermediate"; 
$high = "High "; 

switch($perm) {
     case $perm == $low: echo "Low perm <br>"; break;
     case $perm == $int: echo "Intermediate perm <br>"; break;
     case $perm == $high: echo "High perm <br>"; break;
     default: echo "No information has been provided. ";
}

I have tried giving the options numerical values, however, when i retrieve the results they are still strings so comparint $perm == 1, 2, or 3 just prints the default message. Any help appreciated.

3
Contributors
8
Replies
9
Views
7 Years
Discussion Span
Last Post by ayooshkasmth
0
$low = "Low";
$int = "Intermediate"; 
$high = "High "; 

switch($perm) {
     case $low: echo "Low perm <br>"; break;
     case $int: echo "Intermediate perm <br>"; break;
     case $high: echo "High perm <br>"; break;
     default: echo "No information has been provided. ";
}

Edited by edwinhermann: Forgot the [code] tags

0

Thank you. I have tried it and it is still only printing the default. The database entry for $perm is Low.

0

Hi,

I am trying to print different messages based on what is selected. The selections are stored in a database as $perm, and the options are low, intermediate and high. I am using switch, however i only get the first case printed:

$low = "Low";
$int = "Intermediate"; 
$high = "High "; 

switch($perm) {
     case $perm == $low: echo "Low perm <br>"; break;
     case $perm == $int: echo "Intermediate perm <br>"; break;
     case $perm == $high: echo "High perm <br>"; break;
     default: echo "No information has been provided. ";
}

I have tried giving the options numerical values, however, when i retrieve the results they are still strings so comparint $perm == 1, 2, or 3 just prints the default message. Any help appreciated.

switch($perm) {
     case 'Low': 
         echo "Low perm <br>"; 
     break;
     case 'Intermediate': 
         echo "Intermediate perm <br>"; 
     break;
     case 'High': 
         echo "High perm <br>"; 
     break;
     default: 
         echo "No information has been provided. ";
}
0

can you put var_dump($perm); in your code prior to the case statement and post the output here? I'm going to guess either perm is not being set or is not what you expect it to be.

0

You are right - this got me:

$row = mysql_fetch_array( $result );

Looped it, and it works great as it should :)

while ($row = mysql_fetch_array($result)) {
extract($row);	}

Thank you so much.

PS: How do i assign numeric values to the strings and then compare them using switch?

E.g low, med, high = 1, 2, 3 where $a, $b, $c could be either low, med, high? and both the low, med, high and $a, b,c etc are sql result queries? So something like:

'Low' = 1;
'Med' = 2;
'High' = 3;

switch ($x) {
     case $a == $b : echo "<br> Equal <br>";  break;
     case $b < $c: echo "<br> Less <br>"; break;
     case $a < $c: echo "<br> Less <br>"; break;
     default: echo "<br> No selection "; }

I've been on this so long im not even thing straight anymore :(

0

Thank you. I have tried it and it is still only printing the default. The database entry for $perm is Low.

I think it must be to do with $perm not being set properly, because before I posted my solution I tested it first by adding $perm = "Intermediate"; at the beginning.

(By the way, every time I see the variable $perm I think it says "sperm" :-))

Edited by edwinhermann: n/a

0

Solved - thanks everyone.

edwinhermann: haha, wouldn't have noticed that if my life depended on it :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.