I've been trying to create an editable form, but am getting hung up on how to make the checkboxes editable/storable. Using the information provided in the PHPBuilder article "Storing Checkbox Data In Your Database", I realized that the solution in the article won't work for me. 🙁
Although the first part (the code of which, I'll paste below) does, in fact, print beautifully formatted checkboxes based on table values so perfectly, it hurts. But, the part of the script that inserts the checkbox values into the database inserts a separate record for each one checked. So, if you check 2 boxes, you get 2 rows. That won't work for me because there are other elements on this form that I need stored in the same record. I had hoped there was a way to store the checkbox values as an array in mysql, and then anytime a user wants to edit these values, it'll just read it from mysql and check the boxes accordingly.
For example, if I have a grocery list form that has checkboxes: milk, bread, eggs, and a textbox called "buyfrom", I'd like Joe to check off milk and bread and then fill in the textbox with "Safeway". Then, Joe changes his mind, so he calls up the form, and this time, he deselects "milk", selects "eggs", and changes "Safeway" to "Albertsons". I want all of the changes in the same record, in the same table.
Does anyone have code they can point me to that accomplishes exactly this? Here's the borrowed code from the uberexcellent article, which reads the values from my database and will print out really great checkboxes:
/* code truncated */
<table>
<input name="did" type="hidden" value="10125" />
<?php
/* insert code to connect to your database here */
require_once ('../../../files/config.inc');
require_once ('../../../files/mysql_connect.php');
/* get the checkbox labels */
$icos = get_checkbox_labels("incs");
/* create the html code for a formatted set of
checkboxes */
$html_icos = make_checkbox_html($icos, 3, 450, "icos[]");
?>
<form name="gsq" method="POST" action="insertgs.php">
<? echo "$html_icos"; ?>
<br>
<p><b>Comments</b>
<textarea name="comments" cols="55" rows="7"><?php if (isset($_POST['comments'])) echo $_POST['comments']; ?>
</textarea>
/* code truncated */
<input type="submit" value="Submit">
</form>
</body>
</html>
<?php
function get_checkbox_labels($incs) {
/* make an array */
$arr = array();
/* construct the query */
$query = "SELECT * FROM $incs";
/* execute the query */
$qid = mysql_query($query);
/* each row in the result set will be packaged as
an object and put in an array */
while($row= mysql_fetch_object($qid)) {
array_push($arr, $row);
}
return $arr;
}
/* Prints a nicely formatted table of checkbox choices.
$arr is an array of objects that contain the choices
$num is the number of elements wide we display in the table
$width is the value of the width parameter to the table tag
$name is the name of the checkbox array
$checked is an array of element names that should be checked
*/
function make_checkbox_html($arr, $num, $width, $name, $checked=array()) {
/* create string to hold out html */
$str = "";
/* make it */
$str .= "<table width=\"$width\" border=\"0\">\n";
$str .= "<tr align=\"left\" valign=\"top\">\n";
/* determine if we will have to close add
a closing tr tag at the end of our table */
if (count($arr) % $num != 0) {
$closingTR = true;
}
$i = 1;
if (isset($checked)) {
/* if we passed in an array of the checkboxes we want
to be displayed as checked */
foreach ($arr as $ele) {
$str .= "<td align=\"left\" valign=\"top\"><input type=\"checkbox\" name=\"$name\" value=\"$ele->id\"";
foreach ($checked as $entry) {
if ($entry == $ele->value) {
$str .= "checked";
continue;
}
}
$str .= ">";
$str .= "$ele->value";
if ($i % $num == 0) {
$str .= "</tr>\n<tr align=\"left\" valign=\"top\">";
} else {
$str .= "</td>\n";
}
$i++;
}
} else {
/* we just want to print the checkboxes. none will have checks */
foreach ($arr as $ele) {
$str .= "<td align=\"left\" valign=\"top\"><input type=\"checkbox\" name=\"$name\" value=\"$ele->id\">";
$str .= "$ele->value";
if ($i % $num == 0) {
$str .= "</tr>\n<tr align=\"left\" valign=\"top\">";
} else {
$str .= "</td>\n";
}
$i++;
}
}
/* tack on a closing tr tag if necessary */
if ($closingTR == true) {
$str .= "</tr></table>\n";
} else {
$str .= "</table>\n";
}
return $str;
}
?>