Jump to content

auto check checkboxes from MYSQL query


tutorialstuff

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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"

	}
}
}
?>

Link to comment
Share on other sites

<?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>';      
      }
   }
}
?>

Link to comment
Share on other sites

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]";
		}

	}
}
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.