AdrenalinMaster
AdrenalinMaster

Reputation: 31

PHP, using array_push or?

can anyone help me with a little problem, I have array with 1 value in it, and with every submit input I want to add +1 value to array, for example in array I have value [0] -> "Name1", when I press submit button I want to add another value so array would be [0] -> "Name1", [1] -> "Name2" and when I press again it adds Name3 and so on, I used array_push, but it only adds one value, and then just updates that one value, so, do you have any ideas how to make my code work properly? Thanks!

$users = array("Name1");
$_SESSION["add"] = $users;

if(isset($_GET["create"])) {
    $name = $_GET["name"];  

    array_push($_SESSION["add"], $name);

}

foreach($users as $acc) {
    echo $acc;
}

Upvotes: 0

Views: 89

Answers (3)

Shakil Hossain
Shakil Hossain

Reputation: 1743

Check here how its working i think it working as par you said : http://www.tutorialsscripts.com/php-tutorials/array-functions/php-array_push-function-demo.php

Upvotes: 0

Eakethet
Eakethet

Reputation: 682

You can make multiline form, which will represent every value from array.

echo '<input type="text" name="create[]" value="">';
foreach($_GET['create'] as $name)
{
    echo '<input type="text" name="create[]" value="$name">';
}

So you can iterate over each field in array, show it to user and let him edit entries and add new. You can access names as array in php ($_GET['names'], $_POST['names']);

Upvotes: 1

S. Ton
S. Ton

Reputation: 35

Keep an array in which you store your data in a session/cookies/database. When your script stops executing, this array doesn't exist anymore, so when you submit a form, the new array is created.

Upvotes: 0

Related Questions