-1

我正在创建一个条件搜索,这将取决于大约 9 个参数。现在我只使用 4 并且它已经变得相当大并且维护起来很忙。有人可以建议最好的方法,以便我可以缩短代码行并轻松维护它。下面是我的代码

if($min_year=="Select" && $max_year=="") {
        if($makes=='All' && $models=='All Models') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where  vehicles.manufacturerID=models.manufacturerID and vehicles.modelID=models.modelID 
                    and models.manufacturerID=manufacturers.manufacturerID LIMIT $start, $per_page");
            $select_count = mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where vehicles.manufacturerID=models.manufacturerID and vehicles.modelID=models.modelID 
                    and models.manufacturerID=manufacturers.manufacturerID");                                   
        } elseif($makes!='All' && $models=='All Models') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails,vehicles.* from models, manufacturers, 
                    vehicles where  models.manufacturerID=$makes and vehicles.manufacturerID=models.manufacturerID and 
                    vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID  LIMIT $start, $per_page
                    ");
            $select_count = mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where  models.manufacturerID=$makes and vehicles.manufacturerID=models.manufacturerID and 
                    vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID");           

        } elseif($makes=='All' && $models!='All Models') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails,vehicles.* from models, manufacturers, 
                    vehicles where vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID 
                     LIMIT $start, $per_page"); 
            $select_count =  mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID");

        } elseif($makes!='All' && $models!='All Models' && $makes!='select' && $models!='select') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where models.modelID=$models and models.manufacturerID=$makes and vehicles.modelID=models.modelID
                     and models.manufacturerID=manufacturers.manufacturerID LIMIT $start, $per_page");
            $select_count = mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where models.modelID=$models and models.manufacturerID=$makes and vehicles.modelID=models.modelID
                     and models.manufacturerID=manufacturers.manufacturerID");          
        } else {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, 
                    manufacturers, vehicles where vehicles.modelID=models.modelID and 
                    models.manufacturerID=manufacturers.manufacturerID LIMIT $start, $per_page");
            $select_count = mysql_query("select count(vehicles.modelID) as count from models, manufacturers, vehicles 
                        where vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID");                 
        }
    } elseif($min_year!="Select" && $max_year==""){
        if($makes=='All' && $models=='All Models') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where  vehicles.manufacturerID=models.manufacturerID and vehicles.year>=$min_year and
                    vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID LIMIT $start, $per_page 
                    ");
            $select_count = mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where  vehicles.manufacturerID=models.manufacturerID and vehicles.year>=$min_year and
                    vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID");       

        } elseif($makes!='All' && $models=='All Models') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where  models.manufacturerID=$makes and vehicles.manufacturerID=models.manufacturerID and 
                    vehicles.year>=$min_year and vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID 
                     LIMIT $start, $per_page");
            $select_count =  mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where  models.manufacturerID=$makes and vehicles.manufacturerID=models.manufacturerID and 
                    vehicles.year>=$min_year and vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID 
                    ");         

        } elseif($makes=='All' && $models!='All Models') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where vehicles.modelID=models.modelID and vehicles.year>=$min_year and
                     models.manufacturerID=manufacturers.manufacturerID LIMIT $start, $per_page");
            $select_count = mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where vehicles.modelID=models.modelID and vehicles.year>=$min_year and
                     models.manufacturerID=manufacturers.manufacturerID");              
        } else {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails,vehicles.* from models, manufacturers, 
                    vehicles where models.modelID=$models and models.manufacturerID=$makes and vehicles.modelID=models.modelID
                     and vehicles.year>=$min_year and models.manufacturerID=manufacturers.manufacturerID LIMIT $start, $per_page");
            $select_count =  mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where models.modelID=$models and models.manufacturerID=$makes and vehicles.modelID=models.modelID
                     and vehicles.year>=$min_year and models.manufacturerID=manufacturers.manufacturerID");         
        }       
    } elseif($min_year=="Select" && $max_year!=""){
        if($makes=='All' && $models=='All Models') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where  vehicles.manufacturerID=models.manufacturerID and vehicles.year<=$max_year and
                    vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID  LIMIT $start, $per_page
                    ");
            $select_count =  mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where  vehicles.manufacturerID=models.manufacturerID and vehicles.year<=$max_year and
                    vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID");       

        } elseif($makes!='All' && $models=='All Models') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where  models.manufacturerID=$makes and vehicles.manufacturerID=models.manufacturerID and 
                    vehicles.year<=$max_year and vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID 
                     LIMIT $start, $per_page"); 
            $select_count =  mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where  models.manufacturerID=$makes and vehicles.manufacturerID=models.manufacturerID and 
                    vehicles.year<=$max_year and vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID 
                    ");      

        } elseif($makes=='All' && $models!='All Models') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where vehicles.modelID=models.modelID and vehicles.year<=$max_year and
                     models.manufacturerID=manufacturers.manufacturerID LIMIT $start, $per_page");
            $select_count =  mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where vehicles.modelID=models.modelID and vehicles.year<=$max_year and
                     models.manufacturerID=manufacturers.manufacturerID");              
        } else {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where models.modelID=$models and models.manufacturerID=$makes and vehicles.modelID=models.modelID
                     and vehicles.year<=$max_year and models.manufacturerID=manufacturers.manufacturerID LIMIT $start, $per_page");
           $select_count = mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where models.modelID=$models and models.manufacturerID=$makes and vehicles.modelID=models.modelID
                     and vehicles.year<=$max_year and models.manufacturerID=manufacturers.manufacturerID");             
        }       
    } else {
        if($makes=='All' && $models=='All Models') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where  vehicles.manufacturerID=models.manufacturerID and vehicles.year<=$max_year and vehicles.year>=$min_year
                     and vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID LIMIT $start, $per_page 
                    ");
            $select_count = mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where  vehicles.manufacturerID=models.manufacturerID and vehicles.year<=$max_year and vehicles.year>=$min_year
                     and vehicles.modelID=models.modelID and models.manufacturerID=manufacturers.manufacturerID");      

        } elseif($makes!='All' && $models=='All Models') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where  models.manufacturerID=$makes and vehicles.manufacturerID=models.manufacturerID and 
                    vehicles.year<=$max_year and vehicles.year>=$min_year and vehicles.modelID=models.modelID 
                    and models.manufacturerID=manufacturers.manufacturerID LIMIT $start, $per_page ");  
            $select_count = mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where  models.manufacturerID=$makes and vehicles.manufacturerID=models.manufacturerID and 
                    vehicles.year<=$max_year and vehicles.year>=$min_year and vehicles.modelID=models.modelID 
                    and models.manufacturerID=manufacturers.manufacturerID");       

        } elseif($makes=='All' && $models!='All Models') {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where vehicles.modelID=models.modelID and vehicles.year<=$max_year and vehicles.year>=$min_year and
                     models.manufacturerID=manufacturers.manufacturerID LIMIT $start, $per_page");
            $select_count = mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where vehicles.modelID=models.modelID and vehicles.year<=$max_year and vehicles.year>=$min_year and
                     models.manufacturerID=manufacturers.manufacturerID");              
        } else {
            $select = mysql_query("select models.modelID, models.manufacturerID, models.modelName, manufacturers.manufacturerID, 
                    manufacturers.manufacturerName, manufacturers.manufacturerDetails, vehicles.* from models, manufacturers, 
                    vehicles where models.modelID=$models and models.manufacturerID=$makes and vehicles.modelID=models.modelID
                     and vehicles.year<=$max_year and vehicles.year>=$min_year and models.manufacturerID=manufacturers.manufacturerID 
                      LIMIT $start, $per_page");
            $select_count = mysql_query("select count(vehicles.modelID) as count from models, manufacturers, 
                    vehicles where models.modelID=$models and models.manufacturerID=$makes and vehicles.modelID=models.modelID
                     and vehicles.year<=$max_year and vehicles.year>=$min_year and models.manufacturerID=manufacturers.manufacturerID 
                     ");            
        }       
    }

正如你所看到的,这些循环变得越来越长和复杂。请提出一些建议。提前致谢

4

2 回答 2

1

使您的 sql 更通用,而不是预先构建每个案例。只是一个简单的例子:

$strQuery = 'select 
 models.modelID
,models.manufacturerID
,models.modelName
,manufacturers.manufacturerID
,manufacturers.manufacturerName
,manufacturers.manufacturerDetails
,vehicles.*
from 
models
,manufacturers
,vehicles';

$aWhere = array();
if($min_year != "Select")
{
    array_push($aWhere,'vehicles.year >= ' . $min_year);
}
if($makes != 'All')
{
    array_push($aWhere,'models.manufacturerID=' . $makes);
}

if(count($aWhere) > 0)
{
    $strQuery = $strQuery . ' WHERE ';
}
for($i = 0;$i < count($aWhere);$i++)
{
    if($i == 0)
    {
        $strQuery .= $aWhere[$i];
    }
    else
    {
        $strQuery .= ' AND ' . $aWhere[$i];
    }
}

// Append the other things like order by or limit ...

PS:不要忘记转义你的 sql-query 变量!

于 2013-01-30T11:49:58.860 回答
0

你的代码真的已经很复杂了。据我了解,您有 9 个条件来显示不同的查询。

假设,每个条件都会添加一个查询。

现在,您检查条件,然后选择适当的查询。为什么不根据每个条件逐步构建查询?

于 2013-01-30T11:32:27.037 回答