mraza Posted September 21, 2010 Share Posted September 21, 2010 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 Quote Link to comment Share on other sites More sharing options...
ShibSta Posted September 21, 2010 Share Posted September 21, 2010 The while() is already looping through the results, you don't need another loop. Look at the in_array() function. Quote Link to comment Share on other sites More sharing options...
mraza Posted September 21, 2010 Author Share Posted September 21, 2010 Thank you solved Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.