PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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...
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.
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.