PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 19th, 2013, 06:27 AM
westie2506 westie2506 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 11 westie2506 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 3 m 45 sec
Reputation Power: 0
Send a message via MSN to westie2506 Send a message via Skype to westie2506
PHP-General - 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?

Reply With Quote
  #2  
Old February 19th, 2013, 07:22 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,879 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 7 h 20 m 28 sec
Reputation Power: 813
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.

Reply With Quote
  #3  
Old February 19th, 2013, 08:40 AM
westie2506 westie2506 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 11 westie2506 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 3 m 45 sec
Reputation Power: 0
Send a message via MSN to westie2506 Send a message via Skype to westie2506
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:

Reply With Quote
  #4  
Old February 19th, 2013, 11:55 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,879 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 7 h 20 m 28 sec
Reputation Power: 813
*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).

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-General - How the Heck is this not working?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap