1

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 ) )

Purchase item area

enter image description here

Database:

<?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)){
?>

Add to cart:

  <div class="addtocart">
                <!--<div class="button">
                Add to cart

                </div>-->
                <div class="button">
                <form id="form1" name="form1" method="post" action="cart.php?detailsid=<?php echo $row['ID'];?>">
            <input type="hidden" name="pid" id="pid" value="<?php echo $row['ID'];?>"/>
            <input type="submit" name="button" id="button" value="Add to Shooping Cart"/>        

                    </form>

Cart fucntion:

<?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 = "<h2 align=center'>Your shopping cart is empty</h2>";
}
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 .="<tr>";
        $cartOutput .= "<td>" . $product_name . "</td>";
        $cartOutput .= "<td>" . $price . "</td>";
        $cartOutput .= "<td>" . $each_item['quantity'] . "</td>";
        $cartOutput .= "<td>" . $pricetotal . "</td>";
        $cartOutput .= "<td>X</td>";
        $cartOutput .="</tr>";
    }
}
6
  • show the code that handles adding item to $_SESSION['cart_arrray'] Commented Feb 2, 2016 at 21:59
  • Please show the code for $_SESSION["cart_array"] - It may be creating a new item below instead of merging QTY because array key is diff Commented Feb 2, 2016 at 22:10
  • Can you add those 3 items to the cart and then show us the results from print_r($_SESSION["cart_array"]) so we can see what the array looks like inside Commented Feb 2, 2016 at 22:20
  • Array ( [0] => Array ( [item_id] => 2 [quantity] => 1 ) [1] => Array ( [item_id] => 4 [quantity] => 5 ) ) is what it says Commented Feb 2, 2016 at 22:25
  • I dont understand, you added 3 items to the card and its showing quantity 5 ? I think the main problem is your item_id are different somewhere and thats why its not adding to the QTY and creating a new entry.. Commented Feb 2, 2016 at 22:44

1 Answer 1

1

Update:

A possible fix could be to add the details ID to the session array like so:

if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1)
{
    $_SESSION["cart_array"] = array(1 => array("item_id" => $pid, "quantity" => 1, "details_id" => $_GET['detailsid']));
}
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, "details_id" => $_GET['detailsid'])));
                $wasFound = true;
            }
        }
    }
    if ($wasFound == false)
    {
        array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1, "details_id" => $_GET['detailsid']));
    }
}

Then when displaying the cart use the value instead of the $_GET one:

$chair     = new Chair($conn);
$chair->id = $each_item['details_id'];
$stmt      = $chair->readDetails();

You are fetching the chair details using $_GET['detailsid'] - which will always be the same ID for each loop:

$chair     = new Chair($conn);
$chair->id = $_GET['detailsid'];
$stmt      = $chair->readDetails();

Should this be $item_id instead so you are fetching the details of the correct chair?

Sign up to request clarification or add additional context in comments.

4 Comments

yeah i kind of figured as much but I'm not sure how to pass the var other than GET. the ID, is the unique identifier for each item in the sql file. While $item_id is just for each item in the cart, so you can delete each item. when the "add to cart" button is hit the ID is sent as being = to detailsid.
Can you add the detailsid to the cart array along with the other details? array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1, "detailsid" => 99));
okay amazing, it works, a few errors pop up now though if there's only one item in the cart. i think it has to do with - 1 function, but it reads each item separately. just for future people who find the question though you did leave gaps between your "=" and ">" when referring to detailsid.
God pickup, I've fixed the arrows - I also missed the first add to cart statement, so I've just added that as well to the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.