I am 4 months new to PHP! So please be gentle with me. : )

I have an input form that contains check-boxes(it works and posts array to database correctly!) I have an update form that contains the same check-boxes (it updates and posts array to database correctly BUT IT DOESN'T CORRECTLY DISPLAY database info in the checkboxes!)

The Problem is: I cannot get the update form to correctly display "the check-box array" from the database" It only displays the LAST unit in each array!?!?!?!?!?

Here is my php code to pull the array from the database in a while loop (while loop works perfect and I can echo to the top of the page correctly but I want it in my html form below the php code in the HTML):

...
 
$childarr = explode(",", $row["children_pref"]);
for($i=0; $i<sizeof($childarr); $i++) {
$children_pref = mysql_real_escape_string($childarr[$i]); }

I have MANY of these but will just show you one...

This is my html code for the form...

... 
<input type="checkbox" name="children_pref[]" value="0" <?php if ($children_pref == "0") { echo "checked"; } ?>>
None<br>
<input type="checkbox" name="children_pref[]" value="1" <?php if ($children_pref == "1") { echo "checked"; } ?>> 1<br>
<input type="checkbox" name="children_pref[]" value="2" <?php if ($children_pref == "2") { echo "checked"; } ?>> 2<br>
<input type="checkbox" name="children_pref[]" value="3" <?php if ($children_pref == "3") { echo "checked"; } ?>> 3<br>
<input type="checkbox" name="children_pref[]" value="4" <?php if ($children_pref == "4") { echo "checked"; } ?>> 4<br>
<input type="checkbox" name="children_pref[]" value="5" <?php if ($children_pref == "5") { echo "checked"; } ?>> 5<br>
<input type="checkbox" name="children_pref[]" value="6" <?php if ($children_pref == "6") { echo "checked"; } ?>> 6 or more<br>

Any insight would be greatly appreciated.

After looping $children_pref will hold the last value you assign it to the loop. Why don’t you echo (or add each input checkbox html in a variable) input check boxes inside the loop ?

below is code.
with the help of in_array function.

<?
	$childarr = explode(",", $row["children_pref"]);	
?>
<input type="checkbox" name="children_pref[]" value="0" <?=(in_array('0',$childarr))?('checked'):('');?> >None<br>
<input type="checkbox" name="children_pref[]" value="1" <?=(in_array('1',$childarr))?('checked'):('');?>> 1<br>
<input type="checkbox" name="children_pref[]" value="2" <?=(in_array('2',$childarr))?('checked'):('');?>> 2<br>
<input type="checkbox" name="children_pref[]" value="3" <?=(in_array('3',$childarr))?('checked'):('');?>> 3<br>
<input type="checkbox" name="children_pref[]" value="4" <?=(in_array('4',$childarr))?('checked'):('');?>> 4<br>
<input type="checkbox" name="children_pref[]" value="5" <?=(in_array('5',$childarr))?('checked'):('');?>> 5<br>
<input type="checkbox" name="children_pref[]" value="6" <?=(in_array('6',$childarr))?('checked'):('');?>> 6 or more<br>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.