Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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);
?>
share|improve this question
Read about variable scope: php.net/manual/en/language.variables.scope.php – str Apr 16 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. – ndasusers Apr 16 at 14:31

2 Answers

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']);
share|improve this answer
Null given (personlist). – Niek Jonkman Apr 16 at 14:01

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();
share|improve this answer
1  
+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. – Snifff Apr 16 at 13:48
unfortunately it is still placing a second (3rd,4th etc..) person array on index[0] of personlist causing the previous person to "dissolve". – Niek Jonkman Apr 16 at 13:56
Understood, i edited my answer to fit your issue. – Luigi Siri Apr 16 at 14:03
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 Apr 16 at 14:07
1  
@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. – roXon Apr 26 at 13:05
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.