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
2 Answers
well, lots of approach can do this. this is what i have in mind..
you have 3 tables.
(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_idthe 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 2 of 2 people found this answer helpful. Did you? Yes No
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 2 of 2 people found this answer helpful. Did you? Yes No Thanks...then how would you query the database?? |
© Advanced Web Core. All rights reserved