Page 1 of 1
Trouble with looping/selected check boxes.
Posted: Mon Nov 10, 2003 6:22 pm
by gjb79
I have a page that is dynamically generated. There is a check box that needs to compare the values from two recordsets, loop, then compare the next set of values from the same recordsets.
Currently I get the feeling that, although the loop works, the two record sets are not moving on from compairing one row in the record to the next with each loop.
Here is my code:
Code: Select all
<?php
do { ?>
<input <?php if ( $row_FiletoPagelist['file'] == $row_filelist['ID'] ) { echo "checked"; } ?> name="selectedfile" type="checkbox" id="selectedfile">
<?php } while ($row_filelist = mysql_fetch_assoc($filelist));
?>
If anybody has an idea I'd appreciate the help. I'm completely puzzled.
Posted: Tue Nov 11, 2003 3:06 am
by twigletmac
Have a go at this,
Code: Select all
<?php
// a while loop is IMHO much easier to read that a do...while one.
while ($row_filelist = mysql_fetch_assoc($filelist)) {
// do a bit of debugging and print the variables
echo $row_FiletoPagelist['file'].' - '.$row_filelist['ID'].'<br />';
// do the if test before the HTML to make the HTML easier to maintain
if ($row_FiletoPagelist['file'] == $row_filelist['ID']) {
$checked = 'checked="checked"';
} else {
$checked = '';
}
$html = '<input name="selectedfile" type="checkbox" id="selectedfile" $checked />';
// more debugging
echo htmlspecialchars($html);
echo $html;
}
?>
Mac
Strange
Posted: Tue Nov 11, 2003 7:59 pm
by gjb79
Its strange. I tried what you said, and although I know the loop works, since I have other variables echoed in the loop I created from the array, it isn't displaying anything. I've checked the syntax and cannot find anything wrong with the code.
It should display the content of the array, correct? I have no clue why it isn't.
Thanks for your help twigletmac
Greg
Posted: Wed Nov 12, 2003 4:48 am
by twigletmac
Apologies, but I'm not sure I'm clear on this - do you get stuff echoed from within the while loop, or nothing at all from within it?
Mac
Nothing
Posted: Wed Nov 12, 2003 8:50 pm
by gjb79
When I ran the script, nothing appears on the screen at all.
My goal is to create a form that lists several items dynamically from an sql database. Infront of these items is a check box. A user could go in and choose what they wanted from the list. when they save it and return to the page later, I want the items they previously checked to remain checked.
The problems I am having is to get multiple items to "check" I can get one item to do it, but if I select 2 or more only the last one will remain checked when i return to the page. Additionally, only the last item gets saved to the sql database.
I'm really getting frustraited with this as I've run out of ideas on how to search for clues in fixing my problem.
Thanks again twigletmac, if you have any more advice I am open!
Posted: Thu Nov 13, 2003 2:57 am
by twigletmac
Before the while loop what do you get for:
Code: Select all
echo '<p>Number of results: '.mysql_num_rows($filelist).'</p>';
Mac
Results
Posted: Fri Nov 14, 2003 12:12 pm
by gjb79
I modified the script you gave me slightly, just to show the results from both arrays.
Code: Select all
<?php
echo '<p>Number of results for Filelist: '.mysql_num_rows($filelist).'</p>';
echo '<p>Number of results for FiletoPagelist: '.mysql_num_rows($FiletoPagelist).'</p>';
?>
I get 2 for the filelist, and 1 for filetopagelist (as it only seems to save 1 in to the table.)
Everything appears to work, except that when I check multiple check boxes, it does not save the results from the first boxes, only the last one on the list. Does that make since? Example: lets say I have 3 items.
Item 1
Item 2
Item 3
If I where to check the boxes before all three of them and hit save, it only saves the check infront of number three.
If I where to check one and two, it will save number two. Or if I check only number one, two, or three and none of the other two, it will save which ever I do check.
I assume this is now a problem with the insert command and not with the looping?
Thank you so much!
Posted: Sat Nov 15, 2003 1:44 pm
by twigletmac
Does look like the inserts aren't working as expected - could you post the HTML code for the checkboxes? I think I know what's probably going wrong.
Mac
Here ya go.
Posted: Sun Nov 16, 2003 12:30 pm
by gjb79
Ok the table the check boxes are in contains a lot more info than just the check box and label. However I broke down everything to include just the form info and check box:
This is what the website html looks like when I view source from the webpage:
Code: Select all
<form action="/manage/manage/selectfiles.php?pageid=1" method="POST" name="addfiles" target="_self" id="addfiles">
<input name="selectedfile" type="checkbox" id="selectedfile" value="5" checked>
<input name="selectedfile" type="checkbox" id="selectedfile" value="6" checked>
</form>
Here is the php code that produces the above results:
Code: Select all
<?php do { ?>
<input name="selectedfile" type="checkbox" id="selectedfile" value="<?php echo $row_filelist['ID']; ?>" <?php if ( $row_FiletoPagelist['file'] == $row_filelist['ID']) {echo "checked";} ?>>
</form>
If it helps, here is the insert sql command. Generated by Dreamweaver MX btw.
Code: Select all
<?php
if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "addfiles")) {
$insertSQL = sprintf("INSERT INTO filetopage (page, file) VALUES (%s, %s)",
GetSQLValueString($HTTP_POST_VARS['pageid'], "int"),
GetSQLValueString($HTTP_POST_VARS['selectedfile'], "int"));
mysql_select_db($database_cms, $cms);
$Result1 = mysql_query($insertSQL, $cms) or die(mysql_error());
$insertGoTo = "selectfiles.php";
if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $HTTP_SERVER_VARS['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
} ?>
Obviously, the information is drawn from a table called filelist and to make sure it checks the box if it has been previously selected it compairs it with a secondary table called FiletoPagelist.
Thanks

Posted: Sun Nov 16, 2003 3:26 pm
by Paddy
Just a small note. If you want your code to be strict xhtml compliant you must put checked="checked" in your input tags. Attribute minimisation is a no no in xhtml.
Duh
Posted: Sun Nov 16, 2003 9:22 pm
by gjb79
It just occured to me that the problem I have is with the insert script. If multiple check boxes are checked, then the value of check box "selectedfile" becomes an array.
I need to loop the insert command to insert a new row for each value of the selectedfile array.
Any ideas on how to do this? I tried a do while script, but didn't work.
BTW thanks for the advice Paddy.
thanks
Posted: Sun Nov 16, 2003 10:02 pm
by Paddy
I am thinking what you want is a kind of shopping cart thing? Or something similiar. Ok, what I would do is this.
You have a user and you want to be able to assign multiple items or something to them depending on if they checked it or not. So have a table with a user or session id and an item id. The user can then check multiple things and you can store them. So if a user buys three things they take up three items in the database. If the user changes there mind and unchecks some items and/or adds some others then when they submit, delete all occurences of that user in the table and reinsert from the current checkboxes.
Hope that helps. What it seems to me you are trying to do is create a database that is not normalised. They are a lot harder to deal with in the end.
Oh, and if you don't know how to traverse a list of checkboxes here is some code I used to delete stuff.
Code: Select all
<?php
foreach ($deletebox as $deletechoice)
{
$result = mysql_query("delete from cart where pkey='".$deletechoice."'") or exit();
}
?>
$deletebox is your array of checkbox items and $deletechoice is instantiated in the foreach statement.
Posted: Mon Nov 17, 2003 6:54 pm
by gjb79
Well, what I am attempting to create is a content management system. The idea is that the page would dynamically list a selection of files (stored on the 'files' table) that are stored in an sql database.
The problem with the checkbox is that its value is that of the ID column in the 'files' table. The user would select the files that they want to save. The checked item values are saved to a new table called 'filetopage'. Now if the person decides they want to change thier selection: The check box should compair its value (from the 'files' table) with the value of of the saved items in the 'filetopage' table.
So if the items have a value of 1, 2, and 3, and the checked items saved in the 'filetopage' are 2 and 3, the page should mark the first check box with nothing, and the second and third check box with a check="checked".
So I'm dealing with a loop, 2 tables, and check boxes.
I also cannot seem to get the insert statement to insert a new row for each check box that has been checked.