/ BACKGROUND /
I have a function that is desigend to create dynamic html checkboxes. I want some checkboxes to be checked by default when page loads. Currently it works with a small annoyance. One function writes a javascript function and a user must mouseover a html link to select the default checkboxes. I want to do away with the javascript.
Query 1: select n_id from tbl where id = $id :: retrives a string of note id's where product id = id
Query 2: select id, name from n_tbl :: retrives id, name from the notes table
/ CODE /
function qry_checkbox($query, $default="", $check_name="", $prefix="", $attrib="", $suffix="", $vl="n")
{
/ $query (str) /
/ $default (ary) /
$output = "";
$selected = "";
$result = mysql_query($query);
$checkbox_link = ereg("href", $prefix);/ find out if user is adding a html hyperlink to $prefix and set $checkbox_link to 1/
$suffix = $suffix . "\r\n"; / allow user to add html after the text string label /
$user_prefix = $prefix;
while (list($key, $val) = mysql_fetch_array($result))
{
while (list($key2, $val2) = each($default))
{
$selected = $val2[0] == $key ? "checked" : "";
}
if ($checkbox_link == 1)
{
/* set $prefix to include $check_name with $key */
/* this allows for creation of html hyperlinks */
$prefix = $user_prefix . $key . "\">" . "<input type=\"checkbox\" name=\"".$check_name."__".$key."\""; /* allow user to add html before the input tag */
}
else
{
/* set $prefix to include $val with $check_name */
$prefix = $user_prefix . "<input type=\"checkbox\" name=\"".$check_name."__".$key."\""; /* allow user to add html before the input tag */
}
$output .= "$prefix value=\"$val\" $selected $attrib >$val$suffix";
}
return $output;
}
/ OUTPUT /
<tr><td><a href="notes.php?action=edit&id=1"><input type="checkbox" name="note1" value="Free Tech" >Free Tech </a></td></tr>
<tr><td><a href="notes.php?action=edit&id=6"><input type="checkbox" name="note6" value="Low cost" >Low cost</a></td></tr>
/ PREFERRED OUTPUT /
<tr><td><a href="notes.php?action=edit&id=1"><input type="checkbox" name="note1" value="Free Tech" CHECKED >Free Tech</a></td></tr>
<tr><td><a href="notes.php?action=edit&id=6"><input type="checkbox" name="note6" value="Low cost" CHECKED >Low cost</a></td></tr>
/ PROBLEM /
The first while loop exes correctly drawing the html. The second while loop only loops through once never anding the 'CHECKED' attribute.