Page 1 of 1

Another question about arrays

Posted: Tue May 07, 2002 6:27 pm
by DSM
Have an array question,

I am trying to update a group of things at once.
All the fields update except for a checkbox that I use to set the item to display or not.

here's a snippit:

Code: Select all

$num_rows = count($fid);
	
for($i = 0; $i < $num_rows; ++$i):

$update = "UPDATE faqs SET question = "$question&#1111;$i]", answer = "$answer&#1111;$i]", display = "$display&#1111;$i]" WHERE fid = "$fid&#1111;$i]"";
$result = mysql_query($update) or die ("Could not update");
		
endfor;

Here's the display checkbox code setup:

if($display == yes):
echo"<input type = checkbox name = display&#1111;] value = yes checked>";
else:
echo"<input type = checkbox name = display&#1111;] value = yes>";
endif;
Anyone have any idea why the checkbox does not update properly?

Posted: Tue May 07, 2002 8:09 pm
by fatal
concidering there is sytax errors with your for loop:
correct:
for($i = 0; $i < $num_rows; $i++)
{
statements
}

Posted: Tue May 07, 2002 8:52 pm
by DSM
the loop works as is, I am able to loop thru the other form elements and have them update. Its just the checkboxes that don't update.

Posted: Wed May 08, 2002 12:22 am
by dusty
fatal, $i++ and ++$i will do the same thing.

but dsm, everything seems to check out ok from what you posted..is there any other parts of the code that deals with 'display'

Posted: Wed May 08, 2002 10:56 am
by fatal
i know that, i was just explain the syntax ofit, considering i thought it was wrong.

Posted: Wed May 08, 2002 11:05 am
by DSM
Dusty:

Nope, its a fairly simple update. The form has 4 fields:
  • fid[] (faq id number)- hidden
    question[]
    answer[]
    display[]
Once the "Update" button is hit, the loop works fine for the question[] & answer[] fields, just when you check or uncheck the display checkbox, it does not update the proper faq, it starts from the bottom and works it way up the list.

ex:

FAQ 1 - display checkbox is checked
FAQ 2 - display checkbox is checked
FAQ 3 - display checkbox is checked

Uncheck FAQ 1 click "Update", script loops thru and FAQ 3 becomes unchecked while FAQ 1 remains checked.

Posted: Wed May 08, 2002 11:27 am
by dusty
ahh i see what you're saying now, when you use blah[] in your input fields it only pushes the array, so:

<input type="checkbox" name="blah[]">
<input type="checkbox" name="blah[]">

if you check both boxes, it will be: array('0'=>'on','1'=>'on')
if you check the first OR the second: array('0'=>'on')

it doesn't skip an int for the unused checkboxes.

Posted: Wed May 08, 2002 12:00 pm
by DSM
YUP 8O

Posted: Thu May 09, 2002 3:42 am
by mikeq
Hi,

I have done something similar in the past. I gave each checkbox a unique name i.e.

<input type="checkbox" name="chk-faq1">
<input type="checkbox" name="chk-faq2"> etc.

When the form gets posted I looped through the $HTTP_POST_VARS array (I think that is what its called), I used the pos function to determine if 'chk' was at the beginning of the field name (to distinguish from other fields) then use the explode function to passing '-' as the separator which gave me 'faq1' in $myexplodedarray[1] etc.

Hope I have explained it well enough, I can probably post a bit of sample code if you need it.

Mike

Posted: Thu May 09, 2002 10:34 am
by dusty
that would probably work, but alot of useless code, if you want the checkbox as an array just give the int rather than leaving it blank.

Code: Select all

<input type="checkbox" name="display&#1111;0]">
<input type="checkbox" name="display&#1111;1]">
<input type="checkbox" name="display&#1111;4]">

etc.

while(list($key, $value)=each($display)) &#123;
  echo "$key - $value<br>";
&#125;

would yield: 

0 - on
1 - on
4 - on
(assuming all 3 are checked)