PDA

View Full Version : Unknown, but numberd variables, how to get from $_POST


chrisvmarle
05-22-2003, 09:08 PM
I have form that sends an unknown number of variables to a .php page.
The part that my question is about sends the following variables:
f_1, f_3, f_7, f_10 etc...
But I don't know what the last is and I don't know the exact numbers, but it always is "f_" + number

Does anyone know how to get the name and value of these variables from $_POST?

Thanks in advance
Mzzl, Chris


I'm sure there's a way to go through all the items in $_POST in a for or while loop, if someone could tell me how to to this I think I'm able to do the rest.

I mean something like this:
while($current = get_next_item_from($_POST)) {
echo $current . " = " . $_POST[$current];
}

Spookster
05-22-2003, 11:04 PM
Well you pretty much have the idea. $_POST is just an array like any other. Just loop through it like you would any other array. You can use a for loop since you can determine the number of elements in the array



<html>
<body>

<?php

for($i = 0; $i < sizeof($_POST); $i++){
echo $_POST[$i] . "<br>";
}

?>

</body>
</html>



im typing this in a hurry as I am on my way out the door but should be pretty close to correct.

STDestiny
05-23-2003, 01:41 AM
Here's a really simple way to take any Post or Get variables from the URL. It will turn any variable in the URL into a variable you can use in your scripts


<?
foreach ($_POST as $key=>$val) {
$$key = "$val";
}
foreach ($_GET as $key=>$val) {
$$key = "$val";
}
?>


If you know what the variable is going to be (especially if you want to set a default variable when it doesn't exist) you can use this. (It's from an Image gallery script on my site.)


<?
if (isset($_GET['gallery'])) {
$gallery = $_GET['gallery'];
} else {
$gallery = "vacation";
}
?>


Or you could combine the two:


<?
foreach ($_POST as $key=>$val) {
$$key = "$val";
}
foreach ($_GET as $key=>$val) {
$$key = "$val";
}

if (!isset($gallery)) {
$gallery = "vacation";
}
?>


Hope those help!

firepages
05-23-2003, 03:25 AM
my version of events assumes you kow the max possible number...


<?
$max_num = ( $_POST['max_num'] ) ? $_POST['max_num'] : 20 ;
while( $x < $max_num ){
++$x;
if( $_POST['f_' . $x] ){
echo 'f_'. $x .' = ' . $_GET['f_'.$x] .'<br />' ;
}
}
?>

Weirdan
05-23-2003, 09:41 AM
foreach ($_POST as $key => $val)
if (strpos($key,"f_")===0)
$$key = "$val";


Is it contest? huh? ;)

chrisvmarle
05-23-2003, 09:41 AM
THANKS, All of you!
I'm sure there's a script I can use.

Mzzl, Chris