Another question about arrays

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
DSM
Forum Contributor
Posts: 101
Joined: Thu May 02, 2002 11:51 am
Location: New Mexico, USA

Another question about arrays

Post 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?
User avatar
fatal
Forum Contributor
Posts: 118
Joined: Sat Apr 20, 2002 10:47 am
Location: East Coast

Post by fatal »

concidering there is sytax errors with your for loop:
correct:
for($i = 0; $i < $num_rows; $i++)
{
statements
}
DSM
Forum Contributor
Posts: 101
Joined: Thu May 02, 2002 11:51 am
Location: New Mexico, USA

Post 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.
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post 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'
User avatar
fatal
Forum Contributor
Posts: 118
Joined: Sat Apr 20, 2002 10:47 am
Location: East Coast

Post by fatal »

i know that, i was just explain the syntax ofit, considering i thought it was wrong.
DSM
Forum Contributor
Posts: 101
Joined: Thu May 02, 2002 11:51 am
Location: New Mexico, USA

Post 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.
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post 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.
DSM
Forum Contributor
Posts: 101
Joined: Thu May 02, 2002 11:51 am
Location: New Mexico, USA

Post by DSM »

YUP 8O
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post 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
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

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