Jump to content

array of checkboxes


rn14

Recommended Posts

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

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 :P

Link to comment
https://forums.phpfreaks.com/topic/134782-array-of-checkboxes/#findComment-701843
Share on other sites

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

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

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.