This topic is locked
[SOLVED]

 Case command not working with Number values

10/5/2011 7:12:06 AM
PHPRunner General questions
C
crhys author

Hi Guys,
I have a line of code that will not work with numeric values but does with Alpha characters, here is my example
Does work :-
case 'None';

$strUpdate = "update Profile set Collision Score=2 where UserName='".$values["UserName"]."'";
Does Not work :-
case 0;

$strUpdate = "update Profile set Collision Score=2 where UserName='".$values["UserName"]."'";
I did change the data in the record from Number "0" to text "None" in the database while testing, The table field is VARCHAR, not sure if this makes any difference.
Any help is appreciated

C
cgphp 10/5/2011

You need quotes around the 0:

case '0';

$strUpdate = "update Profile set `Collision Score`=2 where UserName='".$values["UserName"]."'";



if the field is VARCHAR

C
crhys author 10/5/2011

Hi Cristian,
Thanks for the reply, I should have thought of that because its a VARCHAR field, however the fix has brought up another little issue.
The code below should change the value of 'Licence Score' to 2 when the "LicPoints" is 0, but it gives me the value of 4, changing the "LicPoints" value to 3, 5 or any other value gives me the expected results.
Strange that it only gives me unexpected results when the value is 0, any ideas appreciated <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=61225&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' />
CODE:-
if ($values["LicPoints"])

{

switch ($values["LicPoints"]) {

case '0';

$strUpdate = "update Profile set Licence Score=2 where UserName='".$values["UserName"]."'";

break;

case '3';

$strUpdate = "update Profile set Licence Score=4 where UserName='".$values["UserName"]."'";

break;

case '5';

$strUpdate = "update Profile set Licence Score=4 where UserName='".$values["UserName"]."'";

break;

default;

$strUpdate = "update Profile set Licence Score=7 where UserName='".$values["UserName"]."'";

break;

}

CustomQuery($strUpdate);

}

C
cgphp 10/5/2011

Is LicPoints a varchar ?

Try to echo $values["LicPoints"] before the switch statement. What you see ?

C
crhys author 10/5/2011

