tutorialstuff Posted October 27, 2008 Share Posted October 27, 2008 I am working on a CMS for a restaurant guide. The admin section will let users change/add normal restaurant details such as name, phone, description etc... The problem I'm having is figuring out the best way to list a group of check boxes that let's the user select the restaurant category. For example: A restaurant can have multiple categories such as: American food, Chinese, Cafe, Burgers, steaks, fast food etc... I have no problem setting up the database to get this right. The problem I'm running into is what is the best way to query the database and populate the check boxes to be checked or un-checked for a particular restaurant. Hope this makes sense. Quote Link to comment Share on other sites More sharing options...
tutorialstuff Posted October 27, 2008 Author Share Posted October 27, 2008 I have a table called restaurant_types that has two fields (restaurant_id and category_id). so basically I make the quert of SELECT * FROM restaurant_types WHERE restaurant_id = (whatever the id of the current restaurant I'm on is such as 4) Let's say the query came up with two results of category_id = 9 and category_id = 8 the check box area should end up looking something like this (note that the categories that came up in the query are checked and the categoris are in alphabetical order): <input type="checkbox" name="type[]" value="9" checked="checked" /> American <input type="checkbox" name="type[]" value="12" /> Bakery <input type="checkbox" name="type[]" value="8" checked="checked" /> BBQ <input type="checkbox" name="type[]" value="18" /> Breakfast <input type="checkbox" name="type[]" value="21" /> Brewery <input type="checkbox" name="type[]" value="1" /> Chinese <input type="checkbox" name="type[]" value="13" /> Coffee/Tea <input type="checkbox" name="type[]" value="3" /> Fast Food <input type="checkbox" name="type[]" value="22" /> Fine Dining <input type="checkbox" name="type[]" value="17" /> German Hope this explains it a little better. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 27, 2008 Share Posted October 27, 2008 Are the checkboxes displayed via a loop from the query results? Or from a different query? Quote Link to comment Share on other sites More sharing options...
tutorialstuff Posted October 27, 2008 Author Share Posted October 27, 2008 I guess it would be from a different query. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 27, 2008 Share Posted October 27, 2008 Can you show us the code of the page, not really got alot to work with ATM. Quote Link to comment Share on other sites More sharing options...
tutorialstuff Posted October 27, 2008 Author Share Posted October 27, 2008 <?php if($_GET['id'] != ''){ $restaurant_query = mysql_query("SELECT * FROM `restaurant` WHERE approved = 'yes' AND id = $_GET[id]"); $num_rows = mysql_num_rows($restaurant_query); if($num_rows == 0){ print "We don't currently have any restaurants that match your criteria."; }else{ while($restaurant_results = mysql_fetch_assoc($restaurant_query)){ "code for check boxes would go here" } } } ?> Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 27, 2008 Share Posted October 27, 2008 <?php if($_GET['id'] != ''){ $restaurant_query = mysql_query("SELECT * FROM `restaurant` WHERE approved = 'yes' AND id = $_GET[id]"); $num_rows = mysql_num_rows($restaurant_query); if($num_rows == 0){ print "We don't currently have any restaurants that match your criteria."; }else{ while($restaurant_results = mysql_fetch_assoc($restaurant_query)){ if ($_GET['id'] == $resteraunt_results['category_id']){ $check = 'checked="checked"'; }else{ $check = ''; } echo '<input type="checkbox" name="type[]" value="{$resteraunt_results['category_id']}" $check /> {$resteraunt_results['type']}<br>'; } } } ?> Quote Link to comment Share on other sites More sharing options...
tutorialstuff Posted October 27, 2008 Author Share Posted October 27, 2008 Your method would only produce one checkbox I need something more like this: <?php if($_GET['id'] != ''){ $restaurant_query = mysql_query("SELECT * FROM `restaurant` WHERE approved = 'yes' AND id = $_GET[id]"); $num_rows = mysql_num_rows($restaurant_query); if($num_rows == 0){ print "We don't currently have any restaurants that match your criteria."; }else{ while($restaurant_results = mysql_fetch_assoc($restaurant_query)){ $category_query = mysql_query("SELECT * FROM restaurant_category"); while($category_results = mysql_fetch_assoc($category_query)){ "code to see if restaurant currently has this category" print "<input type=\"checkbox\" name=\"type[]\" value=\"$category_results[id]\" $checked /> $category_results[name]"; } } } } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.