RE: [PHP] trouble with each() list() and array()
| From: | MR | Date: | Mon, 20 Nov 2000 10:15:55 +0000 |
| Subject: | RE: [PHP] trouble with each() list() and array() | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-26235@lists.php.net to get a copy of this message | ||
Christian Dechery ...
> How do I store all the form data in a list for example?
All the form data is already in a list on PHP!!!
Do you know what $HTTP_POST_VARS is? It's a big array that stores the names and values of the
submitted form!!
Well, as you come from C, i may tell you a thing: forget that pointers. In PHP, everything has a
name. (personally i've found it
easier).
Well, let's do the typical example of how list() and each() work... put this on the php page
where the form is submitted to:
while ( list($name,$value) = each($HTTP_POST_VARS) )
{
echo $name;
echo " - ";
echo $value;
echo "<BR>";
}
Well, let's see how the hell do list() and each() and HTTP_POST_VARS work... you got this on
your page:
<FORM ACTION="receive_data.php" METHOD=POST>
First name:<INPUT NAME="first_name"> <BR>
Last name: <INPUT NAME="last_name"> <BR>
Adress: <INPUT NAME="adress"> <BR>
<INPUT TYPE=SUBMIT>
Well. Now, you type the data in the three fields, and press the submit button...
What the hell is created when receive_data.php is being parsed??
- a variable called $first_name
- a variable called $last_name
- a variable called $adress
- three arrays within the array called $HTTP_POST_VARS:
- first array within: ( name=>"first_name" ,
value=>"whatever_you_put_in_the_input_field" )
- first array within: ( name=>"last_name" ,
value=>"whatever_you_put_in_the_input_field" )
- first array within: ( name=>"adress" ,
value=>"whatever_you_put_in_the_input_field" )
Note that in PHP, the arrays can be referenced by name OR by number... the normal thing is think
about "numbered" arrays:
$array[0] = 5423;
echo $array[0];
but you can use names into the arrays:
$john[first_name] = "John";
$john[last_name] = "Jones";
In PHP you have no records at all, you must use that kind of arrays. That is damn pretty simple,
believe me.
Let's return with my example of the three input fields...
Let's analize this line of code:
while ( list($name,$value) = each($HTTP_POST_VARS) )
First of all, it's a while sentence, with an asignment. As long as each() can be asigned to
list(), it will loop. OK?
Second. each($HTTP_POST_VARS) will return you a array - with the name of the index and the content.
How does this work? Imagine the
variable $john. the first time you do each($john), it will return
("first_name","John"). The second time you do each($john), it'll
return ("last_name","Jones"). Got it?
Third. list($name,$value). That allows you to store the contents of an array into separate
variables. You know, each($john) returns
("first_name","John"). So list($name,$value) = each($john) will store
"first_name" into $name and "John" into $value. Got it?
If applied to the input fields, $name will store first_name, $value will store the thing you typed
into the first input field.
Hope this helps... And forget that damned pointers!!