Jump to content

Recommended Posts

I have this script i am coding and it just repeats the checkboxes over and over... i need it to only show the checkboxes for each amentie once and show checked if it is in the database...

 

 

   <? 

    $result = mysql_query("SELECT * FROM amenties ORDER BY id DESC");
while ($row = mysql_fetch_array($result)) {

foreach ($explode_amen as $aman) {

if($aman == $row['id']) {
$picked = "checked";
} else {
$picked = "";
}	 

?>
    
    <div style="float:left; width:210px;"><input name="amen[]" type="checkbox" id="amen[]" style="width:25px;" value="<?=$row['id']?>" <?=$picked?> /><?=$row['amen_name']?></div>
   
    <? 
 } 
 }
?>

Link to comment
https://forums.phpfreaks.com/topic/241104-help-stop-foreach-looping/
Share on other sites

Instead of the foreach loop you can use this:

 

if (in_array($row['id'], $explode_amen)) {
  # Yes, it's checked
} else {
  # No it's not checked
}

 

That should fix the problem.  The problem is that your checkbox display code is inside the foreach loop.

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.