Marking Checkboxes as Checked if in_array

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

So in your while loop you get a row from query 4 and a row from query 2.
Then you put the date_id from the query 4 row in an array with 1 element (the date_id).
Then you put the date_id from the query 2 row in a variable.
Then you check if that variable is present in the array with 1 element.

I don’t know what those queries did, and I’m not sure what you’re trying to do with this code, but why don’t you just compare the two date_id’s, instead of putting 1 in an array that contains only that 1 date_id?

Thank you for your response Guido

Great Point - I tried that but didn’t know how to check if date_id in query2 are in date_id from query4 / i only knew in_array.

Your comment seems right - had to make it an array just to see if one is in the other - any ideas on how to do it your way?

thank you

Sorry maybe I need to provide more information.

$query2 - checks to see which checkboxes the user selected
$query4 - a list of all checkboxes available.

we’d like to display the checkboxes as checked if the date_id exists in $query2

$query4 controls the checkboxes displayed.

Thank you for any help you can offer.

I would do something along these lines

 
$availChkBoxes = array();
 
while ($div=mysql_fetch_assoc($query4)) {  //put all avail checkboxes in an array
       $availChkBoxes[] = $div['date_id'];
} 
 
while($row22=mysql_fetch_assoc($query2) {
       if(in_array($row22['date_id'],$availChkBoxes)) {
              //do something
       } else {
              //do something else
    }
}
 

Thank you Kalon

When i do this it only shows one checkbox and doesn’t loop through. If I remove the second while statement, i get all the checkboxes but the system doesn’t seem to know which ones are checked - eventhough if I echo them, it does…

funny, funny…

thank you

without seeing your updated code I can’t tell what the problem is.

but the first thing I would do is use mysql_num_rows() to make sure you have the correct number of rows in both $query4 and $query2 result sets.

to debug your code you can also use echo statements to echo the values of $div[‘date_id’] and $row22[‘date_id’] to make sure they are correct.

Hi Kalon

That is the wierdest thing - when I echo date_id - it is always correct / i tried your suggestion for mysql_num_rows and that was also correct.

The program acts correct if I choose the first of the 3 checkboxes… or all 3. However, if the middle, or last 2 are selected, or first and third - none are selected.

Here the code I ran

	while ($div=mysql_fetch_assoc($query4)) {
					$name[]= $div['date_id'];
	
			$row22=mysql_fetch_assoc($query2);


	if (in_array($row22['date_id'], $name)) {

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

}else{

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

}

	}

ok, but the logic in your while loop code is not what I posted.

Hi Kalon

When I used your code as is with the 2 while loops it only showed one checkbox and not 3…

perhaps i’ve misunderstood…

Hi Kalon

Thank you for your expertize… your code is showing a checked checkbox for each item in $query2 however it’s not showing all the checkboxes with only those selected.

again, without seeing your updated code I can’t tell what you have done wrong and so I can’t help anymore.

Thank you Kalon

I understand if you can’t help anymore… however here’s the code I used.

2 things: name of checkbox isn’t printing ($div[‘date’]), only showing checkboxes that are checked.

$availChkBoxes = array();

while ($div=mysql_fetch_assoc($query4)) {  
//put all avail    checkboxes in an array
          $availChkBoxes[] = $div['date_id'];
}

while($row22=mysql_fetch_assoc($query2)) {
      if(in_array($row22['date_id'],$availChkBoxes)) {
          echo '<input type="checkbox" value="' . $div[date_id] .'" checked>'.$div['date'].'</option>';

}else{

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

Have a great day

if you validate using the w3c validator the ouputed html your php script generates I think you will find some invalid code which could be the cause of the names not being displayed, assuming you have checked that the records returned from the database for each query are in fact correct.

Thank you Kalon

you were right about the names - they printing fine.

Just trying to figure out how to display all the checkboxes - right now, the code displayed above displays only the checkboxes checked.

seems we’re almost there, just missing something.

Thank you for your time.

ok, I think I misunderstood what you want.

I’m now working on the assumption you want all the checkboxes displayed and the checkboxes that are in $query2 should be set to checked by default.

 
$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"
    } else {
            //display checkbox without being checked
    }

    //reset the pointer in $row22 back to the start
    mysql_data_seek($row22,0);
} 


Hi

Working way better but we have something funny… i’m going to check the rest of the code in case I made a mistake.

your code - i just changed the data_seek to $query2 as we were getting an error of not existing with $row22.

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.

ooops I made a couple of errors :headbang:,one of which you picked up with mysq_data_seek().

the other is I haven’t reset $checked at the beigining of each outer while lopp iteration

put $checked = false inside the outer while loop

 
while ($div=mysql_fetch_assoc($query4)) {  //$query4 contains all the checkboxes
    [COLOR=red]$checked = false;[/COLOR]
    while($row22=mysql_fetch_assoc($query2)) { //query2 contains the checked checkboxes

Kalon

You are truly a patient one… love the head banging, seems i’ve been doing that all day!

YEAH!!! it worked…

now i’m going to try and update it back into the database!

Thank you so much :rofl: