简体   繁体   English

PHP购物车数组

[英]PHP shopping cart array

So I have my cart set up to receive specific information about each item added, using the GET function. 因此,我将购物车设置为使用GET函数接收有关添加的每个项目的特定信息。 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. 如果我添加椅子1,然后再添加椅子1,则它将增加到椅子1的总数,即有2个椅子1。 But if I then add chair 2, there will be a new entry but with all the values of chair one. 但是,如果我随后添加椅子2,将有一个新条目,但包含椅子1的所有值。

array output 数组输出

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

Purchase item area 购买项目区域

在此处输入图片说明

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>";
    }
}

Update: 更新:

A possible fix could be to add the details ID to the session array like so: 可能的解决方法是将详细信息ID添加到会话数组中,如下所示:

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: 然后,在显示购物车时,请使用值代替$_GET

$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: 您正在使用$_GET['detailsid']获取椅子的详细信息-每个循环的ID始终相同:

$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? 应该使用$item_id代替,以便获取正确椅子的详细信息吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM