1

Could anyone explain me how to solve the following:

function GetSetClearForm(){

   $person = array('firstname' => $_POST["fname"], 'lastname' => $_POST["lname"],'age' => $_POST["age"] ,'city' => $_POST["city"] ,'zipcode' => $_POST["zcode"],'address' => $_POST["address"]);
   print_r($person);
   $personlist = array();
   array_push($personlist,$person);
   print_r($personlist);

   return $personlist;
}

When the print_r($personlist); has ran I get the following output:

 Array ( 
    [0] => Array ( 
       [firstname] => 2 
       [lastname] => 2 
       [age] => 2 
       [city] => 2 
       [zipcode] => 2 
       [address] => 2 
       ) 
    ) 

(filled in all textboxes with "2").

This is ok at this point, but whenever I fill in another one I get this output:

Array ( 
    [0] => Array ( 
           [firstname] => 1 
           [lastname] => 1 
           [age] => 1 
           [city] => 1 
           [zipcode] => 1 
           [address] => 1 
           ) 
     )

(filled in all textboxes with "1").

So instead of creating another person on a new index (index[1]) it replaces index[0] with a new person and deletes the older one. I dont want it do delete it I want to get a list full of people. I think it has to do with the array_push but I am not sure I hope anyone could help me out here.

EDIT:

Added the index.php:

<form action="check.php" method="POST">
<table>
<tr><td>First name</td><td><input type="text" name="fname"></td></tr>
<tr><td>Last name</td><td><input type="text" name="lname"></td></tr>
<tr><td>Age</td><td><input type="text" name="age"></td></tr>
<tr><td>City</td><td><input type="text" name="city"></td></tr>
<tr><td>Zipcode</td><td><input type="text" name="zcode"></td></tr>
<tr><td>Adress</td><td><input type="text" name="address"></td></tr>
<tr><td></td><td><input type="submit" value="Submit"></td></tr>
</table>
</form>

Added the check.php

<?php
include("functions.php");

$personlist = array();
$personlist[] = GetSetClearForm();
print_r($personlist);
?>
2
  • Read about variable scope: php.net/manual/en/language.variables.scope.php Commented Apr 16, 2013 at 13:40
  • You are stil re-iniializing person list. it will keep adding them to [0] each time the script runs. It is why I put them in the seesion. But as stated below, you might be better off saving each form entry to a database or file. Commented Apr 16, 2013 at 14:31

2 Answers 2

2

Instead of using array_push you can use this:

$personlist = array();
$personlist[] = $person;

This way a new index is created every time you 'insert' a new value, in this case an array.

EDIT:

You need to declare and fill your $personlist array outside your function. That said:

function GetSetClearForm(){

   $person = array('firstname' => $_POST["fname"], 'lastname' => $_POST["lname"],'age' => $_POST["age"] ,'city' => $_POST["city"] ,'zipcode' => $_POST["zcode"],'address' => $_POST["address"]);
   print_r($person);
   return $person;
 }

 $personlist = array();
 $personlist[] = GetSetClearForm();
Sign up to request clarification or add additional context in comments.

7 Comments

+1, actually, it is stated even in php manual: *Note*: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.
unfortunately it is still placing a second (3rd,4th etc..) person array on index[0] of personlist causing the previous person to "dissolve".
After edit: still replaces it, Array (personlist) [0] fname = 1 lname = 2 etc.... adding second user, Array (personlist) [0] fname = 2 lname = 2 , person 1 is gone
@Niek Jonkman, Are you running the script after every POST action? If that's the case, the personlist array will always be declared as a new one.
@NiekJonkman you sohuld accept (checking the "V" mark near) the Answer that helped you in your problem. It's a way to say "thanks" here on S.O.
|
0

I think the problem is you are re-creating person list each time the function is called. Maybe you can solve it like this:

//Create your person list some where else and keep it alive.
//Like in a session? 
session_start();


//Then give your keep alive array to your function each time.
function GetSetClearForm(){

$person = array('firstname' => $_POST["fname"], 'lastname' => $_POST["lname"],'age' => $_POST["age"] ,'city' => $_POST["city"] ,'zipcode' => $_POST["zcode"],'address' => $_POST["address"]);
print_r($person);

return $person;
}


$_SESSION['personlist'][] = GetSetClearForm() ;   
print_r( $_SESSION['personlist']);

Comments

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.