Jump to content


Checkbox to mySQL back to Checkboxes

  • Please log in to reply
8 replies to this topic

#1 FloridaNutz

  • Members
  • PipPip
  • Member
  • 24 posts

Posted 22 August 2006 - 03:31 PM

Ok, I want to edit a bar in my database that has attributes.  Since an Array cannot be put into mySQL I seralized it.

$barKey is seralized...

Now, I want to go back and edit that bar, how can I pull the sealized $barKey information and make all the check boxes check in the right order.  Or am I just going about this the whole wrong way?

This is my edit_bar.php
<input name="barKey[]" type="checkbox" id="barKey" value="bar" />
              <input name="barKey[]" type="checkbox" id="barKey" value="club" />
              <input name="barKey[]" type="checkbox" id="barKey" value="food" />

#2 Daniel0

  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 22 August 2006 - 04:03 PM

I modified your form a bit since you can't have to identical ids in a page, and since we will need them to be different:
<label><input type='checkbox' name='barKey[]' id='bar' value='bar' /> Bar</label>
<label><input type='checkbox' name='barKey[]' id='club' value='club' /> Club</label>
<label><input type='checkbox' name='barKey[]' id='food' value='food' /> Food</label>

Now this will be how to set them checked based on the data in the database (we say that item 1 and 3 should be checked):
// ... do stuff before - we have the unserialized array in $barKey

$checkboxes = array(
	'bar' => 'Bar',
	'club' => 'Club',
	'food' => 'Food',

foreach($checkboxes as $key => $value)
	$checked = in_array($key,$barKey) ? " checked='checked'" : null;
	echo "<label><input type='checkbox' name='barKey[]' id='{$key}' value='{$key}'{$checked} /> {$value}</label>";

#3 FloridaNutz

  • Members
  • PipPip
  • Member
  • 24 posts

Posted 22 August 2006 - 04:33 PM

i'm not sure I started this right then...

add_bar.php (simplified)
<form method="POST" action="do_addbar.php">
<label><input type='checkbox' name='barKey[]' id='Bar' value='Bar' /> Bar</label>
              <label><input type='checkbox' name='barKey[]' id='Club' value='Club' /> Bar</label>

<input type="submit" name="submit" value="Add Bar" />

do_addbar.php (simplified)
$barKeyOut = serialize($_POST['barKey']);
$sql = "INSERT INTO $table_name ) VALUES ('$barKeyOut')";

this is where i get confused on how i am going to unserialize and take that info and fill in the checkboxes...

#4 FloridaNutz

  • Members
  • PipPip
  • Member
  • 24 posts

Posted 22 August 2006 - 04:39 PM


I was wondering if it would be better to create a table in mySQL since i'm going to be associating images with those keys as well...

keyID - keyDescription - keyImage
1 - Bar - bar.png
2 - Club - club.png

#5 HeyRay2

  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 22 August 2006 - 05:08 PM

Placing your attributes into their own table is a good idea.

You could also place the attributes for each bar into a relation table, which would remove the need to have a serialized list of the attributes in the database.

So your MySQL tables would look something like this:




Then for each bar, you can pull an array of attributes currently associated from the relation table(attrib_rel), and compare each to an array of all items from the attributes table.

I'll post some sample code shortly... ;)

#6 FloridaNutz

  • Members
  • PipPip
  • Member
  • 24 posts

Posted 22 August 2006 - 06:33 PM

I love you.


I pretty much learned everything I could about PHP in two days and built an entire admin system and I think my head is going to explode.  The only two things i need left is the Key system and a way to sort tables...

#7 HeyRay2

  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 22 August 2006 - 06:53 PM

Glad we could help!  :)

Some basic code to get you started:

bar_detail.php -- Shows details for a bar in the database, and update form.

