Can anyone help me simplify this? I have a page for viewing a bunch of data from a table, the page is supposed to accept different values via GET calls. I’d just like to slim it down a bit. Again, the code works, it’s just spaghetti to me.
if(!$_REQUEST['foo']){
$bar = $_REQUEST['bar'];
if($bar=='all'||!$bar){ $stmt = "SELECT * FROM table
WHERE qty > 0 and
somedate >= \"{$start}\" and
somedate <= \"{$end}\"
ORDER BY id desc
";}
else{
$stmt = "SELECT * FROM table
WHERE qty > 0 and
somedate >= \"{$start}\" and
somedate <= \"{$end}\" and
bar = '$bar'
ORDER BY id desc
";}
}
else{
switch($_REQUEST['foo']){
case 'all':
$stmt = "SELECT * FROM table
WHERE qty > 0 and
somedate >= \"{$start}\" and
somedate <= \"{$end}\"
ORDER BY id desc";
break;
case 'open':
$stmt = "SELECT * FROM table
WHERE qty > 0 and
closd = 0 and
somedate >= \"{$start}\" and
somedate <= \"{$end}\"
ORDER BY id desc";
break;
case 'closed':
$stmt = "SELECT * FROM table
WHERE qty > 0 and
closd = 1 and
somedate >= \"{$start}\" and
somedate <= \"{$end}\"
ORDER BY id desc";
break;
}}
This would handle the switch() assuming you have some sanity checks in the program.
var_dump($stmt);
HTH, ~Ray