rn14 Posted November 29, 2008 Share Posted November 29, 2008 Hi, <p><input type="checkbox" name="meal[]" value="<?php echo $row['id']; ?>" CHECKED/> The above is printed out as part of a loop. there are 7 checkboxes printed out each one being checked. However i still want the seven printed out but with only the first 5 checked. Could anybody help me with this? Thanks Link to comment https://forums.phpfreaks.com/topic/134782-array-of-checkboxes/ Share on other sites More sharing options...
ds111 Posted November 29, 2008 Share Posted November 29, 2008 here... <?php if($i =< 5) { echo'<p><input type="checkbox" name="meal[]" value="'.$row['id'].'" checked="checked"/>'; } else { echo'<p><input type="checkbox" name="meal[]" value="'.$row['id'].'" />'; } replace the $i variable with whatever other variable you are using that you add 1 at the end of the loop. Note i didnt test this code Link to comment https://forums.phpfreaks.com/topic/134782-array-of-checkboxes/#findComment-701843 Share on other sites More sharing options...
rn14 Posted November 29, 2008 Author Share Posted November 29, 2008 Its part of a while loop so will I be able to work it like that. I have to still print out the 7 checkboxes but just with 5 selected while($row = mysql_fetch_array($result)) { $image = $row['image']; $title = $row['title']; $description = $row['text']; echo strip_tags($image, '<img>, <span>'); ?> <p><input type="checkbox" name="meal[]" value="<?php echo $row['id']; ?>" CHECKED/> What you think?? Link to comment https://forums.phpfreaks.com/topic/134782-array-of-checkboxes/#findComment-701850 Share on other sites More sharing options...
rn14 Posted November 29, 2008 Author Share Posted November 29, 2008 Really tearing my hair out with this could anybody give me some help?? Thanks Link to comment https://forums.phpfreaks.com/topic/134782-array-of-checkboxes/#findComment-701870 Share on other sites More sharing options...
rn14 Posted November 29, 2008 Author Share Posted November 29, 2008 Should this work???? <? if($rows <= 5) { echo'<p><input type="checkbox" name="meal[]" value="'.$row['id'].'" checked="checked"/>'; } else { echo'<p><input type="checkbox" name="meal[]" value="'.$row['id'].'" />'; } ?> Thanks Link to comment https://forums.phpfreaks.com/topic/134782-array-of-checkboxes/#findComment-701886 Share on other sites More sharing options...
rn14 Posted November 30, 2008 Author Share Posted November 30, 2008 Any chance of anybody having a look at this??? Link to comment https://forums.phpfreaks.com/topic/134782-array-of-checkboxes/#findComment-702258 Share on other sites More sharing options...
.josh Posted November 30, 2008 Share Posted November 30, 2008 I don't understand how you can work with a database and use a loop but fail to understand the concept of incrementing a variable, but whatever. <?php $x = 0; while(true) { $checked = ($x < 5)? "checked='checked'" : ""; echo "<p><input type='checkbox' name='meal[]' value='{$row['id']}' $checked/>"; $x++; } Link to comment https://forums.phpfreaks.com/topic/134782-array-of-checkboxes/#findComment-702262 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.