how to handle multiple variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aneuryzma
Forum Contributor
Posts: 106
Joined: Sat May 17, 2008 7:03 am

how to handle multiple variables

Post by aneuryzma »

How can I refer to all variables of a POST string using PHP ?

$_POST['var0']
$_POST['var1']

what if I don't know the names var0, var1.. and how many they are ?

thanks
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: how to handle multiple variables

Post by Apollo »

Code: Select all

foreach($_POST as $k => $v) { print("['$k'] = $v, "); }
aneuryzma
Forum Contributor
Posts: 106
Joined: Sat May 17, 2008 7:03 am

Re: how to handle multiple variables

Post by aneuryzma »

thanks, I still have one question:

if this is my POST:
dropDown0=1&dropDown1=1&dropDown2=1
and this is my code:

Code: Select all

 
 
 
    $fname = "form.xml";
 
    $fhandle = fopen($fname,"w");
    
    foreach($_POST as $entry => $value) {
    
        if ($entry!="dataFormat") fwrite($fhandle, "<$entry> $value </$entry> \n");
    
    }
    
    fclose($fhandle);
 
 
 

why do my output is inverted ? (dropDown2 is before dropDown1)

Code: Select all

 
<dropDown0> 1 </dropDown0> 
<dropDown2> 1 </dropDown2> 
<dropDown1> 3 </dropDown1>
 
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: how to handle multiple variables

Post by Apollo »

No clue, probably depends on your webserver and PHP settings. But it's easily taken care of with

Code: Select all

ksort($_POST);
See also the manual.
Post Reply