You are here: Fw: [PHP] Problem with arrays « PHP « IT news, forums, messages
Fw: [PHP] Problem with arrays

Posted by Jeff Schmidt on 03/26/05 00:13

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@weldingengineering.com>
To: <virtualsoftware@gmail.com>
Sent: Friday, March 25, 2005 5:10 PM
Subject: Re: [PHP] 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@gmail.com>
> To: <php-general@lists.php.net>
> Sent: Friday, March 25, 2005 12:01 PM
> Subject: [PHP] 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!!!

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация