0

On a previous page, I have gotten the names and the number of playeys values from the user.

I wish to fill them into an array. Here is my code so far:

<?php

$numberOfPlayers = $_POST['numberOfPlayers'];
$counter = 1;
$playerName = array();

while($counter<=$numberOfPlayers-1){
$playerName[$counter-1]= $_POST[$counter];
$counter=$counter+1;
}  

print_r($playerName);

?>

However it is throwing out an error saying "Undefined offset: 1 in C:\wamp\www\test.php on line 8".

Any help would be much appreciated.

Thanks

1
  • 1
    $_POST['1'] does not exist. Commented Dec 14, 2012 at 19:40

1 Answer 1

3

Just use array_push(). or [] array short hand. No need to use a counter.

while($numberOfPlayers--){
    if(isset($_POST[$numberOfPlayers]))
        $playerName[]= $_POST[$numberOfPlayers];
}
Sign up to request clarification or add additional context in comments.

5 Comments

This isn't exactly what I want. I need to populate the array so I need the counter. There are many player names, but the amount of players vary depending on what the user has chossen on the previous screen.
Does $_POST['numberOfPlayers'] contain the total number of players or total selected number for players?
It contains the total number of players.
This also doesnt work. This would only assign the numberofplayer to the playerName array. I want the names stored in the playerName array. Thanks for the help though man.
could you show what does var_dump($_POST) contain. You must show your data layout. Otherwise I can not code just blindly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.