Jump to content

[SOLVED] Loops abound!


dennismonsewicz

Recommended Posts

Code:

 

<?php $checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error());
$field = mysql_num_fields($checkbox_qry);
	while($row = mysql_fetch_assoc($checkbox_qry)) {
	for($i = 2; $i < $field; $i++) {
	$names = mysql_field_name($checkbox_qry, $i);	
foreach($row as $k=>$val) {
	$chk = $val>0?'checked = "checked"':"";
}
$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);																	
$title .= '<div><input type="checkbox" name="checkboxes[' . $names . ']" class="checkbox" id="' . $names . '" ' . $chk . ' /> <label for="checkboxes[' . $names . ']">' . ucwords(str_replace($numbers, '', $names)) . '</label></div>';
}
echo $title;
}?>

 

I am writing out the sql column names with this code. The foreach statement is checking if the values of the columns is > 0 and if so set $chk var to checked="checked" but nothing happens when I run the code. Well The names are printed along with the checkboxes but its not checking to see if $val is > 0. Any ideas on how to get this running?

Link to comment
https://forums.phpfreaks.com/topic/126894-solved-loops-abound/
Share on other sites

AWESOME! It works! Man I have been dealing with this code for three days now and it has been rattling my brain! Thanks!

 

Working Code:

 

<?php $checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error());
	$field = mysql_num_fields($checkbox_qry);
		while($row = mysql_fetch_assoc($checkbox_qry)) {													
			for($i = 2; $i < $field; $i++) {
				$names = mysql_field_name($checkbox_qry, $i);	
				$chk = $row[$names]==1?'checked="checked"':'';
				$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);																	
				$title .= '<div><input type="checkbox" name="checkboxes[' . $names . ']" class="checkbox" id="' . $names . '" ' . $chk . ' /> <label for="checkboxes[' . $names . ']">' . ucwords(str_replace($numbers, '', $names)) . '</label></div>';
			}
	echo $title;
} ?>

Link to comment
https://forums.phpfreaks.com/topic/126894-solved-loops-abound/#findComment-656375
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.