|
Posted by Tyrone Slothrop on 08/14/05 03:06
On Sat, 13 Aug 2005 18:24:57 +0100, Peter Wilson
<pwilson@sci-comm.clara.net> wrote:
>Hi
>
>I am not sure I can see how this works sorry. What is the $i++ doing.
>I take it that the hidden field id is just there to help set up a count.
>Also would this work if I was inserting multiple fields?
>
>Many thanks for your help I am still earning and arrays are something I
>have never used before.
>
>>OK, I'll take a stab at this. I assume that when you say "lines" that
>>you mean records in a table. Most likely, the easiest way is to have
>>the field names post as an array, so you end up with a
>>multi-dimensional array.
>>
>>For example:
>><form method="post" action="a_url.php">
>><input type="hidden" name="id[]" value="1">
>><input type="text" name="field[]" value="cogs">
>><br>
>><input type="hidden" name="id[]" value="2">
>><input type="text" name="field[]" value="widgets">
>><br>
>><input type="submit" value="update">
>></form>
>>
>>
>>Then, on the receiving end:
>><?php
>>for ($i=0; $i<count($_POST['id']); $i++)
>> {
>> mysql_query ("UPDATE table SET field=\"{$_POST['field'][$i]}\" WHERE
>>id = '{$_POST['id'][$i]}'");
>> }
>>?>
Please do not top post. It makes it difficult for people to follow
the thread of the discussion.
First, an array is simply a variable with multiple keys and values. A
key is the name of the variable and the value is the associated value.
When you post a form, it contains multiple keys and values within the
$_POST array. To view what you are posting, create a form (or use the
one you have) and post to a page which contains the following code:
<pre>
<?php print_r($_POST); ?>
</pre>
This should reveal to you the contents of the $_POST array.
Next, applying the brackets to the name of the common variables allows
you to create another dimension to the array. That mean that for each
$_POST['id'] there can be multiple values as defined by the value of
the integer $i when you process the form. So you have:
<input type="hidden" name="id[]" value="1">
<input type="hidden" name="id[]" value="2">
<input type="hidden" name="id[]" value="3">
When the form is posted you get the following values:
$_POST['id'][0] = 1
$_POST['id'][1] = 2
$_POST['id'][2] = 3
(The first array element is always 0.)
Now, let's say you are generating this form from a table:
<form>
<?php
$qy = "SELECT * FROM table";
$rs = mysql_query ($qy) or die (mysql_error);
while ($r = mysql_fetch_array ($rs)) { ?>
<input type="hidden" name="id[]" value="<?=$r['id']?>">
<input type="text" name="field[]" value="<?=$r['field']?>">
<?php }
?>
<input type="submit" value="Update">
Using the print_r function code above to display the arrays posted you
can see what happens. Every $_POST['id'][$i] has a field associated
with $_POST['field'][$i] where $i has the same value.
So, when you finally get around to updating the records, you have to
consider all of the possible values of $i determined by counting how
many there are. this is defined by counting the number of
$_POST['id']'s, or count($_POST['id']).
Once we know how many possible values if $i there are, we are ready to
save the data. We do this by looping through all of the $_POST values
using a for statement:
for ($i=0; $i<count($_POST['id']); $i++) {
// update query here
}
Class is dismissed. ;-)
Navigation:
[Reply to this message]
|