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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.