Problem With an Associative Array in Shopping Cart
Posted: Mon Sep 01, 2008 8:19 am
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>
?>
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>
?>