Dynamically setting checkbox to checked

I could not find this question in the forum by searching, so I thought I would post it…

I have a checkbox group that is dynamically generated from a SQL statement. Here is that code…

        <p class="formServices">
        	<?php do { ?>
			<label>
            	<input type="checkbox" name="services[<?php echo $row_rsServices['ID']; ?>]" value="1" id="<?php echo $row_rsServices['ID']; ?>">
            	<?php echo $row_rsServices['SERVICE']; ?>
            </label>
          <br>
			<?php } while ($row_rsServices = mysql_fetch_assoc($rsServices)); ?>
          </p>

I have a SQL statement that pulls the previous selections that an end user has made from the db. So what I am attempting to do is to create an edit screen where the end user can make changes. So how do I dynamically set checked=checked on items within this dynamically built checkbox groups? For some reason, I am having difficulty in figuring out the looping logic.

Thanks,

~Clay

I figured it out. Thought I would post it here in case anyone else runs into a similar issue…

<?php while ($row_rsServices = mysql_fetch_assoc($rsServices)) { ?>
        <label>
            <input type="checkbox" name="services[<?php echo $row_rsServices['ID']; ?>]" value="1" id="<?php echo $row_rsServices['ID']; ?>"
				<?php 
					//Reset SQL array pointer
					mysql_data_seek($rsServicesselected,0);
					//Loop over services available and if they have been selected, check them
					while ($row_rsServicesselected = mysql_fetch_assoc($rsServicesselected))
					{
						if ($row_rsServices['ID'] == $row_rsServicesselected['SERVICEID'])
							{
								echo "checked='checked'";
							}
					}
                ?>
             >
            <?php echo $row_rsServices['SERVICE']; ?>
        </label>
      <br>
        <?php } while ($row_rsServices = mysql_fetch_assoc($rsServices)); ?>

One of the keys was resetting the pointer. Note…there is a newer pointer reset function for mysqli

While this will work, it’s not very efficient as you are looping through the whole recordset for each checkbox, so, if you have 10 check boxes, you are looping 100 times.

You might want to consider first loading the selected services into an array like this:

[php]while ($row_rsServicesselected = mysql_fetch_assoc($rsServicesselected))
$arrSelected[] = $row_rsServicesselected[‘ID’];
}[/php]

So we only loop once for the selected services.

Then you should then be able to do something like this:

[php]<?php
while ($row_rsServices = mysql_fetch_assoc($rsServices))
{
?>

<input type=“checkbox” name=“services[<?php echo $row_rsServices['ID']; ?>]” value=“1” id="<?php echo $row_rsServices['ID']; ?>"

<?php //Reset SQL array pointer mysql_data_seek($rsServicesselected,0); //Loop over services available and if they have been selected, check them while ($row_rsServicesselected = mysql_fetch_assoc($rsServicesselected)) { if (in_array($row_rsServices['ID'], $arrSelected)) { echo "checked='checked'"; } } ?>
 >
<?php echo $row_rsServices['SERVICE']; ?>

<?php } while ($row_rsServices = mysql_fetch_assoc($rsServices));?>

$result = mysql_query(“SELECT ID FROM SelectedServices”);[/php]

I will take a look at that. I believe I tried that in an earlier version and could not get it to work for some reason, but I will look again. Thanks for the tip.

Sponsor our Newsletter | Privacy Policy | Terms of Service