// Get the barID from the URL
if( (isset($_GET['barID'])) && (!empty($_GET['barID'])) && (ctype_digit($_GET['barID'])) ){
	// Set the barID from the URL
	$barID = $_GET['barID'];
	// Check if the update form was submitted
		// Update the bar name, description, etc
		$sql_bar_update = "UPDATE bars
				   SET barName = '".mysql_real_escape_string(trim($_GET['barName']))."',
				       barDescription = '".mysql_real_escape_string(trim($_GET['barDescription']))."'
				   WHERE barID = $barID";
		mysql_query($sql_bar_update) or die("Error Updating Bar: ".mysql_error());
		// Update the bar attributes, by removing all the attribute relations
		// then adding back the current selected values
		$sql_attrib_del = "DELETE FROM attribute_rel WHERE rel_barID = ".$barID."";
		mysql_query($sql_attrib_del) or die("Error Removing Attributes: ".mysql_error());
		foreach($attrib_checkboxes as $key=>$value){
			$sql_attrib_add = "INSERT INTO attribute_rel
					   SET rel_barID = ".$barID.",
					       rel_attributeID = ".$value."";
			mysql_query($sql_attrib_add) or die("Error Adding Attribute: ".mysql_error())
	echo "Bar Information Updated!<br />\n";
	// Show the detail page
	// Get the details for the current bar
	$sql_bar = "SELECT * FROM bars WHERE barID = $barID LIMIT 1";
	$bar_result = mysql_query($sql_bar) or die("Error Querying Bar: ".mysql_error());
	// Get an array of attributeID's for the current bar
	$sql_attrib_rel = "SELECT rel_attributeID FROM rel_attributes WHERE rel_barID = $barID";
	$attrib_rel_result = mysql_query($sql_attrib_rel) or die("Error Querying Current Attributes: ".mysql_error());
	while($row = mysql_fetch_array($attrib_rel_result){
		$bar_attrib_array[] = $row['rel_attributeID'];
	// Get the full array of attributes
	$sql_arrtib = "SELECT * FROM attributes ORDER BY attributeName";
	$attrib_result = mysql_query($sql_attrib) or die("Error Querying All Attributes: ".mysql_error());
	// Start the form
	echo '<form name="bar_detail" action="'.$_SERVER['PHP_SELF'].'" method="GET">';
	// Print the bar details
	while($bar_row = mysql_fetch_array($bar_result)){
		echo '<input type="hidden" name="barID" value="'.$barID.'" />\n
		      Bar: <input type="text" name="barName" value="'.$bar_row['barName'].'" />\n
		      Bar Description: <input type="text" name="barDescription" value="'.$bar_row['barDesc'].'" />\n';
		// Print the attribute checkboxes, with current values checked
		while($attrib_row = mysql_fetch_array($attrib_result){
			// Check if the current checkbox should be checked
			$checked = (in_array($attrib_row['attributeID'], $bar_attrib_array)) ? 'checked="checked"' : NULL;
			// Print the checkbox
			echo $attrib_row['attributeName'].' <input type="checkbox" name="attrib_checkboxes[]" value="'.$attrib_row['attributeID'].'" '.$checked.' />\n';
	// End the form
	echo '<input type="submit" name="Bar_Detail_Update" value="Update Bar!" />
} else {
	// No barID was passed, error out!
	echo "Invalid or No Bar ID Specified!";


#8 FloridaNutz

  • Members
  • PipPip
  • Member
  • 24 posts

Posted 22 August 2006 - 09:52 PM

I don't think this is what i ment...

Add Bar:

Name of Bar = $barName

Things that describe the bar  =  $barKey
[ ] bar  [x] club  [ ] live music

i have two tables

DSO_bars                              DSO_key
--------------                        --------------
$barName = "x"                        keyID = 1  keyDescription = Bar  keyImage = bar.gif
$barPhone = "555-5555"                        2                        Club                  club.gif
$barKey = ???                                    3                          Live Music          livemusic.gif
i tried serializing, but couldn't get it to work

I already have a form that fills in all those areas, including checkboxes that adds the entries just like for mentioned.  I just want to be able to use the same form as submited before to come up when you edit the bar again, which i pretty much have except for the check boxes.

This is how I got my former drop down menus...
<select name="monFemaleAge">
                      <option value="">-- Select One--</option>
    					$ages = array("Teen","18+","21+");
    					foreach($ages as $val) {
						$chk = $val==$monFemaleAge ? 'selected' : '';
        				echo "<option value='$val' $chk> $val</option>"; } ?>

#9 FloridaNutz

  • Members
  • PipPip
  • Member
  • 24 posts

Posted 22 August 2006 - 09:56 PM

I think I might be able to fix this if I could make some sort of an array to make it add each of the selections and then be able to brake them back down later....

barKey= "/bar /club /pool /lm"

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users