Have an account? Sign in
Login  Register  Facebook
Using checkboxes
How would you go about using checkboxes to query a database? Let's say i'm doing a database about property. Property features would include a pool, views, garden... Users will search using checkboxes for these 3 features. What's the best way to go about it?? Thanks
Started: September 21, 2011 Latest Activity: September 21, 2011 checkbox query
2 Answers
well, lots of approach can do this. this is what i have in mind..
you have 3 tables.
  • properties
  • features
  • properties_features

(1) and (2) is your main table, these have manyToMany relationship which is stored in the join table (3).
in (1), you have a collections of properties, while in (2) you have a collections of features. (i know you'll figure that out.)
table (3) will be the place where you bind each of the property with their respective features, database talk, here you store the property_id and the feature_id.
when it comes to your case, you want user be able to search property by features, which is listed using checkbox. apply the method provided by greentree above, in your query:
SELECT property_id
FROM properties_features
WHERE feature_id==X OR/AND feature_id==Y ...and so on
GROUP BY property_id
the result you should get an array of property_id, which later you can loop or manipulate in your search result page. isnt that fun?!

Posted: MacOS
In: September 21, 2011

For example, if I have this code:
<form>
<input name="features[]" type="checkbox" value="pool"> pool
<input name="features[]" type="checkbox" value="views"> views
<input name="features[]" type="checkbox" value="garden"> garden
</form>
and select the first and third checkboxes, then $_GET will be:
Array ( [features] => Array ( [0] => pool [1] => garden ) )
You can use this data to query a database of your choice.

Posted: greentree
In: September 21, 2011

Thanks...then how would you query the database??
September 21, 2011

Your Answer

xDo you want to answer this question? Please login or create an account to post your answer