Hi, all
First off, it's a SOLVED problem which a) still keeps me thinking, but b) requires a question for the future. I'm actually still learning (by doing) PHP so maybe there's a simple reason for this case, in which case - sorry for bothering
I'm writing a very simple script that displays 1 of 4 possible events depending on a number in a (MySQL) database. It's really basic.
$ttest = $usrfield['ttest'];
switch ($ttest) {
case ($ttest < 50):
$ttestr = "<div>Get at least 50 credits</div>";
break;
case ($ttest > 49 && $ttest < 500):
$ttestr = "<div><b>RANK 1</b></div><div style='color: #d00000; font-size: 10px;'>You have <b>".$ttest."</b> credits</div>";
break;
case ($ttest >= 500 && $ttest < 5000):
$ttestr = "<div><b>RANK 2</b></div><div style='color: #d00000; font-size: 10px;'>You have <b>".$ttest."</b> credits</div>";
break;
case ($ttest >= 5000):
$ttestr = "<div><b>RANK 3</b></div><div style='color: #d00000; font-size: 10px;'>You have <b>".$ttest."</b> credits</div>";
break;
}
As I said - very basic. There are 15 (almost) EXACT codes, varying by the variable name (for example: $rtest + $rtestr, $ptest + $ptestr and so on) which make a document included in the "main" file which echoes all of them $ptestr, $rtestr, $ttestr etc.
The main document also sets
$usrfield = mysql_fetch_assoc($resultusr);
and $resultusr is a mysql_query selecting a particular user's row with ptest, rtest, ttest... named columns.
Here's the funny thing - it works perfectly in 13 out of 15 cases. In 2 cases, where the number is 0 it doesn't display "Get at least 50 credits", but returns "RANK 1..." instead (with 0 within the text). And it's not like these are the only two zeros - there are a 4 more and all 4 work fine and return "Get at least 50 credits".
I've been staring at the complete code half an hour, copying/pasting the working part that displays 0 properly and mass-replace "rtest" to "ttest" (for example) and still nothing. I've tried to check/repair the particular MySQL table - PhpMyAdmin says it's OK. All fields have same properties, all are 0-default, decimal(6,0). I've tried to make a separate case for $ttest equaling 0 and one for it being less than 50 and then 0 worked, but... nothing else, still returned 0-case even with 45671.
Then I've separated the first case and made an if (case1) else (switch):
$ttest = $usrfield['ttest'];
if ($ttest < 50) {
$ttestr = "<div>Get at least 50 credits</div>";
}
else {
switch ($ttest) {
case ($ttest > 49 && $ttest < 500):
$ttestr = "<div><b>RANK 1</b></div><div style='color: #d00000; font-size: 10px;'>You have <b>".$ttest."</b> credits</div>";
break;
case ($ttest >= 500 && $ttest < 5000):
$ttestr = "<div><b>RANK 2</b></div><div style='color: #d00000; font-size: 10px;'>You have <b>".$ttest."</b> credits</div>";
break;
case ($ttest >= 5000):
$ttestr = "<div><b>RANK 3</b></div><div style='color: #d00000; font-size: 10px;'>You have <b>".$ttest."</b> credits</div>";
break;
}
}
and surprise, surprise - it works
So here's the question - is there something special about the 0 or about switch that it doesn't always work or only with particular numbers ( )? Should I always mix if/else with switch in some particular situations? Or maybe ditch the switch completely and make it an if/elseif for... whatever reason?