I have tailored it a little to fit my needs so you'll need to change variables, fields, etc. to whatever you use it for but it does output checkboxes for each field in your const table and insert a new row for each checkbox checked in lookup table.
For those with little programming experience, like me, make sure that you have a userid value being passed through your form as well. For instance in mine
$userid=$_GET['userid'];
I have a list of usernames on another page and pass that username's userid through the url.
form.php
<title>Insert Flags</title>
<?php
include ("common.php");
$userid=$_GET['userid'];
$query=" SELECT * FROM users WHERE userid='$userid'";
$result=mysql_query($query);
/* get the checkbox labels */
$flags = get_checkbox_labels("const_flags");
/* create the html code for a formatted set of
checkboxes */
$html_flags = make_checkbox_html($flags, 3, 600, "flags[]");
?>
<body>
<br>
<div align="center">
<form name="flags" method="POST" action="insertflags.php">
<input name="userid" type="hidden" id="userid" value="<? echo $userid;?>">
<?
while($row=mysql_fetch_assoc($result)){
?>
<h1><? echo $row['username']; ?>'s - Flags</h1>
<?
}
?>
<? echo "$html_flags"; ?>
<br>
<input type="submit" value="Submit">
</form>
</div>
</body>
<?php
function get_checkbox_labels($table_name) {
/* make an array */
$arr = array();
/* construct the query */
$query = "SELECT * FROM $table_name";
/* 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->value) {
$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;
}
?>
insertflags.php
<?php
// Connect database.
include ("common.php");
$userid=$_POST['userid'];
// Get values from form.
insert_flags($userid, $_POST['flags']);
/* the function we call to insert.
the $flags argument is the flags array that
is sent to the script when the user hits the submit button
*/
function insert_flags($userid, $flags) {
/* first, we'll delete any entries this user already has
in the table */
purge_lookup("lookup_flags", $userid);
/* now create the sql insert query */
$query = create_checkbox_query($flags, "lookup_flags", $userid);
//echo "Query to execute: [$query]";
/* execute the query */
mysql_query($query);
}
/* helper function for insert_flags().
removes all rows in $table with $userid */
function purge_lookup($table, $userid) {
$query = "DELETE FROM lookup_flags WHERE userid = $userid";
mysql_query($query);
$q = "DELETE FROM $table, WHERE userid = $userid";
mysql_query($q);
}
/* helper function for insert_flags().
generates the sctual SQL query */
function create_checkbox_query($arr, $table, $userid) {
$q = "INSERT INTO $table (userid, flag_id) VALUES";
foreach ($arr as $check) {
$q .= " ( '$userid' , $check )" . ",";
}
/* remove the last comma and return */
return substr($q, 0, -1);
}
/*redirect to this page when complete*/
header( 'Location: listmember.php' ) ;
exit;
?>
I hope this helps someone out there! 🙂