<?php
/* insert code to connect to your database here */
/* get the checkbox labels */
$skills = get_checkbox_labels("const_skills");
/* create the html code for a formatted set of
checkboxes */
$html_skills = make_checkbox_html($skills, 3, 400, "skills[]");
?>
<html> <body> <br> <form name="skills" method="POST" action="insertskills.php"> Check off your web development skills:
<? echo "$html_skills"; ?> <br> <input type="submit" value="Submit"> </form> </body> </html>
<?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) {
/* 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;
}
?>
That was quite a bit of code, so here is a quick overview of what is going on. The majority of the work is done in two functions,
with that number. We then pass this array to
insert the string into our html form code to include the skill checkboxes in our form. Notice that I didn’t pass the
is an array of the objects we want to appear checked. If a user learned a new web development skill after filling out the form for the first time, we could provide
an “edit skills” page that would present the checkboxes with his previously stored skills already checked off.
get_checkbox_labels
andmake_checkbox_html
. get_checkbox_labels
queries our const_skills table and returns an array of objects, each object having an id and the name of the skill that correspondswith that number. We then pass this array to
make_checkbox_html
along with a few other parameters and it returns a string with the html code for the checkboxes. We now justinsert the string into our html form code to include the skill checkboxes in our form. Notice that I didn’t pass the
$checked
argument to make_checkbox_html
. This argumentis an array of the objects we want to appear checked. If a user learned a new web development skill after filling out the form for the first time, we could provide
an “edit skills” page that would present the checkboxes with his previously stored skills already checked off.
What is the benefit of creating the form in this way instead of simply hard coding the skill choices in the html code of our form? Well, suppose we want to allow the web
developers to be able to choose a skill that we don’t already have in our const_skills table, DHTML for instance. All we would need to do is insert a row for DHTML into the
const_skills table. Then, when the developers go to the form page, DHTML will be there. No modifications to the html form code is necessary.
developers to be able to choose a skill that we don’t already have in our const_skills table, DHTML for instance. All we would need to do is insert a row for DHTML into the
const_skills table. Then, when the developers go to the form page, DHTML will be there. No modifications to the html form code is necessary.