#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0

    How the Heck is this not working?


    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 :
    PHP Code:
    $oa1['slots'
    . 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?
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    in PHP, neither is 0 > 5 nor '0' > '5'. So obviously there's something else going on.

    Make a var_dump() of each value and show the result here.

    You should also consider normalizing your database and getting rid of this weird "a:b:c" stuff.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0
    Here's the error-checked output and var dumps

    ================= Using PRINT command ================
    Slots value: 0
    First Threshold: 5
    Second Threshold: 3
    Third Threshold: 0
    ================== VAR_DUMP Values ===================
    dump of slots int(0)
    dump of thresholds array(3) { [0]=> string(1) "5" [1]=> string(1) "3" [2]=> string(1) "0" }
    Result: Should say WAPW_OA_BOOKED_TEXT string(75) "WAPW_OA_AVAILABLE_TEXT"

    First thing I noticed was that the thresholds are strings and the slots value is an integer. Wonder if it's worthwhile converting the thresholds to int values too?

    As for the database data - I can't change that format as it has to interface with a different system in that format.
    Last edited by westie2506; February 19th, 2013 at 08:41 AM. Reason: updated:
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    *lol*

    You have put conditions instead of values into your switch statement. That's not how it works. The first condition expression evaluates to false, so you compare 0 with false. And since this is true, you end up with the first case being executed.

    So you simply didn't use the switch correctly. To be honest, I didn't notice this at first either.

    Just replace that stuff with an if statement, and you're done. You should generaly avoid using switch, because it's cumbersome, difficult to read and error-prone (thanks to the fall through mechanism).
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo