Nitish
Nitish

Reputation: 2763

Creating a dynamic array show only the last

I have a list of links similar to below:

<a href="array_add.php?n=1">1</a>
<a href="array_add.php?n=2">2</a>
<a href="array_add.php?n=3">3</a>
<a href="array_add.php?n=4">4</a>

And the array_add.php is :

$myarray = array();
$number = $_GET['n'];
$myarray[] = $number;
var_dump($myarray);

But the above var_dump displays only the last number received from GET. But I need an array of the numbers clicked.

Upvotes: 0

Views: 36

Answers (1)

ReeCube
ReeCube

Reputation: 2607

session_start();

if (isset($_SESSION['myarray'])) {
   $myarray = $_SESSION['myarray'];
} else {
   $myarray = array();
}
if (isset($_GET['n'])) {
   $number = $_GET['n'];
   $myarray[] = $number;
   $_SESSION['myarray'] = $myarray;
}

Upvotes: 2

Related Questions