Firstly, I'm not a new coder nor am I new to PhP, but for some reason my latest section of code seems to have some weird error that I can't work out.
I have value read in from a database of 3 values separated by colons - for example 5:3:0. The format is always the same, and it always goes from high to low, with the highest possible value being 8 and the lowest possible at 0.
From a db table, I pull a numeric value out of the 'slots' field and assign it to a variable name called $oa1 :
. The field is a TINYINT type in the database
I then use the three values as thresholds to trigger a message on the screen. The code is below.
PHP Code:
$thresholds = explode(":",WAPW_OA_THRESHOLDS);
switch ($oa1['slots']) {
case ($oa1['slots'] > $thresholds[0]):
// Full Availability
$m1text = '<span style="color:'.WAPW_OA_AVAILABLE_COLOUR.';">'.WAPW_OA_AVAILABLE_TEXT.'</span>';
break;
case ($oa1['slots'] <= $thresholds[0] && $oa1['slots'] > $thresholds[1] ):
//Limited Availability
$m1text='<span style="color:'.WAPW_OA_LTD_COLOUR.';">'.WAPW_OA_LIMITED_TEXT.'</span>';
break;
case ($oa1['slots'] <= $thresholds[1] && $oa1['slots'] > $thresholds[2] ):
// Slots Available
$text1 = explode(":", WAPW_OA_SLOTS_TEXT);
if ($oa1['slots']==1) {
$text1[1] = str_replace("slots", "slot", $text1[1]);
}
$m1text = '<span style="color:'.WAPW_OA_SLOTS_COLOUR.';">'.$text1[0].$oa1['slots'].$text1[1].'</span>';
break;
case ($oa1['slots'] <= $thresholds[2]):
// No Availability
$m1text='<span style="color:'.WAPW_OA_BOOKED_COLOUR.';">'.WAPW_OA_BOOKED_TEXT.'</span>';
break;
}
What this does, is to produce 1 of 4 results.
greater than first threshold - Available
Less than or equal to the first & greater than the second - Limited
Less than or equal to the second & greater than the third - xx slots remaining
equal to or less than the third - fully booked.
The problem is that no matter which order the case statements are in, setting 0 slots always marks it as available, that is : greater than the first threshold. In this case, it reads 0 slots free as being greater than 5.
Every other number falls neatly into the sections that it needs to, and if I set the last threshold and the free slots to 1, it manages to display the fully booked status.
I've added some error trapping to this (not shown here) and the values are being exploded correctly and it isn't using a default value - it really is assuming that 0 is greater than 5. Any ideas on what I've done wrong?