我正在创建一个条件搜索,这将取决于大约 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
");
}
}
正如你所看到的,这些循环变得越来越长和复杂。请提出一些建议。提前致谢