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
https://forums.phpfreaks.com/topic/130295-auto-check-checkboxes-from-mysql-query/
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.

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

	}
}
}
?>

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

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

	}
}
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.