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

Link to comment
Share on other sites

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.

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.