Page 1 of 1

$_POST Variable List New bee question

Posted: Mon Aug 31, 2009 10:44 pm
by arhunter
I am fairly new to php and I was wondering if there is a function or object all that will return the following:

Total number of variables or array elements in the $_POST array.
The Variable Names in the $_POST array.

for example if $_POST contains 3 vars such as v1, v2, v3 is there a function that I can call someing like $Count=elements($_POST);

for example if v1 is called Name and v2 is Address and v3 is phone is there something like
$VarNames = array();

$VarNames = Varlist($_POST);

where $VarNames[0] would contain Name, [2] contain Address and [3] contain phone

so therefore instead of
$Name = $_POST['Name'];
$Address = $_POST['Address'];
$Phone = $_POST['Phone'];

I can
$Name = $_POST['$VarName[0]']
$Address = $_POST['$VarName[1]'];
$Phone = $_POST['$VarName[2]'];


I am new to this so please if this sounds like a novice cause I am thx

Re: $_POST Variable List New bee question

Posted: Tue Sep 01, 2009 3:38 am
by lord_webby

Code: Select all

 
$i=0;
foreach ($_POST as $key=>$value){
echo "$key has a value of $value");
$i++;
}
echo "There are $i items";
 
should work...

Re: $_POST Variable List New bee question

Posted: Tue Sep 01, 2009 5:22 am
by lord_webby
Better:

Code: Select all

 
$i=0;
while ($var = each($_POST)) { 
printf ("Key <b>%s</b> has the value of: <b>%s</b><br>", $var['key'], $var['value']); 
$i++;
}
echo $i." results total.";
 

Re: $_POST Variable List New bee question

Posted: Tue Sep 01, 2009 2:40 pm
by Darhazer
array_keys($_POST) will return array with the variable names, count($_POST) will return the count of the variables, foreach($_POST as $key => $val) {} is a loop that will give you both variable and value in the body of the loop