RE: [PHP] trouble with each() list() and array()

From: Date: Mon, 20 Nov 2000 13:22:33 +0000
Subject: RE: [PHP] trouble with each() list() and array()
Groups: php.general 
Request: Send a blank email to php-general+get-26257@lists.php.net to get a copy of this message
At 11:15 20/11/00 +0100, you wrote:
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!!
well... thanks a lot, now I got it! You can explain much better than the manual. Is hard for me to "forget those damned poniters" since in C, pointers are everything. It's hard to let them go. Now I got what an array is, and what list and each does. But somethings are still amiss. Array's are not dynamic are they? I can't fill an array within a loop right? like in C for(i=0;i<50;i++) vet[i]=something; I can't feed an array in PHP right? and how do I create a list in memory? Like, if I have a text file with each line containing name|lastname|email in C I'd create a struct called X typedef struct X { char *name; char *lname; char *email; } and just loop around the file until EOF feeding a list of X, right? I didn't quite get how to do things like that in PHP. The same problem with binary files, how do I read them without structs?? I hope those aren't stupid doubts. thanks again ____________________________ . Christian Dechery (lemming) . http://www.tanamesa.com.br . Gaita-L Owner / Web Developer

« previous php.general (#26257) next »