0

谁能帮我简化一下?我有一个用于查看表中的一堆数据的页面,该页面应该通过 GET 调用接受不同的值。我只是想瘦一点。再次,代码有效,对我来说只是意大利面条。

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;
}}
4

1 回答 1

2

假设您在程序中有一些健全性检查,这将处理 switch() 。

<?php // RAY_temp_andy_foster.php
error_reporting(E_ALL);

$closd = '1=1';
switch($_REQUEST['foo'])
{
    case 'all':    $closd = '1=1';       break;
    case 'open':   $closd = 'closd = 0'; break;
    case 'closed': $closd = 'closd = 1'; break;
}
$stmt = "SELECT * FROM table WHERE qty > 0 AND somedate >= \"{$start}\"  AND somedate <= \"{$end}\" AND $closd ORDER BY id desc";

var_dump($stmt);

HTH,~雷

于 2012-12-17T17:25:38.523 回答