mysql - Php filter system -


i want make php filter system 4 variables:

echo"<form action='' method='get' class='form-inline' role='form'>"; $query = "select naam soortmaaltijd"; //alle soortmaaltijden ophalen $result= mysql_query($query) or die(mysql_error()); echo"<div class='row'>";     echo"<div class='form-group' >";     echo"<label for='soortmaaltijd'>soort maaltijd</label></br>";     echo"<select name='soortmaaltijd' class='form-control' id='soortmaaltijd'>";         echo"<option value=''>alle</option>";     while($row=mysql_fetch_array($result)) {                 echo"<option value='$row[soortmaaltijdid]'>$row[naam]</option>";     }     echo"</select>";     echo"</div>";      $query = "select * soortgerecht"; //alle soortgerechten ophalen     $result= mysql_query($query) or die(mysql_error());     echo"<div class='form-group' >";     echo"<label for='soortgerecht'>soort gerecht</label></br>";     echo"<select name='soortgerecht' class='form-control' id='soortgerecht'>";         echo"<option value=''>alle</option>";     while($row=mysql_fetch_array($result)) {                 echo"<option value='$row[soortgerechtid]'>$row[naam]</option>";     }     echo"</select>";     echo"</div>";      echo"<div class='form-group' >";     echo"<label for='moeilijkheid'>moeilijkheid</label></br>";//moeilijkheid     echo"<select name='moeilijkheid' class='form-control' id='moeilijkheid'>";         echo"<option value=''>alle</option>";                echo"<option value='1'>1</option>";         echo"<option value='2'>2</option>";         echo"<option value='3'>3</option>";     echo"</select>";     echo"</div>";      echo"<div class='form-group' >";     echo"<label for='tijd'>max bereidingstijd</label></br>";//max bereidingstijd     echo"<select name='tijd' class='form-control' id='tijd'>";         echo"<option value=''>alle</option>";                echo"<option value='5'><5</option>";         echo"<option value='10'><10</option>";         echo"<option value='15'><15</option>";         echo"<option value='20'><20</option>";         echo"<option value='25'><25</option>";         echo"<option value='30'><30</option>";     echo"</select>";     echo" <button type='submit' name='filter' class='btn btn-primary btn-lg-2'>filter</button>";     echo"</div>"; echo"</div>"; echo"</form>"; ?> 

but how can contruct query uses variables when filter settings aren't changed. possible create 20 queries costs time. possible create this:

where tijd = $tijd , soortmaaltijd = $soortmaaltijd , soortgerecht = $soortmaaltijd , moeilijkheid = $moeilijkheid 

but if value not set in filter 'tijd', 'tijd' has standard value?

you need construct full where in advance instead of sending 4 vars query send single where statement.

example 1 (constructing $where var concatenates conditions):

$where = "where "; $count = 0; if ( !empty($tijd) ) {      $where .= "`tijd` = " . $tijd . " ";     ++$count;  } elseif( !empty($soortmaaltijd) ) {      if ($count == 0)         $where .= "`soortmaaltijd` = " . $soortmaaltijd . " ";     else         $where .= "and `soortmaaltijd` = " . $soortmaaltijd . " ";      ++$count;  } elseif( !empty($soortgerecht) ) {      if ($count == 0)         $where .= "`soortgerecht` = " . $soortgerecht . " ";     else         $where .= "and `soortgerecht` = " . $soortgerecht . " ";      ++$count;  } elseif( !empty($moeilijkheid) ) {      if ($count == 0)         $where .= "`moeilijkheid` = " . $moeilijkheid . " ";     else         $where .= "and `moeilijkheid` = " . $moeilijkheid . " ";      ++$count;  } else {     $where = null; // if none of conditions met null                     // entire `where` statement can safely                    // send our sql query regardless of no conditions                    // being met }  // sql statement like: $sql = "select *         tablename         $where";  // remember: if ($where == null)                   // means no filters set ,                   // records table returned 

this example assumes want handle empty vars null queries. it's bit unclear in question whether want skip querying null values or if want set null vars default values. mentioned in comments, setting vars toggle on default values easy using ternary operator.

example 2 (setting default value on empty vars):

$default_tijd = "whatever want"; $tijd = ($tijd) ? $tijd : $default_tijd; // if $tijd set take                                          // value, else use $default_tijd 

however, i'm positive don't want return records filters aren't set setting default value vars going filter records when don't want to. want first example.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -