Page 2 of 2
Posted: Tue Apr 05, 2005 6:57 am
by rsmarsha
Like this?
Code: Select all
switch ($_GET['Status']) {
case A:
$newstatus = 'Finance Accepted';
break;
case D:
$newstatus = 'Finance Declined';
$reason = 'Finance Declined';
break;
case R:
$newstatus = 'Finance Referred';
break;
case S:
$newstatus = 'Agreement Received';
break;
case C:
$newstatus = 'Cancelled';
$reason = 'Finance Cancelled';
break;
}
Posted: Tue Apr 05, 2005 7:27 am
by CoderGoblin
Almost..
Code: Select all
switch (strtoupper($_GETї'Status'])) {
case "e;A"e;:
$newstatus = 'Finance Accepted';
break;
case "e;R"e;:
$newstatus = 'Finance Referred';
break;
case "e;S"e;:
$newstatus = 'Agreement Received';
break;
case "e;C"e;:
$newstatus = 'Cancelled';
$reason = 'Finance Cancelled';
break;
case "e;D"e;:
$newstatus = 'Finance Declined';
$reason = 'Finance Declined';
break;
default:
$newstatus = 'Finance Declined';
$reason = 'Illegal System Option detected';
break;
}
changes made include Uppercasing the input before checking (defensive programming).
Adding quotes
Adding default - If the option does not exist in the list what to do...
Posted: Tue Apr 05, 2005 8:01 am
by rsmarsha
Thanks
Why do you suggest uppercasing?
Posted: Tue Apr 05, 2005 9:33 am
by CoderGoblin
It is, in reality, only a defensive check to make your code more robust. I assume the GET value is from outside (not necessarily under your control). Rather than having to code 'if ("A" or "a")' it is easier to just uppercase the initial value. Your comparisons then only have do deal with one check. If someone elses code/html passes parameters into yours, your code will not fail if they accidently pass in the value lowercase and will still work as they expected. This is known as defensive programming. The idea is that the code works as expected and "nicely" even with incorrect data being passed into it. That is also the reason for the default value in the switch. If an illegal value is passed you "fail" gracefully rather than produce incorrect data. I made an assumption about which status would be the default, but highlight the error.
Posted: Wed Apr 06, 2005 5:44 am
by rsmarsha
Ok, thanks for the tip.
One more thing was the mysql_result, i've not used that before, your query works a bit different to how i set mine up. Is one better than the other?
Posted: Wed Apr 06, 2005 6:08 am
by CoderGoblin
When using databases it is better (quicker) to get only the information you need. Using "SELECT *" often returns more information than we need and produces extra processing (a.k.a uses additional time) to return the result. That is why I use count(status) rather than count(*) within the SQL statement.
As we only return 1 row and 1 field there is no point in getting the complete row and then finding the field (the number of rows is always going to be 1). Because of this, mysql_result is a better command to use (IMO). When you are returning more than 1 row you should always use mysql_fetch_array (or my preference mysql_fetch_assoc). If you are only returning 1 row (no chance of more) and multiple fields you have to make the decision as to what makes more sense to use although I still normally go with mysql_fetch_assoc.
Hope this makes things a bit clearer.
Posted: Wed Apr 06, 2005 7:21 am
by rsmarsha
Yeah, thanks for all the help.
