Loops abound!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
curtisfitzke
Forum Newbie
Posts: 4
Joined: Tue Sep 30, 2008 5:58 pm

Loops abound!

Post by curtisfitzke »

<?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?
carmen.smth1
Forum Newbie
Posts: 8
Joined: Mon Sep 29, 2008 12:45 pm

Re: Loops abound!

Post by carmen.smth1 »

If I understand your code correctly, the problem is your foreach loop constantly overwrites $chk so it's only valid for the very last field in $row.
curtisfitzke
Forum Newbie
Posts: 4
Joined: Tue Sep 30, 2008 5:58 pm

Re: Loops abound!

Post by curtisfitzke »

do you know how I could fix this?
carmen.smth1
Forum Newbie
Posts: 8
Joined: Mon Sep 29, 2008 12:45 pm

Re: Loops abound!

Post by carmen.smth1 »

Well it's tricky, because I don't know the data your reading. however.........I dont know if you need the foreach at all, just remove the loop and try$chk = $row[$names]>0?'checked = "checked"':"";.... but I diont know what it meant to do....soo.......
curtisfitzke
Forum Newbie
Posts: 4
Joined: Tue Sep 30, 2008 5:58 pm

Re: Loops abound!

Post by curtisfitzke »

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

<?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; }
?>
carmen.smth1
Forum Newbie
Posts: 8
Joined: Mon Sep 29, 2008 12:45 pm

Re: Loops abound!

Post by carmen.smth1 »

Glad I could help. I know what it like to be stumped!!!
Post Reply