-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 に答える