Hi,
When I add echo $values["LicPoints"]; before the switch statement, the number that I put in the box displays top left after saving the record.
eg.. 3 (in the top left corner of the screen
Ahhhhh but if I choose the number 0 then nothing appears in the top left.
Getting somewhere now
PS. Off topic, dont the developers mind you offering support from 15USD, if they are ok with this I will be happy to request help from you on a couple of projects I have running.

C
crhys author 10/5/2011

Missed this info
LicPoints is and INT

C
cgphp 10/5/2011

If LicPoint is a INT, remove the quotes around the case values:

if ($values["LicPoints"])

{

switch ($values["LicPoints"]) {

case 0;

$strUpdate = "update Profile set `Licence Score`=2 where UserName='".$values["UserName"]."'";

break;

case 3;

$strUpdate = "update Profile set `Licence Score`=4 where UserName='".$values["UserName"]."'";

break;

case 5;

$strUpdate = "update Profile set `Licence Score`=4 where UserName='".$values["UserName"]."'";

break;

default;

$strUpdate = "update Profile set `Licence Score`=7 where UserName='".$values["UserName"]."'";

break;

}

CustomQuery($strUpdate);

}
C
crhys author 10/5/2011

Thats what I started off with - lol
case 0;

$strUpdate = "update Profile set Licence Score=2 where UserName='".$values["UserName"]."'";

break;
This variation gives me no echo at all, where if the value was anything but 0 it will echo that number using the echo $values["LicPoints"]; command
hmmmm why is the code treating the numberic value 0 differently to all the other values....

C
cgphp 10/5/2011

At the end of the case statement place a colon not a semicolon:

case 0:
C
crhys author 10/5/2011

adding a colon made no difference - case 0: replacing case0;
No echo with the numberic value 0 and the pofile table didnt get any value changes.

Sergey Kornilov admin 10/5/2011

All case 0: lines should end with colons (semicolons are incorrect there). The same applies to default:

C
crhys author 10/5/2011

Thats interesting info, done the changes to the case & default commands but I still get the same issue, the code works great for any number except for 0
CODE:-
if ($values["LicPoints"])

{

echo $values["LicPoints"];

switch ($values["LicPoints"]) {

case 0:

$strUpdate = "update Profile set Licence Score=2 where UserName='".$values["UserName"]."'";

break;

case 1:

$strUpdate = "update Profile set Licence Score=2 where UserName='".$values["UserName"]."'";

break;

case 2:

$strUpdate = "update Profile set Licence Score=2 where UserName='".$values["UserName"]."'";

break;

case 3:

$strUpdate = "update Profile set Licence Score=2 where UserName='".$values["UserName"]."'";

break;

case 4:

$strUpdate = "update Profile set Licence Score=4 where UserName='".$values["UserName"]."'";

break;

case 5:

$strUpdate = "update Profile set Licence Score=4 where UserName='".$values["UserName"]."'";

break;

case 6:

$strUpdate = "update Profile set Licence Score=4 where UserName='".$values["UserName"]."'";

break;

case 7:

$strUpdate = "update Profile set Licence Score=7 where UserName='".$values["UserName"]."'";

break;

case 8:

$strUpdate = "update Profile set Licence Score=7 where UserName='".$values["UserName"]."'";

break;

case 9:

$strUpdate = "update Profile set Licence Score=7 where UserName='".$values["UserName"]."'";

break;

case 10:

$strUpdate = "update Profile set Licence Score=7 where UserName='".$values["UserName"]."'";

break;

case 11:

$strUpdate = "update Profile set Licence Score=7 where UserName='".$values["UserName"]."'";

break;

default:

$strUpdate = "update Profile set Licence Score=7 where UserName='".$values["UserName"]."'";

break;

}

CustomQuery($strUpdate);

}

C
cgphp 10/5/2011

Try out this code:

$test = 0;

switch ($test) {

case 0:

$strUpdate = "update Profile set `Licence Score`=2 where UserName='".$values["UserName"]."'";

break;

case 1:

$strUpdate = "update Profile set `Licence Score`=2 where UserName='".$values["UserName"]."'";

break;

case 2:

$strUpdate = "update Profile set `Licence Score`=2 where UserName='".$values["UserName"]."'";

break;

case 3:

$strUpdate = "update Profile set `Licence Score`=2 where UserName='".$values["UserName"]."'";

break;

case 4:

$strUpdate = "update Profile set `Licence Score`=4 where UserName='".$values["UserName"]."'";

break;

case 5:

$strUpdate = "update Profile set `Licence Score`=4 where UserName='".$values["UserName"]."'";

break;

case 6:

$strUpdate = "update Profile set `Licence Score`=4 where UserName='".$values["UserName"]."'";

break;

case 7:

$strUpdate = "update Profile set `Licence Score`=7 where UserName='".$values["UserName"]."'";

break;

case 8:

$strUpdate = "update Profile set `Licence Score`=7 where UserName='".$values["UserName"]."'";

break;

case 9:

$strUpdate = "update Profile set `Licence Score`=7 where UserName='".$values["UserName"]."'";

break;

case 10:

$strUpdate = "update Profile set `Licence Score`=7 where UserName='".$values["UserName"]."'";

break;

case 11:

$strUpdate = "update Profile set `Licence Score`=7 where UserName='".$values["UserName"]."'";

break;

default:

$strUpdate = "update Profile set `Licence Score`=7 where UserName='".$values["UserName"]."'";

break;

}

CustomQuery($strUpdate);
C
crhys author 10/5/2011

Yep
If I add your code above my original code the system works, but If I remove my code then it dosent.
Any chance of combining the new code to the old so its not so bulky.