Fw: Problem with arrays

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I sent this to the original user, but forgot to CC it to the list. I'm just sending this now, for completeness.
----- Original Message ----- From: "Jeff Schmidt" <jsbiff@xxxxxxxxxxxxxxxxxxxxxx>
To: <virtualsoftware@xxxxxxxxx>
Sent: Friday, March 25, 2005 5:10 PM
Subject: Re: Problem with arrays



Hello there.

Well, let me start with a pointer to some documentation. I think you will find the following to be helpful:
http://www.php.net/manual/en/control-structures.foreach.php


In a nutshell, when your form is submitted, you will have present in the $_POST super-global array, a reference to an array representing position[pos].
(As an aside, why are you using the same value for the first key in each of the fields? What you will end up with is an array that looks like:
array( "pos" => array("1" => "number 1", "2"=>"number 2", etc. . .))
Which seems like an extraneous level of nesting to me. . .)


You can use foreach to walk through that array like so:

foreach ($_POST['position']['pos'] as $key => $value)
{
//some code to update the database here.
}

NOTE: your $value from the form, in this case, corresponds to $key in the code above.

That is, even though you are calling it $value in your original post, what you really are setting is a key in an associative array in PHP. Anyhow, back to the example code. This structure will loop through the array of form elements that were submitted, and each time it loops, it will set $key and $value to the submitted key and submitted value respectively, once for each text input.

So, looking at your form below, let's say that the first text field has a $value of 1, second has $value of 2, etc.

That is, the form gets sent to the user's browser like this:

<form action="products.php" method="post">
<input name="position[pos][1]" type="text" id="position[pos][1]" value="number 1" />
<!-- etc -->
</form>


The first time our foreach loop executes, $key will be assigned the value "1", and $value will be assigned the value "number 1" (or whatever the user put into the text field, as they probably have changed it).

So, bringing it all together, the code will look something like this:

foreach ($_POST['position']['pos'] as $key => $value)
{
  $key = mysql_real_escape_string($key);
  $value = mysql_real_escape_string($value);
  $query = "UPDATE TABLE tablename SET value='$value' WHERE id='$key'";
 //code to actually execute the query goes here.
}

You can call the local values that foreach assigns to something other than $key and $value, but that is kind of a convention, and helps to keep the code clear.Also, I'd just like to throw out here, as a suggestion, in case you aren't already aware of this, that since user-input isn't trustworthy, before constructing your query string, you should make $key and $value 'safe' by using something like mysql_real_escape_string(), as in the example above.

Hope this helps.

Jeff Schmidt

----- Original Message ----- From: <virtualsoftware@xxxxxxxxx>
To: <php-general@xxxxxxxxxxxxx>
Sent: Friday, March 25, 2005 12:01 PM
Subject: Problem with arrays



Hi,
I have a form like this:
<form action="products.php" method="post">
<input name="position[pos][$value]" type="text" id="position[pos][$value]" value="number 1">
<input name="position[pos][$value]" type="text" id="position[pos][$value]" value="number 2">
<input name="position[pos][$value]" type="text" id="position[pos][$value]" value="number 3">
</form>


Note that $value from position[pos][$value] is different in all 3 fields.

For each element of the array i want to update the value in the database. For example, for each value of the position[pos][$value] i want to update the database with the specific number.
Something like this:


$query = "UPDATE table SET value = 'number 1'  WHERE id='$value'";

Thanks in advance for your help!!!

-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux