Page 1 of 1

Problem With an Associative Array in Shopping Cart

Posted: Mon Sep 01, 2008 8:19 am
by anointing
Hi,

I'm, designing my first simple php shopping cart. Ultimately, I want the cart to add and delete items contained in an associative array. I'm currently testing using a simple associative array, $test which is initialized with 3 elements. A form calls process.php which displays the contents of $test. When an item is selected for deletion, process.php is again called with info added to the url: href='process.php?action=delete&id=" .$key. "'...

Process.php displays the item reference, the item name and a link to delete the item. This is what it displays on the first call as it loops through the 3 elements of the array:

Cake reference: BCL-1000 Name: Barbie Doll Remove item Remove item
Cake reference: BCL-1001 Name: Action Man Remove item
Cake reference: BCL-1002 Name: Humpty Dumpty Remove item

Process.php then deletes the item from the array using unset(). It then loops through $test using a foreach function and displays the contents of the array.

When I select the first link which deletes item id=1000, this is what I get:

Cake reference: BCL- Remove item
Cake reference: BCL-1001 Name: Action Man Remove item
Cake reference: BCL-1002 Name: Humpty Dumpty Remove item

The curious thing is, even after the item is deleted, the foreach method loops through 3 times as though there are still 3 elements in the $test array. But it echos 2 using <?php echo count($test); ?>

I find this really strange and frustrating. Guys, I need your help. Here's the coding:

==========

<?php
session_start();

$test= array("1000" => "Name: Barbie Doll", "1001" => "Name: Action Man", "1002" => "Name: Humpty Dumpty");
$_SESSION['test'] = $test;

$action = $_POST['action'];

$cake_id = $_POST['cake_id'];
$name = $_POST['cake_name'];
$order = $_POST['order_details'];
$price = $_POST['total_cost'];
$quantity = $_POST['quantity'];

?>
.
.
.
<?php

if($_GET['action']=="add")
{

foreach($test as $key=>$value)
{
echo "<tr bgcolor='#ffffff'>";
echo "<td class='normal'>Cake reference: BCL-" .$key. "</td>";
echo "<td class='normal'>" .$value. "</td>";
echo "<td class='normal'><a class='cartlinks' href='process.php?action=delete&id=" .$key. "'>Remove item</a></td>";
echo "</tr>";
}
}

if($_GET['action']=="delete")
{

unset($test[$_GET['id']]);

foreach($test as $key=>$value)
{
if($key != "")
{
echo "<tr bgcolor='#ffffff'>";
echo "<td class='normal'>Cake reference: BCL-" .$key. "</td>";
echo "<td class='normal'>" .$value. "</td>";
echo "<td class='normal'><a class='cartlinks' href='process.php?delete='" .$key. "'>Remove item</a></td>";
echo "</tr>";
}
}


}


?>

<?php
<tr><td class="totalprice">£<?php echo count($test); ?></td></tr>
?>

Re: Problem With an Associative Array in Shopping Cart

Posted: Thu Sep 04, 2008 11:04 am
by anointing
PROBLEM FIXED


I now use $_SESSION[cart][$key] associatve array to keep track of updates.

A) ADDING ELEMENTS
I add updates as follows:

$_SESSION[cart][$key] = $value

B) DELETING ELEMENTS
I delete as follows:

if($_GET['action']=="delete")
{
// use unset method to rmeove item using the array's key ($key)
unset($_SESSION['cart'][$_GET[$key]]);
// then loop through the array to dispay items
foreach($_SESSION['cart'] as $key=>$value) {
// display items in the array
echo "<td class='normal'>Item reference: " .$key. "</td>";
echo "<td class='normal'>Value of item " .$value. "</td>";
}
}

Re: Problem With an Associative Array in Shopping Cart

Posted: Thu Sep 04, 2008 11:22 am
by anointing
A FURTHER POINT

I read somewhere that $_SESSION['cart'] is prone to memory issues and the php engine sometimes loose it in memory. To work around this, declare and initialise an array and use that array as the session array for your cart:

$obj;
if(!$obj)
{
$obj=array();
$_SESSION['cart']=$obj;
}

// proceed to use $_SESSION['cart'] and add and delete items using it