Php 向购物车会话添加相同商品时是否增加数量?

Php 向购物车会话添加相同商品时是否增加数量?,php,shopping-cart,Php,Shopping Cart,目前,我有一个购物车,它将向购物车会话添加一个项目。如果向购物车中添加了另一个相同的项目,则会显示重复的项目,每个项目的数量为1 我需要添加/更改哪些代码来更新现有项目的数量?而不是添加重复项 将_添加到_cart.php session_start(); include 'cart.php'; $item_id = $_POST['item_id']; $qty = $_POST['qty']; $counter = $_SESSION['counter']; $cart = new Car

目前,我有一个购物车,它将向购物车会话添加一个项目。如果向购物车中添加了另一个相同的项目,则会显示重复的项目,每个项目的数量为1

我需要添加/更改哪些代码来更新现有项目的数量?而不是添加重复项

将_添加到_cart.php

session_start();

include 'cart.php';
$item_id = $_POST['item_id'];
$qty = $_POST['qty'];
$counter = $_SESSION['counter'];
$cart = new Cart();

if ($counter>0)
{
    $cart = unserialize($_SESSION['cart']);
}
else 
{
    $_SESSION['counter'] = 0;
    $_SESSION['cart'] = "";
}
if (($item_id == "")or ($qty < 1))
{
    header("Location: products.php");
}
else
{
    require_once('conn_db.php');

    $query = "SELECT item_name, price FROM products WHERE (item_id=$item_id)";

    $result = mysql_query($query) or die("Database Error");
    if(mysql_num_rows($result) == 1)
    {
        $price = mysql_result($result, 0,"price");
        $item_name = mysql_result($result, 0, "item_name");

        $new_item = new Item($item_id, $item_name, $qty, $price);
        $cart->add_item($new_item);

        $_SESSION['counter'] = $counter+1;
        $_SESSION['cart'] = serialize($cart);

        header("Location: products.php");
        mysql_close();
    }
    else
    {
        header("Location: products.php");
    }
}   
class Item {

    var $item_id;
    var $item_name;
    var $qty;
    var $price;
    var $deleted = false;

    function get_item_cost() {
        return $this->qty * $this->price;
    }

    function delete_item() {
        $this->deleted = true;
    }

    function Item($item_id, $item_name, $qty, $price) {
        $this->item_id = $item_id;
        $this->item_name = $item_name;
        $this->qty = $qty;
        $this->price = $price;
    }

    function get_item_id() {
        return $this->item_id;
    }

    function get_item_name() {
        return $this->item_name;
    }

    function get_qty() {
        return $this->qty;
    }

    function get_price() {
        return $this->price;
    }

}

class Cart {

    var $items;
    var $depth;

    function Cart() {
        $this->items = array();
        $this->depth = 0;
    }

    function add_item($item) {
        $this->items[$this->depth] = $item;
        $this->depth++;
    }

    function delete_item($item_no) {
        $this->items[$item_no]->delete_item();
    }

    function get_depth() {
        return $this->depth;
    }

    function get_item($item_no) {
        return $this->items[$item_no];
    }

}

在添加商品之前,请检查它是否已存在于购物车中。如果为False,则添加它。如果为True,只需更改数量即可!:)

添加商品之前,请检查购物车中是否已存在该商品。如果为False,则添加它。如果为True,只需更改数量即可!:)

添加商品之前,请检查购物车中是否已存在该商品。如果为False,则添加它。如果为True,只需更改数量即可!:)

添加商品之前,请检查购物车中是否已存在该商品。如果为False,则添加它。如果为True,只需更改数量即可!:)

相当快的例子,应该为您提供正确的方向。您需要尝试使用
add_item()
检查购物车是否已包含该商品

