This article is the holy grail solution I need:
http://www.phpbuilder.com/columns/laflamme20001016.php3
But the code is apparently incomplete and full of errors. I've tried to make it work by making various edits suggested in the comments, but for a non-programmer it's impossible to puzzle together.
I'm not a programmer. I'm looking for a stripped down, working example showing the basic principles: How to store/retrieve multiple values from checkboxes using a mapping (?) table.
In my case I want to add multiple categories to an existing table of records. A solution with a 'lookup_category' table ('lookup_skills' in LaFlamme's example) looks like the way to go.
I already had a category solution with a dropdown, but then you can only select one category at a time. I want to replace it with checkboxes.
Another crucial piece missing from Dan LaFlamme's code is how to populate checkboxes that had already been checked. I know that's cumbersome even without a mapping table.
Can anyone please update that code, post a complete working version? Or point me to alternative tutorials?
Here's my non-working version of the code after adapting it to my database, adding and removing edits, losing the plot:
/* helper function for insert_categories(). removes all rows in $table with $id */
function purge_lookup($table, $id) {
$q = "DELETE FROM lookup_category WHERE uid = '$id'";
mysql_query($q);
}
/* helper function for insert_categories(). generates the sctual SQL query */
function create_checkbox_query($arr, $table, $id) {
$q = "INSERT INTO lookup_category (uid, skill_id) VALUES";
foreach ($arr as $check) {
$q .= "($id , $check )" . ",";
}
/* remove the last comma and return */
return substr($q, 0, -1);
}
/* first, we'll delete any entries this user already has in the table */
purge_lookup("lookup_category", $id);
/* now create the sql insert query */
$query = create_checkbox_query($categories, "lookup_category", $id);
/* execute the query */
mysql_query($query);
function get_checkbox_labels() {
/* make an array */
$arr = array();
/* construct the query */
$query = "SELECT * FROM categories";
/* 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>\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><input type="checkbox" name="$name" value="$ele->id"';
foreach ($checked as $entry) {
if ($entry == $ele->id) {
$str .= "checked";
continue;
}
}
$str .= ">";
$str .= "$ele->value";
if ($i % $num == 0) {
$str .= "</tr>\n<tr>";
} else {
$str .= "</td>\n";
}
$i++;
}
} else {
/* we just want to print the checkboxes. none will have checks */
foreach ($arr as $ele) {
$str .= "<td><input type=\"checkbox\" name=\"$name\" value=\"$ele->id\">";
$str .= "$ele->value";
if ($i % $num == 0) {
$str .= "</tr>\n<tr>";
} 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;
}
/* get the checkbox labels */
$categories = get_checkbox_labels("categories");
/* create the html code for a formatted set of checkboxes */
if (!isset($categories)) {
$checked_categories = array();
} else {
$checked_categories = $categories;
}
$categories = get_checkbox_labels("const_categories");
$html_categories = make_checkbox_html($categories, 3, 400, "categories[]", $checked_categories);
I know this needs html, database access, etc. See Dan LaFlamme's article for details. Hopefully a real PHP-coder can immediately spot the mistakes.