anointing Posted September 1, 2008 Share Posted September 1, 2008 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> ?> Link to comment https://forums.phpfreaks.com/topic/122221-problem-with-an-associative-array-in-a-shopping-cart/ Share on other sites More sharing options...
blinky001 Posted September 1, 2008 Share Posted September 1, 2008 In your test (other than the fact you don't use your session variable for the loops...) the line echo "<td class='normal'><a class='cartlinks' href='process.php?delete='" .$key. "'>Remove item[/url]</td>"; has the wrong quotes around the $key variable - you only need the double ones: Perhaps try: <?php session_start(); if(!isset($_SESSION['test'])) { $_SESSION['test'] = array( "1000" => "Name: Barbie Doll", "1001" => "Name: Action Man", "1002" => "Name: Humpty Dumpty" ); } $action = $_POST['action']; $cake_id = $_POST['cake_id']; $name = $_POST['cake_name']; $order = $_POST['order_details']; $price = $_POST['total_cost']; $quantity = $_POST['quantity']; echo '<table>'; if($_GET['action'] == "delete") { unset($_SESSION['test'][$_GET['id']]); foreach($_SESSION['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='dum.php?delete=".$key.">Remove item</td>"; echo "</tr>"; } } } ?> <tr><td class="totalprice">£<?php echo count($_SESSION['test']); ?></td></tr> </table> Hope this helps. Paul Link to comment https://forums.phpfreaks.com/topic/122221-problem-with-an-associative-array-in-a-shopping-cart/#findComment-631136 Share on other sites More sharing options...
anointing Posted September 1, 2008 Author Share Posted September 1, 2008 Hi, I've tried just using the double quotes but it has made no diffference. The real problem is that when I delete an element, then use the foreach method to display the array, the array does not subtract the deletion. It cycles through 3 times as before, displaying null values for the deleted key / value pairs of the supposedly deleted element. Very odd. Link to comment https://forums.phpfreaks.com/topic/122221-problem-with-an-associative-array-in-a-shopping-cart/#findComment-631332 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.