FloridaNutz Posted August 22, 2006 Share Posted August 22, 2006 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[code]<input name="barKey[]" type="checkbox" id="barKey" value="bar" /> Bar <input name="barKey[]" type="checkbox" id="barKey" value="club" /> Club <input name="barKey[]" type="checkbox" id="barKey" value="food" /> Food[/code] Link to comment https://forums.phpfreaks.com/topic/18308-checkbox-to-mysql-back-to-checkboxes/ Share on other sites More sharing options...
Daniel0 Posted August 22, 2006 Share Posted August 22, 2006 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: [code]<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>[/code]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):[code]<?php// ... 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>";}?>[/code] Link to comment https://forums.phpfreaks.com/topic/18308-checkbox-to-mysql-back-to-checkboxes/#findComment-78636 Share on other sites More sharing options...
FloridaNutz Posted August 22, 2006 Author Share Posted August 22, 2006 i'm not sure I started this right then...add_bar.php (simplified)[code]<form method="POST" action="do_addbar.php"><label><input type='checkbox' name='barKey[]' id='Bar' value='Bar' /> Bar</label> Bar <label><input type='checkbox' name='barKey[]' id='Club' value='Club' /> Bar</label> Club<br><input type="submit" name="submit" value="Add Bar" /></form>[/code]do_addbar.php (simplified)[code]$barKeyOut = serialize($_POST['barKey']);$sql = "INSERT INTO $table_name ) VALUES ('$barKeyOut')";[/code]this is where i get confused on how i am going to unserialize and take that info and fill in the checkboxes... Link to comment https://forums.phpfreaks.com/topic/18308-checkbox-to-mysql-back-to-checkboxes/#findComment-78643 Share on other sites More sharing options...
FloridaNutz Posted August 22, 2006 Author Share Posted August 22, 2006 update: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 - keyImage1 - Bar - bar.png2 - Club - club.png Link to comment https://forums.phpfreaks.com/topic/18308-checkbox-to-mysql-back-to-checkboxes/#findComment-78644 Share on other sites More sharing options...
HeyRay2 Posted August 22, 2006 Share Posted August 22, 2006 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:[b]bars[/b]barIDbarNamebarDescriptionetc...[b]attributes[/b]attributeIDattributeNameattributeImage[b]attribute_rel[/b]relIDrel_barIDrel_attributeIDThen for each bar, you can pull an array of attributes currently associated from the relation table([b]attrib_rel[/b]), and compare each to an array of all items from the [b]attributes[/b] table.I'll post some sample code shortly... ;) Link to comment https://forums.phpfreaks.com/topic/18308-checkbox-to-mysql-back-to-checkboxes/#findComment-78668 Share on other sites More sharing options...
FloridaNutz Posted August 22, 2006 Author Share Posted August 22, 2006 I love you.Seriously.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... Link to comment https://forums.phpfreaks.com/topic/18308-checkbox-to-mysql-back-to-checkboxes/#findComment-78713 Share on other sites More sharing options...
HeyRay2 Posted August 22, 2006 Share Posted August 22, 2006 Glad we could help! :)Some basic code to get you started:[b]bar_detail.php[/b] -- Shows details for a bar in the database, and update form.[code]<?php// Get the barID from the URLif( (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 if($_GET['Bar_Detail_Update']){ // 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!" /> </form>'; } else { // No barID was passed, error out! echo "Invalid or No Bar ID Specified!";}?>[/code] Link to comment https://forums.phpfreaks.com/topic/18308-checkbox-to-mysql-back-to-checkboxes/#findComment-78725 Share on other sites More sharing options...
FloridaNutz Posted August 22, 2006 Author Share Posted August 22, 2006 I don't think this is what i ment...Add Bar:Name of Bar = $barNameThings that describe the bar = $barKey[ ] bar [x] club [ ] live musici have two tablesDSO_bars DSO_key-------------- --------------$barName = "x" keyID = 1 keyDescription = Bar keyImage = bar.gif$barPhone = "555-5555" 2 Club club.gif$barKey = ??? 3 Live Music livemusic.gifi tried serializing, but couldn't get it to workI 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...[code]<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>"; } ?> </select>[/code] Link to comment https://forums.phpfreaks.com/topic/18308-checkbox-to-mysql-back-to-checkboxes/#findComment-78831 Share on other sites More sharing options...
FloridaNutz Posted August 22, 2006 Author Share Posted August 22, 2006 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" Link to comment https://forums.phpfreaks.com/topic/18308-checkbox-to-mysql-back-to-checkboxes/#findComment-78834 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.