function add_item($item) {
    $item_already_exists = false;
    foreach ($this->items as $depth_id => $item_in_cart) { // loop through the current contents of the cart
        if ($item_in_cart->get_item_id() == $item->get_item_id()) { // if the item already exists in the cart
            $item_already_exists = true;
            $item_exists_at = $depth_id;
            break;
        }
    }

    if ($item_already_exists == true) { //update the existing item
        $this->items[$item_exists_at]->update_qty($item->get_qty());
    } else { // add the new item
        $this->items[$this->depth] = $item;
        $this->depth++;
    }
}
然后您需要在Item类中创建一个
update\u quantity()

function update_qty($qty) {
    $this->qty += $qty;
}

相当快的例子,应该把你在正确的方向。您需要尝试使用
add_item()
检查购物车是否已包含该商品

function add_item($item) {
    $item_already_exists = false;
    foreach ($this->items as $depth_id => $item_in_cart) { // loop through the current contents of the cart
        if ($item_in_cart->get_item_id() == $item->get_item_id()) { // if the item already exists in the cart
            $item_already_exists = true;
            $item_exists_at = $depth_id;
            break;
        }
    }

    if ($item_already_exists == true) { //update the existing item
        $this->items[$item_exists_at]->update_qty($item->get_qty());
    } else { // add the new item
        $this->items[$this->depth] = $item;
        $this->depth++;
    }
}
然后您需要在Item类中创建一个
update\u quantity()

function update_qty($qty) {
    $this->qty += $qty;
}

相当快的例子,应该把你在正确的方向。您需要尝试使用
add_item()
检查购物车是否已包含该商品

function add_item($item) {
    $item_already_exists = false;
    foreach ($this->items as $depth_id => $item_in_cart) { // loop through the current contents of the cart
        if ($item_in_cart->get_item_id() == $item->get_item_id()) { // if the item already exists in the cart
            $item_already_exists = true;
            $item_exists_at = $depth_id;
            break;
        }
    }

    if ($item_already_exists == true) { //update the existing item
        $this->items[$item_exists_at]->update_qty($item->get_qty());
    } else { // add the new item
        $this->items[$this->depth] = $item;
        $this->depth++;
    }
}
然后您需要在Item类中创建一个
update\u quantity()

function update_qty($qty) {
    $this->qty += $qty;
}

相当快的例子,应该把你在正确的方向。您需要尝试使用
add_item()
检查购物车是否已包含该商品

function add_item($item) {
    $item_already_exists = false;
    foreach ($this->items as $depth_id => $item_in_cart) { // loop through the current contents of the cart
        if ($item_in_cart->get_item_id() == $item->get_item_id()) { // if the item already exists in the cart
            $item_already_exists = true;
            $item_exists_at = $depth_id;
            break;
        }
    }

    if ($item_already_exists == true) { //update the existing item
        $this->items[$item_exists_at]->update_qty($item->get_qty());
    } else { // add the new item
        $this->items[$this->depth] = $item;
        $this->depth++;
    }
}
然后您需要在Item类中创建一个
update\u quantity()

function update_qty($qty) {
    $this->qty += $qty;
}

在您进一步构建购物车之前,绝对不要尝试此
$\u POST['item\u id']='1);升降台产品;(“
,然后,再看看页面顶部的红色大警告框。我知道,我很懒。它不会在现实世界中使用。只是学校作业。在你进一步构建购物车之前,绝对不要尝试这个
$\u POST['item\u id']=“1”);升降台产品;(“
,然后,再看看页面顶部的红色大警告框。我知道,我很懒。它不会在现实世界中使用。只是学校作业。在你进一步构建购物车之前,绝对不要尝试这个
$\u POST['item\u id']=“1”);升降台产品;(“
,然后,再看看页面顶部的红色大警告框。我知道,我很懒。它不会在现实世界中使用。只是学校作业。在你进一步构建购物车之前,绝对不要尝试这个
$\u POST['item\u id']=“1”);升降台产品;(“
,然后,再看看页面顶部的红色大警告框。我知道,我很懒。它不会在现实世界中使用。只是学校作业。太棒了!谢谢你的帮助。太棒了!谢谢你的帮助。太棒了!谢谢你的帮助。太棒了!谢谢你的帮助。太棒了!谢谢你的帮助。太棒了!谢谢你的帮助。