Tibby
Tibby

Reputation: 344

loop php $_POST array names

I would like to loop 5 arrays in post.
What should be the right code to display:
$_POST["fp0"]
$_POST["fp1"]
..
$_POST["fp5"]
in a loop?

$x = 0;
while($x <= 5) {
 $fp = ${'_POST["fp'.$x.'"]'};
 echo $fp.'<br>';
 $x++;
}

Upvotes: 0

Views: 58

Answers (3)

Abdallah Alsamman
Abdallah Alsamman

Reputation: 1672

0 to 5 are six numbers, so you want to loop 6 elements in an array,

this is what you want

for( $i = 0; $i <= 5; $i++ ){
    echo $_POST['fp'.$i]."<br>\n";
}

Upvotes: 1

Noman
Noman

Reputation: 4116

You can do it simply by for loop, you form must POST values for

fp0, fp1 .... fp5

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

Upvotes: 0

Ahmad
Ahmad

Reputation: 5760

Try this:

$x = 0;
while($x <= 5) {
 $fp = $_POST["fp$x"];
 echo $fp.'<br>';
 $x++;
}

Upvotes: 0

Related Questions