Click to See Complete Forum and Search --> : Remember Array Contents


Dysan
11-21-2007, 12:17 PM
Hi,

I have the following code, that inserts the ID value of a link into an array, upon the link being clicked. See example link below:


www.somthing.co.uk/functions.php?id=23 (www.somthing.co.uk/functions.php?id=23)

How do I get the array to remember its contents, so if the browsers back button is used, and another link is clicked, this link should append/add it's id to whats already inside the array?

What do I need to change in the following code in order for the array to remember its contents:

<?php

$array[] = $id;
print_r($array);

?>

jasonahoule
11-21-2007, 12:32 PM
HTTP is a stateless protocol. The server knows nothing of any previous calls. If you want to keep data between pages you will need to use sessions. I saw you using sessions in a different post. Just apply the same here.

For example, to modify what you had earlier...

$_SESSION['cart'][] = $_GET['id'];
print_r($$_SESSION['cart']);

Dysan
11-21-2007, 12:36 PM
Where did you go earlier?

jasonahoule
11-21-2007, 12:48 PM
Working...

Dysan
11-21-2007, 01:11 PM
how do I put the array in a session, so when when another link is clicked, on the another page, that link's ID should be added to whats already inside the array.

http://www.websitename/1.php?action=add&id=10
http://www.websitename/1.php?action=add&id=20
http://www.websitename/1.php?action=add&id=30

So if the 3rd then 1st, then 2nd link is clicked, the array should contain

30, 10, 20

<?php
$id = $_GET['id'];
$action = $_GET['action'];
switch ($action)
{
case 'add':

$array = array();
$array[] = $id;
print_r($array);
break;
}

?>

jasonahoule
11-21-2007, 01:28 PM
First, when working with arrays, the very first entry to your page should be session_start();


<?php
session_start();

$id = $_GET['id'];
$action = $_GET['action'];

switch ($action)
{
case 'add':

// Here we check to see if the product is already in the cart
// if it is then we increase the quantity
if(array_key_exists($id, $_SESSION['cart']))
{
$_SESSION['cart'][$id]++;
}
// This product is not in the cart yet so we add it with a
// quantity of 1
else
{
array_push($_SESSION['cart'], array($id=>1));
}
break;
}

?>

Dysan
11-21-2007, 01:49 PM
First, when working with arrays, the very first entry to your page should be session_start();


<?php
session_start();

$id = $_GET['id'];
$action = $_GET['action'];

switch ($action)
{
case 'add':

// Here we check to see if the product is already in the cart
// if it is then we increase the quantity
if(array_key_exists($id, $_SESSION['cart']))
{
$_SESSION['cart'][$id]++;
}
// This product is not in the cart yet so we add it with a
// quantity of 1
else
{
array_push($_SESSION['cart'], array($id=>1));
}
break;
}

?>

How do I display the contents of the array on the screen?

jasonahoule
11-21-2007, 02:22 PM
foreach($_SESSION['cart'] as $item => $quantity) {
echo $item.': '.$quantity.'<br/>';
}

Dysan
11-21-2007, 02:33 PM
Why doesn't the following code work?, I keep getting greeted with the following error message:

Warning: array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object in C:\Documents and Settings\User\Desktop\Xampp\htdocs\1.php on line 7.

<?php

session_start();

if (array_key_exists($_GET['id'], $array))
{
echo "Already Exists!";
}
else
{
$_SESSION['ids'][] = $_GET['id'];
$array = $_SESSION['ids'];
}

print_r($array);

?>

bokeh
11-21-2007, 02:36 PM
switch ($action)
{
case 'add':

// Here we check to see if the product is already in the cart
// if it is then we increase the quantity
if(array_key_exists($id, $_SESSION['cart']))
{
$_SESSION['cart'][$id]++;
}
// This product is not in the cart yet so we add it with a
// quantity of 1
else
{
array_push($_SESSION['cart'], array($id=>1));
}
break;
}
Or just do this:
switch ($action)
{
case 'add':
@$_SESSION['cart'][$id]++;
break;
}

jasonahoule
11-21-2007, 02:58 PM
You are getting that error because the variable $array is not defined.

Dysan
11-21-2007, 03:07 PM
Yes it is, in the else part of the if statement.

jasonahoule
11-21-2007, 03:29 PM
You are testing to see if the key is in $array inside the if, well before you set $array equal to $_SESSION['ids']. $array needs to be declared before you can perform any operations on it.

Dysan
11-21-2007, 03:42 PM
So how do I only add the id to the array, if it doesn't already exist inside the array, and if it does, display a message notifying the user that that id already exists?