PHP shopping cart array

So I have my cart set up to receive specific information about each item added, using the GET function. However in the implementation of reading the database, the values will simply become the same if you add in another item. If i add chair 1, then chair 1 again, it adds to chair 1’s total count saying there are 2 chair 1’s. But if I then add chair 2, there will be a new entry but with all the values of chair one.

array output

Array ( [0] => Array ( [item_id] => 2 [quantity] => 1 ) [1] => Array ( [item_id] => 4 [quantity] => 7 ) )

the item page :

[php]<?php
include_once(‘config/database.php’);
include_once(‘object/chair.php’);
$database = new Database();
$conn = $database->getConnection();
$chair = new Chair($conn);
$chair->id = $_GET[‘detailsid’];
$stmt = $chair->readDetails();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
?>[/php]

Add to cart:

Cart fucntion:
[php]

<?php session_start(); error_reporting(E_ALL); ini_set('display_errrors', '1'); include_once 'includes/db_conx.php'; if (isset($_POST['pid'])) { $pid = $_POST['pid']; $wasFound = false; $i = 0; if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { $_SESSION["cart_array"] = array(1 => array("item_id" => $pid, "quantity" => 1)); } else { foreach ($_SESSION["cart_array"] as $each_item) { $i++; while (list($key, $value) = each($each_item)) { if ($key == "item_id" && $value == $pid) { array_splice($_SESSION["cart_array"], $i - 1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1))); $wasFound = true; } } } if ($wasFound == false) { array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1)); } } } if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") { unset($_SESSION["cart_array"]); } //render cart $cartOutput = ""; if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { $cartOutput = "

Your shopping cart is empty

"; } else { $i = 0; foreach ($_SESSION["cart_array"] as $each_item) { $i++; $item_id = $each_item['item_id']; include_once('config/database.php'); include_once('object/chair.php'); $database = new Database(); $conn = $database->getConnection(); $chair = new Chair($conn); $chair->id = $_GET['detailsid']; $stmt = $chair->readDetails(); while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $product_name = $row['chair_name']; $price = $row['PRICE']; } $pricetotal = $price * $each_item['quantity']; $cartOutput .=""; $cartOutput .= "" . $product_name . ""; $cartOutput .= "" . $price . ""; $cartOutput .= "" . $each_item['quantity'] . ""; $cartOutput .= "" . $pricetotal . ""; $cartOutput .= "X"; $cartOutput .=""; } }[/php]

Normally, in a cart system, you would have an ID for the cart itself so that when it is stored into the database
that it would have some unique ID. Then, each entry into the cart would have it’s own ID for each row of items
that are purchased. Then, when you process the cart and do totals, you do the totals for the line-row items to
get the sub-totals, along with keeping a running grand-totals. I am sure you knew this already since you have it
mostly working. You need to debug it step by step to understand where the error is.

Have you attempted any type of debugging as yet? Perhaps it would be a good place to start. Now, to do that
there are many ways. First, the simplest is to just dump the current cart each time you add an item to see if it
is being created correctly. If it is, then you should dump the outputs of the display routines to see where it fails.

To dump the current cart, you can use: print_r($_SESSION[“cart_array”]); If you do this at the end of the
“render cart” routine, it should show the current cart at the bottom of the displayed cart. Then, as you enter
items and quantities, it will show you what changes and you can sort out your error.

This may not be a full answer, but, it should help you get started debugging… Good luck…

Sponsor our Newsletter | Privacy Policy | Terms of Service