Jump to content

checkbox select in a loop


mraza

Recommended Posts

Hi , i am running a loop where i show 10 categories to edit , now i have values from previous loop of those categories and i wants checkbox to be selected of all those categories whcih i have in my loop but it only select one here is my code:

<?php 
//$category_links is array which contains 3 values should be checked, there are total 10 categories
$category_links = array(2,5,7);

$query = $db->query("SELECT * from categories ORDER BY id");
while($row = $db->fetchArray($query)):		
?>
<input type="checkbox" name="categories[]" value="<?php echo $row['id']; ?>" >
<?php echo $row['name'];  endwhile; }

 

i tried with for loop but it only select one category not other

 

<?php 
//$category_links is array which contains 3 values should be checked
$category_links = array(2,5,7);
$query = $db->query("SELECT * from categories ORDER BY id");
for($i = 0;$i <= count($category_links);$i++) {
while($row = $db->fetchArray($query)):		
?>
<input type="checkbox" name="categories[]" value="<?php echo $row['id']; ?>" <?php if($row['id'] == $category_links[$i]) { echo "checked=checked"; }?> ><?php echo $row['name']; ?>
<?php  endwhile; 
}

 

Thanks for help

Link to comment
https://forums.phpfreaks.com/topic/213961-checkbox-select-in-a-loop/
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.