vendredi 19 février 2016

Alternate to if statements for taking multiple form input values

I have a HTML form for users to select country, region, bedroom number and price from.

<form action="" enctype="" method="POST">
<p>Property Search
<select name="search_country">
  <option value="">Select a country</option>
  <option value="uk">UK</option>
  <option value="france">France</option>
</select>
<select name="search_region">
  <option value="">Select a region</option>
  <option value="london">London</option>
  <option value="birmingham">Birmingham</option>
  <option value="paris">Paris</option>
  <option value="calais">Calais</option>
</select>
<select name="search_bedroomnumber">
  <option value="">Select number of bedrooms</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
</select>   

<input type="submit" name="search_submit" value="Search" />

My current way of querying the database is an if statement for every variation of the selected form options like this:

if($_POST['search_country'] == "uk" and $_POST['search_region'] == "london" and $_POST['search_bedroomnumber'] == 4) {
$sqlcommand = "(SELECT * FROM properties 
WHERE country = 'uk' 
AND region = 'london' 
AND bedroomnumber = 4 
ORDER BY price ASC 
$properties = Properties::find_by_sql($sqlcommand);
};

With a lot of options, this would take a huge amount of if statements and i'm sure that there's a much more efficient way that i'm not seeing (very amateur coder here).

Any advice would be appreciated. Thanks.

Aucun commentaire:

Enregistrer un commentaire