Answer the question
In order to leave comments, you need to log in
How to correctly form a data request by parameter filter?
There is a request to a DB.
$poisks = DB::select("select * from items where brand = '$brand' && fuel_type IN('$fuel_type') && location = '$location' && body_type IN('$body_type') && year BETWEEN '$year_from' AND '$year_to' && price BETWEEN '$price_from' AND '$price_to' && engine BETWEEN '$engine_from' AND '$engine_to'");
$brand = $_POST['brand']; "brand" => ""
Answer the question
In order to leave comments, you need to log in
Make a variable which will add optional parameters if something happens.
For example:
$where = '';
if($_POST['brand']){
$where .= ' brand = ' . $_POST['brand'];
}
$poisks = DB::select("select * from items where ". $where ." && fuel_type IN('$fuel_type') && location = '$location' && body_type IN('$body_type') && year BETWEEN '$year_from' AND '$year_to' && price BETWEEN '$price_from' AND '$price_to' && engine BETWEEN '$engine_from' AND '$engine_to'");
$where = '';
if($_POST['brand']){
$where .= 'AND brand = ' . $_POST['brand'];
}
if($_POST['fuel_type']){
$where .= " AND fuel_type IN(". implode(',' , $_POST['fuel_type']) .");
}
$poisks = DB::select("select * from items where (что-то, что всегда будет проверяться) ". $where );
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question