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