Welcome to WebmasterWorld Guest from 54.237.251.98

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Accessing the $_POST array

A more effiencient way of accessing the $_POST array needed

     
11:57 am on Oct 1, 2003 (gmt 0)

New User

10+ Year Member

joined:Sept 21, 2003
posts:9
votes: 0


Hello

I have simply got hundreds of varibles that I need to access from the $_POST array. Is there an efficient way of doing the below:

$langactx = $_POST['langactx'];
$langc = $_POST['langc'];
$langcplusplus = $_POST['langcplusplus'];
$langcob = $_POST['langcob'];
$langdcom = $_POST['langdcom'];
$langdelphi = $_POST['langdelphi'];
$langfort = $_POST['langfort'];
$langjava = $_POST['langjava'];
$langjavabeans = $_POST['langjavabeans'];
$langoracledev = $_POST['langoracledev'];

You get the idea. I don't fancy writing hundreds more statements in this way, there must be a better way.

Thanks for any help

12:07 pm on Oct 1, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Feb 4, 2002
posts:5044
votes: 0


What is it you want to do with them?

Example


print('some stuff'.$_POST['stuff'].'here');

...and welcome to WebmasterWorld!

Nick

12:25 pm on Oct 1, 2003 (gmt 0)

New User

10+ Year Member

joined:Sept 21, 2003
posts:9
votes: 0


They are going to be used with the mail() function to send information via email.

Each variable will contain " ", "B", "I", and "A".

And I want to pass the variables through the varbia function below

function varbia($test) {
if ($test == "B") {
echo "Beginner";
} elseif ($test == "I") {
echo "Intermediate";
} elseif ($test == "I") {
echo "Advanced";
}

I was thinking that if I had all the variables accessable without the $_POST array calling on each variable I'd find it easier to program the html output I want for the email.

12:33 pm on Oct 1, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Feb 4, 2002
posts:5044
votes: 0


I expect there is a clever way using variable varibles, and a foreach loop, unfortunatley I'm rather crap at variable variables ;-)

One thing of value I might add though, is this:


switch($test) {
case "B":
print('Beginner');
break;
case "A":
print('Advanced');
break;
default:
print('Fill in the form dummy!');
break;
}

More here:
[dk2.php.net...]

So, anyone know how to do the variable variable thing or have another way to do it?

Nick

12:54 pm on Oct 1, 2003 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12541
votes: 1


Is there an efficient way...

extract($_POST);
1:12 pm on Oct 1, 2003 (gmt 0)

New User

10+ Year Member

joined:Sept 21, 2003
posts:9
votes: 0


About using varible varibles, here's a way but I don't understand it too well as i'm new to programming and PHP

while(list($index, $value)=each($_POST))
{ $$index = $value;
}

i'm unable to make this work though, but I'm sure I'm on to something here.

...and thanks nick for the advice on my function

...and coopster the extract() function will help me out if I need to deal with the array in such a way

1:24 pm on Oct 1, 2003 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12541
votes: 1


As far as a possible variable variables [us4.php.net] solution:

<?php
foreach (array_keys($_POST) as $key) {
$$key = $_POST[$key];
print "$key is ${$key}<br />";
}
?>

1:31 pm on Oct 1, 2003 (gmt 0)

New User

10+ Year Member

joined:Sept 21, 2003
posts:9
votes: 0


Nice one coopster this is just what I am looking for.

Thanks ;)

3:58 pm on Oct 1, 2003 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12541
votes: 1


I guess I could try to explain the variable variables solution a bit since, as Nick_W says, it can be a little confusing. First, here is a simple form:


<html><head><title>Variable Variables</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head><body>
<?php
if ($_POST['Submit']) {
foreach (array_keys($_POST) as $key) {
$$key = $_POST[$key];
print "$key is ${$key}<br />";
}
}
?>
<form action="<?php print $_SERVER['PHP_SELF'];?>" method="post">
First Name: <input type="text" name="firstname" /><br />
Middle Name: <input type="text" name="middlename" /><br />
Last Name: <input type="text" name="lastname" /><br />
<input type="submit" name="Submit" value="Submit" />
</form></body></html>


Next, let's pull apart the PHP:

  1. foreach (array_keys($_POST) as $key) {
  2. $$key = $_POST[$key];
  3. print "$key is ${$key}<br />";

  1. The array_keys will pull every POSTed HTML form input name and process it as $key, therefore the first loop through, $key contains 'firstname'.
  2. $$key is then created as a variable variable by using two dollar signs. A variable variable takes the value of a variable and treats that as the name of a variable, kind of "on-the-fly" like. At this point two variables have been defined and stored in the PHP symbol tree: As we said in step 1, $key with contents "firstname" and now we also have $firstname with contents "whatever_the_user_keyed" since we are pulling the value from the $_POSTed variable. So we are really seeing $$key as $firstname without even having to know there was a form input name of 'firstname'! Neither do we need to specifically set the $firstname variable in our PHP program since PHP already did it for us!
  3. OK, why can't I just print out $$key? What's with the braces {}? Ah, the million-dollar question. You can use $$key! You need to move it outside of double quotes, though, for example:

    print "$key is " . $$key . "<br />";

    It will work no problem as long as it is not an array. From the manual:

    In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.

    For this reason alone I prefer to keep in the practice of using braces {} when working with variable variables. It's just a good habit, in my own humble opinion.



Now, be cautious processing variables this way as you should ALWAYS check user-provided input. However, in your particular case, neoBinary, you stated that the form variables will always contain "B", "I", or "A", therefore I'm assuming you are somehow "scrubbing" your user-supplied input before using it -- just don't forget to add it to you loop processing here.

You might also have asked, well why can't I just use ${$_POST}? Because the manual specifically warns against this:

Please note that variable variables cannot be used with PHP's Superglobal arrays. This means you cannot do things like ${$_GET}.

Hope this helps anyone confused by variable variables -- coopster
4:19 pm on Oct 1, 2003 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


very nice coopster,

variable variables are a powerful and useful solution. I also must add that regardless of the best instruction they will still completely mess you up when you start using them more. Once you get them figured though you will wonder why you didn't always use them.

4:23 pm on Oct 1, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Feb 4, 2002
posts:5044
votes: 0


Wow! Star post Coopter, thanks!

I knew it could be done that way but just didn't have the knowledge ;-)

Will re-read and try to get it now...

Nick

5:22 pm on Oct 1, 2003 (gmt 0)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8326
votes: 155


Another reason to use braces. This is from the user comments on the online php manual (comment by frank at vista dot com)


Variable variables let you assign values to invalid variable names (those with hyphens, dots, etc.).

$foo = "invalid-variable";
$$foo = "value of invalid variable";

How do you access them once you've done this?

echo ${"invalid-variable"}; // value of invalid variable

Pretty cool.

Tom