Page 1 of 1

need help with arrays and check boxes

Posted: Sat Nov 29, 2003 7:30 pm
by gjb79
Hi,

I have been struggling with this problem for some time now and decided that it might be time to ask if anyone has run into this problem before, or has ideas on how to fix it.

I have a list containing check boxes. The list is generated from an array built from a mysql database. When a check box is checked and form submitted, the value is saved to a second table.

When the user returns to this page I want to have the previous items they have already checked show up as being checked.

Code: Select all

<?php  do {   ?>

<input name="selectedfile[]" type="checkbox" value="<?php echo $row_filelist['ID']; ?>" <?php if ( $row_FiletoPagelist['file'] == $row_filelist['ID']) { echo "checked='checked'"; } ?>>

<?php } while ($row_filelist = mysql_fetch_assoc($filelist));?>
currently this works, but only shows one check box as being checked. If I have ten items in the list and five get checked, the last checked item will show up as being checked, the previous four are blank.

Posted: Sat Nov 29, 2003 7:52 pm
by lc
I'm not sure but expect the errror may be in this one

name="selectedfile[]"

Posted: Sat Nov 29, 2003 7:54 pm
by Quietus
Your input boxes do not appear to be uniquely named... as separate inputs, they need to be separately named, else only the last will be accepted.

Posted: Sat Nov 29, 2003 8:03 pm
by gjb79
They are named the same for two reasons. 1 the are saved into an array which I use to update the database, as such they need to be named with the same name and []'s, and 2 because the list is dynamically generated.

The problem isn't with the name portion of the input code, but with the if then statement. I suspect that when it takes the two variables to compair, one is looping (filelist) while the other is not (filetopagelist).

Does anybody know how to stop a loop when it finds a match?

Posted: Sat Nov 29, 2003 8:37 pm
by oldtimer
I use check boxes on a few places with arrays. for example

Code: Select all

<?php
echo "<input type="checkbox" name=player[$i] value="".$guser."">$guser\n"; echo "<BR>";

?>
that makes the check boxes for me. I then select however many I want and on the next page it looks for the number checked and processes that many times.


Code: Select all

<?php

$i=0;
  while ($i < $total) {  

echo "$player[$i]<BR>";
++$i; }
?>

Posted: Sat Nov 29, 2003 9:13 pm
by gjb79
Yep that works too. Right now It works perfectly as far as listing the number of check boxes that I need from the array.

My problem is I need the check boxes to actually start as being 'checked'

Lets say you have an internet mail box that has several customizable settings, all doen with check boxes. You check the options you want and save it. Later on, few weeks or months, you want to change your options. When you load the options page those check boxes start off as being checked with whatever you previously selected.

I am trying to accomplish this. I have a list being loaded from an array, and I wish to have the saved options start as being a 'checked' checkbox.

Posted: Sat Nov 29, 2003 9:19 pm
by oldtimer

Code: Select all

<?php

echo "<input type="checkbox" name=player[$i] value="".$guser."" checked>$guser\n"; echo "<BR>"; 


?>
just put that checked there and away you go.

Posted: Sun Nov 30, 2003 12:30 pm
by gjb79
Not sure you understand. That code will go through and check everything in the list automatically. I do not want that. I am trying to set it up so that the first time someone visits, nothing is checked. they make thier selections, save it, then, if they return to the page thier selections (which may not be every check box) are selected while the options they did not choose are not selected.

I have two tables. table one Filelist - is the list the check boxes generate from. Table two - FiletoPagelist - is the table holding the values of the check boxes that have been selected and saved.

The idea is to have the list generate and check to see if the filelist and filetopagelist contain the same value, if so the box should be checked. if not it should be left blank.

Posted: Sun Nov 30, 2003 12:49 pm
by mchaggis
Have you tried print out these vars to see what they actually contain?

Code: Select all

<?php do {   ?> 
$row_filelist['ID'] = <?=$row_filelist['ID']?><br />
$row_FiletoPagelist['file'] = <?=$row_FiletoPagelist['file']?>br />

<input name="selectedfile[]" type="checkbox" value="<?php echo $row_filelist['ID']; ?>" <?php if ( $row_FiletoPagelist['file'] == $row_filelist['ID']) { echo "checked='checked'"; } ?>> 

<?php } while ($row_filelist = mysql_fetch_assoc($filelist));?>
While the above may not answer your questions, it may help you figure out whats going wrong

Posted: Sun Nov 30, 2003 12:51 pm
by mchaggis
I suspect that the prob is with the comparisom fo the $row_FiletoPagelist['file'] var as this would appear to stay the same within the loop??

EXACTLY!!!

Posted: Sun Nov 30, 2003 1:11 pm
by gjb79
I've checked the arrays and each shows all the correct values that I need however I had to loop them seperately.

But your right, when it does the compairison, the files array loops, while the filetopage doesn't seem to loop. it only takes the last value in the array for compairison.

I dont know how to get both to loop at the same time. When I try to do a loop within a loop, it may find a match with the first item and echo the 'checked', but when it does the next cycle in its loop and doesn't find a match I think it overwrites the 'checked' from the first loop and shows nothing.

any ideas?

Posted: Sun Nov 30, 2003 1:16 pm
by mchaggis
What is the loop for the $row_FiletoPagelist array?

Posted: Sun Nov 30, 2003 1:25 pm
by gjb79
Currently there isn't one, I guess. I tried doing a loop within the input tag.

Code: Select all

<input name="selectedfile[]" type="checkbox" value="<?php echo $row_filelist['ID']; ?>" <?php  do { if ( $row_FiletoPagelist['file'] == $row_filelist['ID']) { echo "checked='checked'"; } } while ($row_filelist = mysql_fetch_assoc($FiletoPagelist)); ?>>
didn't work

Is there any way to take the existing code :

Code: Select all

<?php  do {   ?> 

<input name="selectedfile[]" type="checkbox" value="<?php echo $row_filelist['ID']; ?>" <?php if ( $row_FiletoPagelist['file'] == $row_filelist['ID']) { echo "checked='checked'"; } ?>> 

<?php } while ($row_filelist = mysql_fetch_assoc($filelist));?>
and add a code to the "while" portion to get the FiletoPagelist array to loop with the filelist array?

Code: Select all

<?php } while ($row_filelist = mysql_fetch_assoc($filelist) + mysql_fetch_assoc($FiletoPagelist));?>
Thanks