Trouble with looping/selected check boxes.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

Trouble with looping/selected check boxes.

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

Strange

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

Nothing

Post 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!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

Results

Post 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!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

Here ya go.

Post 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

&lt;form action="/manage/manage/selectfiles.php?pageid=1" method="POST" name="addfiles" target="_self" id="addfiles"&gt;
&lt;input name="selectedfile" type="checkbox" id="selectedfile" value="5" checked&gt;
&lt;input name="selectedfile" type="checkbox" id="selectedfile" value="6" checked&gt;
&lt;/form&gt;
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 :)
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post 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.
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

Duh

Post 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
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post 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.
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

Post 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.
Post Reply