$map=array('Township'=>'township','Range'=>'range');
$where='';
foreach ($map as $k => $v) {
    if (!empty($_GET[$v])) {
        $where.=$k.'='.$_GET[$v];
    }   
}
$query =" SELECT Business_Owner,Business_Name,Mailing_Address,Phone_Number,Crop_Type,Crop_Location,Section,Township,Range,Quarter,Acreage FROM pesticide_sensitive_crop";
if (!empty($where)) {
    $query.=' WHERE '.$where;
}
我这里只是提供了一个思路,代码的安全性,效率,可读性等问题要自己去考虑。

解决方案 »

  1.   


    $map=array('Township'=>'township','Range'=>'range');
    $where='';
    $and='';
    foreach ($map as $k => $v) {
        if (!empty($_GET[$v])) {
            $where.=$and.$k.'='.$_GET[$v];
            $and=' and ';
        }   
    }
    $query =" SELECT Business_Owner,Business_Name,Mailing_Address,Phone_Number,Crop_Type,Crop_Location,Section,Township,Range,Quarter,Acreage FROM pesticide_sensitive_crop";
    if (!empty($where)) {
        $query.=' WHERE '.$where;
    }
    刚才的有个bug,改了下。