Il seguente codice (utilissimo quando funziona) l'ho preso da:
http://www.phpbuilder.com/columns/la...3?print_mode=1
serve a generare una serie di checkbox prendendo id e label dal database
L'ho modificato leggermente, spero non in maniera distruttiva, e pur non modificando nulla a volte funziona a volte no (es.: ora funziona, mando a capo una riga vuota non funziona più).
Non riesco a capire cosa non digerisce.
Grazie a coloro che vorranno aiutarmi
Il codice da me rivisitato è (omettendo connessione ecc.):
function get_checkbox_labels($table_name) {
/* make an array */
$arr = array();
/* construct the query */
$query = "SELECT ls_tag_id, ls_tag_ita, ls_tag_parent 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;
}
function make_checkbox_html2($arr, $num, $width, $name, $checked) {
/* create string to hold out html */
$str = "";
/* make it */
$str .= "<table border=\"1\">\n";
$str .= "<tr valign=\"top\">\n";
$i = 1;
foreach ($arr as $ele) { //for 1
if ($ele->ls_tag_parent == 0) {
$str .= "\r<td>\r$ele->ls_tag_ita\r";
$e = 1;
$str .= "<table border=\"0\" cellspacing=\"0\" cellpadding=\"2\">";
/* if we passed in an array of the checkboxes we want to be displayed as checked */
foreach ($arr as $ite) {
if ($ite->ls_tag_parent == $ele->ls_tag_id) {
$str .= "\r<tr><td><input type=\"checkbox\" name=\"$name\" value=\"$ite->ls_tag_id\"";
foreach ($checked as $entry) {
if ($entry == $ite->ls_tag_id) {
$str .= "checked";
continue;
}
}// for 3 end
$str .= "></td><td>";
$str .= "$ite->ls_tag_id - $ite->ls_tag_ita</td></tr>";
}
$e++;
}// for 2 end
$str .= "\r</table>";
if ($i % $num == 0) {
$str .= "\r</td>\r</tr>";
} else {
$str .= "\r</td>\n";
}
}
$i++;
} //for1 end
$str .= "</table>\n";
return $str;
}
/* Array che simula una query - lista dei checkbox che saranno "checked" */
$attivi = array(5,7,13,14,22,19,24);
/* get the checkbox labels */
$chkbox = get_checkbox_labels("db_list_tag2");
/* create the html code for a formatted set of checkboxes */
$html_chkbox = make_checkbox_html2($chkbox, 4, 700, "chkbox[]", $attivi);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<form name="form1" method="POST" action="">
<? echo "$html_chkbox"; ?>
<input type="submit" value="Submit"></form>
</body>
</html>