samedi 21 février 2015

mysql filter results from previous query

If I a query such as:



select * from table1 where product_title = $search


I have my query results and I echo them onto my page.


I want to use checkboxes for 4 different filters on this page. If a user selects a checkbox from 1 of the filters, how can I apply this to the existing results from the previous query?


Would the best way be to:



if($isset($_GET['variable_from_checkbox'])){

//filter results by rating of 8 or higher
$array = select * from reviews where rating >= 8;

foreach ($array as $print) {
$product_id = $print['id'];
$id_array = implode(', ', $product_id);
}

//use results from this query to apply the previous query?
$array2 = select * from table1 where product_title = $search and id IN ($id_array)

//echo results
}


And then do this every time a checkbox is selected? So if two checkboxes were selected:



if($isset($_GET['variable_from_checkbox'] && $_GET['variable_from_checkbox2'])){

//filter results from both checkboxes and apply to original query

}


Is there a better way to do this?





Aucun commentaire:

Enregistrer un commentaire