Jump to content

Help: Stop Foreach Looping


ipwnzphp

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.

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.