Jump to content

Marking Checkboxes as Checked if in_array


Isaiaha

Recommended Posts

while ($div=mysql_fetch_array($query4, MYSQL_ASSOC)) {
    //$div - shows all the entries    

$row22=mysql_fetch_array($query2, MYSQL_ASSOC);
//row22 - shows all the entries that should be checked
$name= array($div['date_id']);
$datess=$row22['date_id'];

if (in_array($datess, $name, true)) {

echo '<input type="checkbox" value="' . $div[date_id] .'" checked>' . $div[date] . '</option>';

}else{

echo '<input type="checkbox" value="' . $div[date_id] .'">' . $div[date] . '</option>';

}  

 

1. when echo $row22 shows only the entries that should be checked - working good

2. checkboxes show checked if only the first checkbox is selected or all 3 - if a combination is selected, it is all blank.

 

 

Any ideas?

Thank you

Hi

 

Thank you - i might have missed it in the posting... this is where i'm at now which is working tons better but having a glitch described at the bottom - wondering if anyone knows?

 

Here's the code:



$checked = false;

while ($div=mysql_fetch_assoc($query4)) {  //$query4 contains all the checkboxes

    while($row22=mysql_fetch_assoc($query2)) { //query2 contains the checked checkboxes

         if($div['date_id'] == $row22['date_id']) {

              $checked = true;

         }

     }



    //display the checkbox

    if($checked) {

             //display checkbox with checked="checked"

             echo '<input type="checkbox" value="' . $div[date_id] .'" "checked">' . $div[date] . '</option>';

    } else {

            //display checkbox without being checked

            echo '<input type="checkbox" value="' . $div[date_id] .'">' . $div[date] . '</option>';

    }



    //reset the pointer in $row22 back to the start

    mysql_data_seek($query2,0);

} 

 

 

 

This is what happens:

 

Anytime checkbox #1 is selected: displays all checkboxes as checked

 

If checkbox #2 is selected: displays 2&3 checked

 

If checkbox #3 is selected: displays correctly - only 3 is checked.

If checkbox 2&3 selected: display correctly

 

 

I hope I haven't kiboshed your code... i was trying something sort of like your code (mine was much messier) and I was having the same issue.

 

 

Wondering what would make it act that way.

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.