如果在PHP中选择了购物车中的项目,则更新该项目

如果在PHP中选择了购物车中的项目,则更新该项目,php,Php,我正在建立一个网上订购食品的网站。因此,基本上,每件商品都有两种尺寸的PT和QT,价格不同。我能够添加到购物车,如果这两个价格选择之一。但有一个问题,例如,如果我选择了PTsize并已添加到购物车,但如果我想稍后更改为QTsize,它就不能这样做。我不知道臭虫在哪里。请看一下我的代码,帮助我解决这个问题。多谢各位 DB(表名为items): index.php $results = $mysqli->query("SELECT * FROM items WHERE produc

我正在建立一个网上订购食品的网站。因此,基本上,每件商品都有两种尺寸的
PT
QT
,价格不同。我能够添加到购物车,如果这两个价格选择之一。但有一个问题,例如,如果我选择了
PT
size并已添加到购物车,但如果我想稍后更改为
QT
size,它就不能这样做。我不知道臭虫在哪里。请看一下我的代码,帮助我解决这个问题。多谢各位

DB(表名为items):

index.php

      $results = $mysqli->query("SELECT * FROM items WHERE product_type='house_specials' ORDER BY id ASC");
      if ($results) {
      while($obj = $results->fetch_object()) {
        echo '<div class="product">'; 
        echo '<form method="post" action="cart_update.php">';
        echo '<div class="product-content"><h3>'.$obj->product_name.'</h3>';
        echo '<div class="product-desc">'.$obj->product_desc.'</div>';
        echo '<div class="product-info">';
        echo 'PT '.$currency.$obj->price.'<input id="pt" type="radio" name="productSize" value="'.$obj->price.'" checked="checked" />';
        echo 'QT '.$currency.$obj->priceBig.'<input id="qt" class="size_butt" type="radio" name="productSize" value="'.$obj->priceBig.'"/>';
        // echo 'Price '.$currency.$obj->price.' | ';
        echo 'Qty <input class="qtyBox" type="number" min="0" name="product_qty" value="1" size="3" min="0" />';
        echo '<button class="add_to_cart">Add To Cart</button>';
        echo '</div></div>';
        echo '<input type="hidden" name="product_code" value="'.$obj->product_code.'" />';
        echo '<input type="hidden" name="type" value="add" />';
        echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
        echo '</form>';
        echo '</div>';
      }

  }

<h2>Your Cart</h2>
<?php
if(isset($_SESSION["items"]))
{
    $total = 0;
    echo '<ol>';
    if (isset($_SESSION["items"])) {

    foreach ($_SESSION["items"] as $cart_itm)
    {
        echo '<li class="cart-itm">';
        echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">&times;</a></span>';
        echo '<h3>'.$cart_itm["name"].'</h3>';
        echo '<div class="p-code">P code : '.$cart_itm["code"].'</div>';
        echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
        echo '<div class="p-price">Price :'.$currency.$cart_itm["price"].'</div>';
        echo '</li>';
        $subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
        $total = ($total + $subtotal);
    }
    echo '</ol>';
    echo '<span class="check-out-txt"><strong>Total : '.$currency.$total.'</strong> <a href="view_cart.php"><button>Check-out!</button></a></span>';
    echo '<span class="empty-cart"><a href="cart_update.php?emptycart=1&return_url='.$current_url.'"><button>Empty Cart</button></a></span>';
  }
} else {
   echo 'Your cart is empty';
}
// This class is meant to be help keep your cart consistent and contained
// It has all the very basics
class   ShoppingCart
    {
        private $currency;

        public  function __construct($currency = '$')
            {
                $this->setCurrency($currency);
            }

        public  function setCurrency($currency)
            {
                $this->currency =   $currency;
            }

        public  function addToCart($item)
            {
                if(!isset($_SESSION['cart']))
                    $_SESSION['cart']   =   array();

                $item_code      =   filter_var($item['product_code'], FILTER_SANITIZE_STRING);
                $qty            =   (!is_numeric($item["product_qty"]))? 1 : $item["product_qty"];
                $price['qt']    =   $item["priceBig"];
                $price['pt']    =   $item["price"];
                $size           =   (!empty($item['productSize']))? $item['productSize'] : 'pt';

                if(isset($_SESSION['cart'][$item_code]))
                    $_SESSION['cart'][$item_code][$size]['qty']     +=  $qty;
                else {
                    $_SESSION['cart'][$item_code]['price']  =   $price;
                    $_SESSION['cart'][$item_code][$size]['qty']     =   $qty;
                }
            }

        public  function changeItemSize($item_code,$from = 'pt',$to = 'qt',$qty = false)
            {
                if(isset($_SESSION['cart'][$item_code][$from])) {
                    if(isset($_SESSION['cart'][$item_code][$to])){
                        $useQty =   (!$qty)? $_SESSION['cart'][$item_code][$from]['qty'] : $qty;
                        $_SESSION['cart'][$item_code][$to]['qty']   +=  $useQty;

                        $remainder  =   $_SESSION['cart'][$item_code][$from]['qty'] - $useQty;

                        if($remainder <= 0)
                            unset($_SESSION['cart'][$item_code][$from]);
                        else
                            $_SESSION['cart'][$item_code][$from]['qty'] = $remainder;
                    }
                    else {
                        $_SESSION['cart'][$item_code][$to]  =   $_SESSION['cart'][$item_code][$from];
                        unset($_SESSION['cart'][$item_code][$from]);
                    }
                }
            }

        public  function removeFromCart($item_code)
            {
                if(isset($_SESSION['cart'][$item_code]))
                    unset($_SESSION['cart'][$item_code]);
            }

        public  function getCurrency()
            {
                return $this->currency;
            }
    }
// This is just meant to pull some data from your db related
// to your products. You may see some value in expanding this out
class Products
    {
        private $con;

        public  function __construct($mysqli)
            {
                // You need to include the query function
                require_once(FUNCTION_DIR.'/function.query.php');
                $this->con  =   $mysqli;
            }

        public  function getProductDetails($itemCodes = false)
            {
                if(empty($itemCodes))
                    return query("SELECT * FROM items ORDER BY id ASC",$this->con);
                else {
                    foreach($itemCodes as $codes) {
                        $val    =   preg_replace('/[^a-zA-Z0-9]/','',$codes);
                        $val    =   trim($val);

                        if(empty($val))
                            continue;

                        $sql[]  =   "`product_code` = '{$val}'";
                    }

                    return query("SELECT * FROM `items` WHERE ".implode(" OR ",$sql)." ORDER BY `id` ASC",$this->con);
                }
            }

        public  function getHouseSpecials()
            {
                return query("SELECT * FROM items WHERE product_type = 'house_specials' ORDER BY id ASC",$this->con);
            }
    }
// This is just a fast and dirty way to run a query and return an array
// It's meant to return, so it's really only good for select statements
function query($sql,$mysqli)
    {
        $results    =   $mysqli->query($sql);

        if(!$results)
            return 0;

        while($obj = $results->fetch_object()) {
            $row[]  =   $obj;
        }

        return (!empty($row))? $row : array();
    }
// If you don't have some sort of config file with some simple defines and such
// I would encourage it so it's easier to keep track of files and allow
// for the re-use of common elements

//*** Include your database file ***//

// Create some defines (if you don't already have some)
define('ROOT_DIR',__DIR__);
define('CLASS_DIR',ROOT_DIR.'/core/classes');
define('FUNCTION_DIR',ROOT_DIR.'/core/functions');
// Autoload classes
spl_autoload_register(function($class){
    if(class_exists($class))
        return;
    if(is_file($file = CLASS_DIR.'/class.'.$class.'.php'))
        include_once($file);
});
// Include config
require_once(__DIR__.'/config.php');
// Include the required functions to render
require_once(FUNCTION_DIR.'/function.cartView.php');
require_once(FUNCTION_DIR.'/function.renderCart.php');
// Create instance of products
$pEngine    =   new Products($mysqli);
// Create shopping cart
$shopEngine =   new ShoppingCart('$');
// Do the cart actions if prompted
if(!empty($_REQUEST['add']))
    $shopEngine->addToCart($_REQUEST);
elseif(!empty($_REQUEST['change'])) {
    $to     =   $_REQUEST['productSize'];
    $from   =   ($to == 'pt')? 'qt' : 'pt';
    $shopEngine->changeItemSize($_REQUEST['product_code'],$from,$to);
}
elseif(!empty($_REQUEST['remove']))
    $shopEngine->removeFromCart($_REQUEST['product_code']);

// Quick render of the cart
// This is retrieved from your products list
// This is just an example
$prods = $pEngine->getHouseSpecials();
foreach($prods as $items) {
    echo cartView((object)$items);
}
// Quick demonstration of the session
if(!empty($_SESSION['cart'])) {
    $details    =   $pEngine->getProductDetails(array_keys($_SESSION['cart']));

    // you would just render the cart here
    // This function does not work, I didn't have time
    // to finish it...use print_r($details) to see what it returns
    // echo renderCart($mysqli,$details,'$');

    foreach($itemsInCart as $item_codes) {
        if(isset($_SESSION['cart'][$item_codes]['qt']))
            echo '<a href="?change=true&product_code='.$item_codes.'&productSize=pt">Item Change to Small</a></br />';
        if(isset($_SESSION['cart'][$item_codes]['pt']))
            echo '<a href="?change=true&product_code='.$item_codes.'&productSize=qt">Item Change to Large</a></br />';

        echo '<a href="?remove=true&product_code='.$item_codes.'">Remove</a></br />';
    }
}
$results=$mysqli->query(“从商品中选择*,商品类型为'house'specials'按id ASC订购”);
如果(结果){
而($obj=$results->fetch_object()){
回声';
回声';
回显'.$obj->产品名称'';
回显“.$obj->product_desc.”;
回声';
回显'PT'。$currency.$obj->price';
回显'QT'。$currency.$obj->priceBig';
//回显“价格”。$currency.$obj->Price.。|;
回显“数量”;
echo“添加到购物车”;
回声';
回声';
回声';
回声';
回声';
回声';
}
}
你的车

我个人主张创建每个函数(或类/方法)都有自己的工作。在这种情况下,我认为您应该这样做,并更改会话数组,如下所示:

function addToCart($item)
    {
        if(!isset($_SESSION['cart']))
            $_SESSION['cart']   =   array();

        $item_code  =   filter_var($item['product_code'], FILTER_SANITIZE_STRING);
        $qty        =   (!is_numeric($item["product_qty"]))? 1 : $item["product_qty"];
        $price['l'] =   $item["priceBig"];
        $price['s'] =   $item["price"];
        $size       =   (!empty($item['productSize']))? $item['productSize'] : 's';

        if(isset($_SESSION['cart'][$item_code]))
            $_SESSION['cart'][$item_code][$size]['qty']     +=  $qty;
        else {
            $_SESSION['cart'][$item_code]['price']  =   $price;
            $_SESSION['cart'][$item_code][$size]['qty']     =   $qty;
        }
    }

function changeItemSize($item_code,$from = 's',$to = 'l',$qty = false)
    {
        if(isset($_SESSION['cart'][$item_code][$from])) {
            if(isset($_SESSION['cart'][$item_code][$to])){
                $useQty =   (!$qty)? $_SESSION['cart'][$item_code][$from]['qty'] : $qty;
                $_SESSION['cart'][$item_code][$to]['qty']   +=  $useQty;

                $remainder  =   $_SESSION['cart'][$item_code][$from]['qty'] - $useQty;

                if($remainder <= 0)
                    unset($_SESSION['cart'][$item_code][$from]);
                else
                    $_SESSION['cart'][$item_code][$from]['qty'] = $remainder;
            }
            else {
                $_SESSION['cart'][$item_code][$to]  =   $_SESSION['cart'][$item_code][$from];
                unset($_SESSION['cart'][$item_code][$from]);
            }
        }
    }

if(!empty($_REQUEST['add'])) {
    addToCart($_REQUEST);
}
elseif(!empty($_REQUEST['change'])) {
    $to     =   $_REQUEST['productSize'];
    $from   =   ($to == 's')? 'l' : 's';
    changeItemSize($_REQUEST['product_code'],$from,$to);
}
更改功能将从大项目扣除到小项目,反之亦然。你可以随便玩玩,但我建议你这么做


编辑:以下是我试图通过上述示例了解的内容。这并不完美,但希望你能理解。我建议在一个完全不同的空间/页面上这样做,以防你弄不明白,至少你不会破坏你所拥有的

夫妻注:

1) 这个系统不需要你进入一个全新的页面来向购物车添加物品。都在一页纸上完成了

2) 注意文件夹之类的东西,这样你就可以把东西放在合适的地方

3) 最后,这仅用于演示,使用风险自负。这并不是为了复制和粘贴,而是为了看看是否有什么东西可以帮助您解决总体问题:

/core/classes/class.ShoppingCart.php

      $results = $mysqli->query("SELECT * FROM items WHERE product_type='house_specials' ORDER BY id ASC");
      if ($results) {
      while($obj = $results->fetch_object()) {
        echo '<div class="product">'; 
        echo '<form method="post" action="cart_update.php">';
        echo '<div class="product-content"><h3>'.$obj->product_name.'</h3>';
        echo '<div class="product-desc">'.$obj->product_desc.'</div>';
        echo '<div class="product-info">';
        echo 'PT '.$currency.$obj->price.'<input id="pt" type="radio" name="productSize" value="'.$obj->price.'" checked="checked" />';
        echo 'QT '.$currency.$obj->priceBig.'<input id="qt" class="size_butt" type="radio" name="productSize" value="'.$obj->priceBig.'"/>';
        // echo 'Price '.$currency.$obj->price.' | ';
        echo 'Qty <input class="qtyBox" type="number" min="0" name="product_qty" value="1" size="3" min="0" />';
        echo '<button class="add_to_cart">Add To Cart</button>';
        echo '</div></div>';
        echo '<input type="hidden" name="product_code" value="'.$obj->product_code.'" />';
        echo '<input type="hidden" name="type" value="add" />';
        echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
        echo '</form>';
        echo '</div>';
      }

  }

<h2>Your Cart</h2>
<?php
if(isset($_SESSION["items"]))
{
    $total = 0;
    echo '<ol>';
    if (isset($_SESSION["items"])) {

    foreach ($_SESSION["items"] as $cart_itm)
    {
        echo '<li class="cart-itm">';
        echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">&times;</a></span>';
        echo '<h3>'.$cart_itm["name"].'</h3>';
        echo '<div class="p-code">P code : '.$cart_itm["code"].'</div>';
        echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
        echo '<div class="p-price">Price :'.$currency.$cart_itm["price"].'</div>';
        echo '</li>';
        $subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
        $total = ($total + $subtotal);
    }
    echo '</ol>';
    echo '<span class="check-out-txt"><strong>Total : '.$currency.$total.'</strong> <a href="view_cart.php"><button>Check-out!</button></a></span>';
    echo '<span class="empty-cart"><a href="cart_update.php?emptycart=1&return_url='.$current_url.'"><button>Empty Cart</button></a></span>';
  }
} else {
   echo 'Your cart is empty';
}
// This class is meant to be help keep your cart consistent and contained
// It has all the very basics
class   ShoppingCart
    {
        private $currency;

        public  function __construct($currency = '$')
            {
                $this->setCurrency($currency);
            }

        public  function setCurrency($currency)
            {
                $this->currency =   $currency;
            }

        public  function addToCart($item)
            {
                if(!isset($_SESSION['cart']))
                    $_SESSION['cart']   =   array();

                $item_code      =   filter_var($item['product_code'], FILTER_SANITIZE_STRING);
                $qty            =   (!is_numeric($item["product_qty"]))? 1 : $item["product_qty"];
                $price['qt']    =   $item["priceBig"];
                $price['pt']    =   $item["price"];
                $size           =   (!empty($item['productSize']))? $item['productSize'] : 'pt';

                if(isset($_SESSION['cart'][$item_code]))
                    $_SESSION['cart'][$item_code][$size]['qty']     +=  $qty;
                else {
                    $_SESSION['cart'][$item_code]['price']  =   $price;
                    $_SESSION['cart'][$item_code][$size]['qty']     =   $qty;
                }
            }

        public  function changeItemSize($item_code,$from = 'pt',$to = 'qt',$qty = false)
            {
                if(isset($_SESSION['cart'][$item_code][$from])) {
                    if(isset($_SESSION['cart'][$item_code][$to])){
                        $useQty =   (!$qty)? $_SESSION['cart'][$item_code][$from]['qty'] : $qty;
                        $_SESSION['cart'][$item_code][$to]['qty']   +=  $useQty;

                        $remainder  =   $_SESSION['cart'][$item_code][$from]['qty'] - $useQty;

                        if($remainder <= 0)
                            unset($_SESSION['cart'][$item_code][$from]);
                        else
                            $_SESSION['cart'][$item_code][$from]['qty'] = $remainder;
                    }
                    else {
                        $_SESSION['cart'][$item_code][$to]  =   $_SESSION['cart'][$item_code][$from];
                        unset($_SESSION['cart'][$item_code][$from]);
                    }
                }
            }

        public  function removeFromCart($item_code)
            {
                if(isset($_SESSION['cart'][$item_code]))
                    unset($_SESSION['cart'][$item_code]);
            }

        public  function getCurrency()
            {
                return $this->currency;
            }
    }
// This is just meant to pull some data from your db related
// to your products. You may see some value in expanding this out
class Products
    {
        private $con;

        public  function __construct($mysqli)
            {
                // You need to include the query function
                require_once(FUNCTION_DIR.'/function.query.php');
                $this->con  =   $mysqli;
            }

        public  function getProductDetails($itemCodes = false)
            {
                if(empty($itemCodes))
                    return query("SELECT * FROM items ORDER BY id ASC",$this->con);
                else {
                    foreach($itemCodes as $codes) {
                        $val    =   preg_replace('/[^a-zA-Z0-9]/','',$codes);
                        $val    =   trim($val);

                        if(empty($val))
                            continue;

                        $sql[]  =   "`product_code` = '{$val}'";
                    }

                    return query("SELECT * FROM `items` WHERE ".implode(" OR ",$sql)." ORDER BY `id` ASC",$this->con);
                }
            }

        public  function getHouseSpecials()
            {
                return query("SELECT * FROM items WHERE product_type = 'house_specials' ORDER BY id ASC",$this->con);
            }
    }
// This is just a fast and dirty way to run a query and return an array
// It's meant to return, so it's really only good for select statements
function query($sql,$mysqli)
    {
        $results    =   $mysqli->query($sql);

        if(!$results)
            return 0;

        while($obj = $results->fetch_object()) {
            $row[]  =   $obj;
        }

        return (!empty($row))? $row : array();
    }
// If you don't have some sort of config file with some simple defines and such
// I would encourage it so it's easier to keep track of files and allow
// for the re-use of common elements

//*** Include your database file ***//

// Create some defines (if you don't already have some)
define('ROOT_DIR',__DIR__);
define('CLASS_DIR',ROOT_DIR.'/core/classes');
define('FUNCTION_DIR',ROOT_DIR.'/core/functions');
// Autoload classes
spl_autoload_register(function($class){
    if(class_exists($class))
        return;
    if(is_file($file = CLASS_DIR.'/class.'.$class.'.php'))
        include_once($file);
});
// Include config
require_once(__DIR__.'/config.php');
// Include the required functions to render
require_once(FUNCTION_DIR.'/function.cartView.php');
require_once(FUNCTION_DIR.'/function.renderCart.php');
// Create instance of products
$pEngine    =   new Products($mysqli);
// Create shopping cart
$shopEngine =   new ShoppingCart('$');
// Do the cart actions if prompted
if(!empty($_REQUEST['add']))
    $shopEngine->addToCart($_REQUEST);
elseif(!empty($_REQUEST['change'])) {
    $to     =   $_REQUEST['productSize'];
    $from   =   ($to == 'pt')? 'qt' : 'pt';
    $shopEngine->changeItemSize($_REQUEST['product_code'],$from,$to);
}
elseif(!empty($_REQUEST['remove']))
    $shopEngine->removeFromCart($_REQUEST['product_code']);

// Quick render of the cart
// This is retrieved from your products list
// This is just an example
$prods = $pEngine->getHouseSpecials();
foreach($prods as $items) {
    echo cartView((object)$items);
}
// Quick demonstration of the session
if(!empty($_SESSION['cart'])) {
    $details    =   $pEngine->getProductDetails(array_keys($_SESSION['cart']));

    // you would just render the cart here
    // This function does not work, I didn't have time
    // to finish it...use print_r($details) to see what it returns
    // echo renderCart($mysqli,$details,'$');

    foreach($itemsInCart as $item_codes) {
        if(isset($_SESSION['cart'][$item_codes]['qt']))
            echo '<a href="?change=true&product_code='.$item_codes.'&productSize=pt">Item Change to Small</a></br />';
        if(isset($_SESSION['cart'][$item_codes]['pt']))
            echo '<a href="?change=true&product_code='.$item_codes.'&productSize=qt">Item Change to Large</a></br />';

        echo '<a href="?remove=true&product_code='.$item_codes.'">Remove</a></br />';
    }
}
/core/functions/function.cartView.php

      $results = $mysqli->query("SELECT * FROM items WHERE product_type='house_specials' ORDER BY id ASC");
      if ($results) {
      while($obj = $results->fetch_object()) {
        echo '<div class="product">'; 
        echo '<form method="post" action="cart_update.php">';
        echo '<div class="product-content"><h3>'.$obj->product_name.'</h3>';
        echo '<div class="product-desc">'.$obj->product_desc.'</div>';
        echo '<div class="product-info">';
        echo 'PT '.$currency.$obj->price.'<input id="pt" type="radio" name="productSize" value="'.$obj->price.'" checked="checked" />';
        echo 'QT '.$currency.$obj->priceBig.'<input id="qt" class="size_butt" type="radio" name="productSize" value="'.$obj->priceBig.'"/>';
        // echo 'Price '.$currency.$obj->price.' | ';
        echo 'Qty <input class="qtyBox" type="number" min="0" name="product_qty" value="1" size="3" min="0" />';
        echo '<button class="add_to_cart">Add To Cart</button>';
        echo '</div></div>';
        echo '<input type="hidden" name="product_code" value="'.$obj->product_code.'" />';
        echo '<input type="hidden" name="type" value="add" />';
        echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
        echo '</form>';
        echo '</div>';
      }

  }

<h2>Your Cart</h2>
<?php
if(isset($_SESSION["items"]))
{
    $total = 0;
    echo '<ol>';
    if (isset($_SESSION["items"])) {

    foreach ($_SESSION["items"] as $cart_itm)
    {
        echo '<li class="cart-itm">';
        echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">&times;</a></span>';
        echo '<h3>'.$cart_itm["name"].'</h3>';
        echo '<div class="p-code">P code : '.$cart_itm["code"].'</div>';
        echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
        echo '<div class="p-price">Price :'.$currency.$cart_itm["price"].'</div>';
        echo '</li>';
        $subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
        $total = ($total + $subtotal);
    }
    echo '</ol>';
    echo '<span class="check-out-txt"><strong>Total : '.$currency.$total.'</strong> <a href="view_cart.php"><button>Check-out!</button></a></span>';
    echo '<span class="empty-cart"><a href="cart_update.php?emptycart=1&return_url='.$current_url.'"><button>Empty Cart</button></a></span>';
  }
} else {
   echo 'Your cart is empty';
}
// This class is meant to be help keep your cart consistent and contained
// It has all the very basics
class   ShoppingCart
    {
        private $currency;

        public  function __construct($currency = '$')
            {
                $this->setCurrency($currency);
            }

        public  function setCurrency($currency)
            {
                $this->currency =   $currency;
            }

        public  function addToCart($item)
            {
                if(!isset($_SESSION['cart']))
                    $_SESSION['cart']   =   array();

                $item_code      =   filter_var($item['product_code'], FILTER_SANITIZE_STRING);
                $qty            =   (!is_numeric($item["product_qty"]))? 1 : $item["product_qty"];
                $price['qt']    =   $item["priceBig"];
                $price['pt']    =   $item["price"];
                $size           =   (!empty($item['productSize']))? $item['productSize'] : 'pt';

                if(isset($_SESSION['cart'][$item_code]))
                    $_SESSION['cart'][$item_code][$size]['qty']     +=  $qty;
                else {
                    $_SESSION['cart'][$item_code]['price']  =   $price;
                    $_SESSION['cart'][$item_code][$size]['qty']     =   $qty;
                }
            }

        public  function changeItemSize($item_code,$from = 'pt',$to = 'qt',$qty = false)
            {
                if(isset($_SESSION['cart'][$item_code][$from])) {
                    if(isset($_SESSION['cart'][$item_code][$to])){
                        $useQty =   (!$qty)? $_SESSION['cart'][$item_code][$from]['qty'] : $qty;
                        $_SESSION['cart'][$item_code][$to]['qty']   +=  $useQty;

                        $remainder  =   $_SESSION['cart'][$item_code][$from]['qty'] - $useQty;

                        if($remainder <= 0)
                            unset($_SESSION['cart'][$item_code][$from]);
                        else
                            $_SESSION['cart'][$item_code][$from]['qty'] = $remainder;
                    }
                    else {
                        $_SESSION['cart'][$item_code][$to]  =   $_SESSION['cart'][$item_code][$from];
                        unset($_SESSION['cart'][$item_code][$from]);
                    }
                }
            }

        public  function removeFromCart($item_code)
            {
                if(isset($_SESSION['cart'][$item_code]))
                    unset($_SESSION['cart'][$item_code]);
            }

        public  function getCurrency()
            {
                return $this->currency;
            }
    }
// This is just meant to pull some data from your db related
// to your products. You may see some value in expanding this out
class Products
    {
        private $con;

        public  function __construct($mysqli)
            {
                // You need to include the query function
                require_once(FUNCTION_DIR.'/function.query.php');
                $this->con  =   $mysqli;
            }

        public  function getProductDetails($itemCodes = false)
            {
                if(empty($itemCodes))
                    return query("SELECT * FROM items ORDER BY id ASC",$this->con);
                else {
                    foreach($itemCodes as $codes) {
                        $val    =   preg_replace('/[^a-zA-Z0-9]/','',$codes);
                        $val    =   trim($val);

                        if(empty($val))
                            continue;

                        $sql[]  =   "`product_code` = '{$val}'";
                    }

                    return query("SELECT * FROM `items` WHERE ".implode(" OR ",$sql)." ORDER BY `id` ASC",$this->con);
                }
            }

        public  function getHouseSpecials()
            {
                return query("SELECT * FROM items WHERE product_type = 'house_specials' ORDER BY id ASC",$this->con);
            }
    }
// This is just a fast and dirty way to run a query and return an array
// It's meant to return, so it's really only good for select statements
function query($sql,$mysqli)
    {
        $results    =   $mysqli->query($sql);

        if(!$results)
            return 0;

        while($obj = $results->fetch_object()) {
            $row[]  =   $obj;
        }

        return (!empty($row))? $row : array();
    }
// If you don't have some sort of config file with some simple defines and such
// I would encourage it so it's easier to keep track of files and allow
// for the re-use of common elements

//*** Include your database file ***//

// Create some defines (if you don't already have some)
define('ROOT_DIR',__DIR__);
define('CLASS_DIR',ROOT_DIR.'/core/classes');
define('FUNCTION_DIR',ROOT_DIR.'/core/functions');
// Autoload classes
spl_autoload_register(function($class){
    if(class_exists($class))
        return;
    if(is_file($file = CLASS_DIR.'/class.'.$class.'.php'))
        include_once($file);
});
// Include config
require_once(__DIR__.'/config.php');
// Include the required functions to render
require_once(FUNCTION_DIR.'/function.cartView.php');
require_once(FUNCTION_DIR.'/function.renderCart.php');
// Create instance of products
$pEngine    =   new Products($mysqli);
// Create shopping cart
$shopEngine =   new ShoppingCart('$');
// Do the cart actions if prompted
if(!empty($_REQUEST['add']))
    $shopEngine->addToCart($_REQUEST);
elseif(!empty($_REQUEST['change'])) {
    $to     =   $_REQUEST['productSize'];
    $from   =   ($to == 'pt')? 'qt' : 'pt';
    $shopEngine->changeItemSize($_REQUEST['product_code'],$from,$to);
}
elseif(!empty($_REQUEST['remove']))
    $shopEngine->removeFromCart($_REQUEST['product_code']);

// Quick render of the cart
// This is retrieved from your products list
// This is just an example
$prods = $pEngine->getHouseSpecials();
foreach($prods as $items) {
    echo cartView((object)$items);
}
// Quick demonstration of the session
if(!empty($_SESSION['cart'])) {
    $details    =   $pEngine->getProductDetails(array_keys($_SESSION['cart']));

    // you would just render the cart here
    // This function does not work, I didn't have time
    // to finish it...use print_r($details) to see what it returns
    // echo renderCart($mysqli,$details,'$');

    foreach($itemsInCart as $item_codes) {
        if(isset($_SESSION['cart'][$item_codes]['qt']))
            echo '<a href="?change=true&product_code='.$item_codes.'&productSize=pt">Item Change to Small</a></br />';
        if(isset($_SESSION['cart'][$item_codes]['pt']))
            echo '<a href="?change=true&product_code='.$item_codes.'&productSize=qt">Item Change to Large</a></br />';

        echo '<a href="?remove=true&product_code='.$item_codes.'">Remove</a></br />';
    }
}
我建议将其作为包含的html页面,而不是直接写入此函数

/core/functions/function.query.php

      $results = $mysqli->query("SELECT * FROM items WHERE product_type='house_specials' ORDER BY id ASC");
      if ($results) {
      while($obj = $results->fetch_object()) {
        echo '<div class="product">'; 
        echo '<form method="post" action="cart_update.php">';
        echo '<div class="product-content"><h3>'.$obj->product_name.'</h3>';
        echo '<div class="product-desc">'.$obj->product_desc.'</div>';
        echo '<div class="product-info">';
        echo 'PT '.$currency.$obj->price.'<input id="pt" type="radio" name="productSize" value="'.$obj->price.'" checked="checked" />';
        echo 'QT '.$currency.$obj->priceBig.'<input id="qt" class="size_butt" type="radio" name="productSize" value="'.$obj->priceBig.'"/>';
        // echo 'Price '.$currency.$obj->price.' | ';
        echo 'Qty <input class="qtyBox" type="number" min="0" name="product_qty" value="1" size="3" min="0" />';
        echo '<button class="add_to_cart">Add To Cart</button>';
        echo '</div></div>';
        echo '<input type="hidden" name="product_code" value="'.$obj->product_code.'" />';
        echo '<input type="hidden" name="type" value="add" />';
        echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
        echo '</form>';
        echo '</div>';
      }

  }

<h2>Your Cart</h2>
<?php
if(isset($_SESSION["items"]))
{
    $total = 0;
    echo '<ol>';
    if (isset($_SESSION["items"])) {

    foreach ($_SESSION["items"] as $cart_itm)
    {
        echo '<li class="cart-itm">';
        echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">&times;</a></span>';
        echo '<h3>'.$cart_itm["name"].'</h3>';
        echo '<div class="p-code">P code : '.$cart_itm["code"].'</div>';
        echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
        echo '<div class="p-price">Price :'.$currency.$cart_itm["price"].'</div>';
        echo '</li>';
        $subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
        $total = ($total + $subtotal);
    }
    echo '</ol>';
    echo '<span class="check-out-txt"><strong>Total : '.$currency.$total.'</strong> <a href="view_cart.php"><button>Check-out!</button></a></span>';
    echo '<span class="empty-cart"><a href="cart_update.php?emptycart=1&return_url='.$current_url.'"><button>Empty Cart</button></a></span>';
  }
} else {
   echo 'Your cart is empty';
}
// This class is meant to be help keep your cart consistent and contained
// It has all the very basics
class   ShoppingCart
    {
        private $currency;

        public  function __construct($currency = '$')
            {
                $this->setCurrency($currency);
            }

        public  function setCurrency($currency)
            {
                $this->currency =   $currency;
            }

        public  function addToCart($item)
            {
                if(!isset($_SESSION['cart']))
                    $_SESSION['cart']   =   array();

                $item_code      =   filter_var($item['product_code'], FILTER_SANITIZE_STRING);
                $qty            =   (!is_numeric($item["product_qty"]))? 1 : $item["product_qty"];
                $price['qt']    =   $item["priceBig"];
                $price['pt']    =   $item["price"];
                $size           =   (!empty($item['productSize']))? $item['productSize'] : 'pt';

                if(isset($_SESSION['cart'][$item_code]))
                    $_SESSION['cart'][$item_code][$size]['qty']     +=  $qty;
                else {
                    $_SESSION['cart'][$item_code]['price']  =   $price;
                    $_SESSION['cart'][$item_code][$size]['qty']     =   $qty;
                }
            }

        public  function changeItemSize($item_code,$from = 'pt',$to = 'qt',$qty = false)
            {
                if(isset($_SESSION['cart'][$item_code][$from])) {
                    if(isset($_SESSION['cart'][$item_code][$to])){
                        $useQty =   (!$qty)? $_SESSION['cart'][$item_code][$from]['qty'] : $qty;
                        $_SESSION['cart'][$item_code][$to]['qty']   +=  $useQty;

                        $remainder  =   $_SESSION['cart'][$item_code][$from]['qty'] - $useQty;

                        if($remainder <= 0)
                            unset($_SESSION['cart'][$item_code][$from]);
                        else
                            $_SESSION['cart'][$item_code][$from]['qty'] = $remainder;
                    }
                    else {
                        $_SESSION['cart'][$item_code][$to]  =   $_SESSION['cart'][$item_code][$from];
                        unset($_SESSION['cart'][$item_code][$from]);
                    }
                }
            }

        public  function removeFromCart($item_code)
            {
                if(isset($_SESSION['cart'][$item_code]))
                    unset($_SESSION['cart'][$item_code]);
            }

        public  function getCurrency()
            {
                return $this->currency;
            }
    }
// This is just meant to pull some data from your db related
// to your products. You may see some value in expanding this out
class Products
    {
        private $con;

        public  function __construct($mysqli)
            {
                // You need to include the query function
                require_once(FUNCTION_DIR.'/function.query.php');
                $this->con  =   $mysqli;
            }

        public  function getProductDetails($itemCodes = false)
            {
                if(empty($itemCodes))
                    return query("SELECT * FROM items ORDER BY id ASC",$this->con);
                else {
                    foreach($itemCodes as $codes) {
                        $val    =   preg_replace('/[^a-zA-Z0-9]/','',$codes);
                        $val    =   trim($val);

                        if(empty($val))
                            continue;

                        $sql[]  =   "`product_code` = '{$val}'";
                    }

                    return query("SELECT * FROM `items` WHERE ".implode(" OR ",$sql)." ORDER BY `id` ASC",$this->con);
                }
            }

        public  function getHouseSpecials()
            {
                return query("SELECT * FROM items WHERE product_type = 'house_specials' ORDER BY id ASC",$this->con);
            }
    }
// This is just a fast and dirty way to run a query and return an array
// It's meant to return, so it's really only good for select statements
function query($sql,$mysqli)
    {
        $results    =   $mysqli->query($sql);

        if(!$results)
            return 0;

        while($obj = $results->fetch_object()) {
            $row[]  =   $obj;
        }

        return (!empty($row))? $row : array();
    }
// If you don't have some sort of config file with some simple defines and such
// I would encourage it so it's easier to keep track of files and allow
// for the re-use of common elements

//*** Include your database file ***//

// Create some defines (if you don't already have some)
define('ROOT_DIR',__DIR__);
define('CLASS_DIR',ROOT_DIR.'/core/classes');
define('FUNCTION_DIR',ROOT_DIR.'/core/functions');
// Autoload classes
spl_autoload_register(function($class){
    if(class_exists($class))
        return;
    if(is_file($file = CLASS_DIR.'/class.'.$class.'.php'))
        include_once($file);
});
// Include config
require_once(__DIR__.'/config.php');
// Include the required functions to render
require_once(FUNCTION_DIR.'/function.cartView.php');
require_once(FUNCTION_DIR.'/function.renderCart.php');
// Create instance of products
$pEngine    =   new Products($mysqli);
// Create shopping cart
$shopEngine =   new ShoppingCart('$');
// Do the cart actions if prompted
if(!empty($_REQUEST['add']))
    $shopEngine->addToCart($_REQUEST);
elseif(!empty($_REQUEST['change'])) {
    $to     =   $_REQUEST['productSize'];
    $from   =   ($to == 'pt')? 'qt' : 'pt';
    $shopEngine->changeItemSize($_REQUEST['product_code'],$from,$to);
}
elseif(!empty($_REQUEST['remove']))
    $shopEngine->removeFromCart($_REQUEST['product_code']);

// Quick render of the cart
// This is retrieved from your products list
// This is just an example
$prods = $pEngine->getHouseSpecials();
foreach($prods as $items) {
    echo cartView((object)$items);
}
// Quick demonstration of the session
if(!empty($_SESSION['cart'])) {
    $details    =   $pEngine->getProductDetails(array_keys($_SESSION['cart']));

    // you would just render the cart here
    // This function does not work, I didn't have time
    // to finish it...use print_r($details) to see what it returns
    // echo renderCart($mysqli,$details,'$');

    foreach($itemsInCart as $item_codes) {
        if(isset($_SESSION['cart'][$item_codes]['qt']))
            echo '<a href="?change=true&product_code='.$item_codes.'&productSize=pt">Item Change to Small</a></br />';
        if(isset($_SESSION['cart'][$item_codes]['pt']))
            echo '<a href="?change=true&product_code='.$item_codes.'&productSize=qt">Item Change to Large</a></br />';

        echo '<a href="?remove=true&product_code='.$item_codes.'">Remove</a></br />';
    }
}
/config.php

      $results = $mysqli->query("SELECT * FROM items WHERE product_type='house_specials' ORDER BY id ASC");
      if ($results) {
      while($obj = $results->fetch_object()) {
        echo '<div class="product">'; 
        echo '<form method="post" action="cart_update.php">';
        echo '<div class="product-content"><h3>'.$obj->product_name.'</h3>';
        echo '<div class="product-desc">'.$obj->product_desc.'</div>';
        echo '<div class="product-info">';
        echo 'PT '.$currency.$obj->price.'<input id="pt" type="radio" name="productSize" value="'.$obj->price.'" checked="checked" />';
        echo 'QT '.$currency.$obj->priceBig.'<input id="qt" class="size_butt" type="radio" name="productSize" value="'.$obj->priceBig.'"/>';
        // echo 'Price '.$currency.$obj->price.' | ';
        echo 'Qty <input class="qtyBox" type="number" min="0" name="product_qty" value="1" size="3" min="0" />';
        echo '<button class="add_to_cart">Add To Cart</button>';
        echo '</div></div>';
        echo '<input type="hidden" name="product_code" value="'.$obj->product_code.'" />';
        echo '<input type="hidden" name="type" value="add" />';
        echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
        echo '</form>';
        echo '</div>';
      }

  }

<h2>Your Cart</h2>
<?php
if(isset($_SESSION["items"]))
{
    $total = 0;
    echo '<ol>';
    if (isset($_SESSION["items"])) {

    foreach ($_SESSION["items"] as $cart_itm)
    {
        echo '<li class="cart-itm">';
        echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">&times;</a></span>';
        echo '<h3>'.$cart_itm["name"].'</h3>';
        echo '<div class="p-code">P code : '.$cart_itm["code"].'</div>';
        echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
        echo '<div class="p-price">Price :'.$currency.$cart_itm["price"].'</div>';
        echo '</li>';
        $subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
        $total = ($total + $subtotal);
    }
    echo '</ol>';
    echo '<span class="check-out-txt"><strong>Total : '.$currency.$total.'</strong> <a href="view_cart.php"><button>Check-out!</button></a></span>';
    echo '<span class="empty-cart"><a href="cart_update.php?emptycart=1&return_url='.$current_url.'"><button>Empty Cart</button></a></span>';
  }
} else {
   echo 'Your cart is empty';
}
// This class is meant to be help keep your cart consistent and contained
// It has all the very basics
class   ShoppingCart
    {
        private $currency;

        public  function __construct($currency = '$')
            {
                $this->setCurrency($currency);
            }

        public  function setCurrency($currency)
            {
                $this->currency =   $currency;
            }

        public  function addToCart($item)
            {
                if(!isset($_SESSION['cart']))
                    $_SESSION['cart']   =   array();

                $item_code      =   filter_var($item['product_code'], FILTER_SANITIZE_STRING);
                $qty            =   (!is_numeric($item["product_qty"]))? 1 : $item["product_qty"];
                $price['qt']    =   $item["priceBig"];
                $price['pt']    =   $item["price"];
                $size           =   (!empty($item['productSize']))? $item['productSize'] : 'pt';

                if(isset($_SESSION['cart'][$item_code]))
                    $_SESSION['cart'][$item_code][$size]['qty']     +=  $qty;
                else {
                    $_SESSION['cart'][$item_code]['price']  =   $price;
                    $_SESSION['cart'][$item_code][$size]['qty']     =   $qty;
                }
            }

        public  function changeItemSize($item_code,$from = 'pt',$to = 'qt',$qty = false)
            {
                if(isset($_SESSION['cart'][$item_code][$from])) {
                    if(isset($_SESSION['cart'][$item_code][$to])){
                        $useQty =   (!$qty)? $_SESSION['cart'][$item_code][$from]['qty'] : $qty;
                        $_SESSION['cart'][$item_code][$to]['qty']   +=  $useQty;

                        $remainder  =   $_SESSION['cart'][$item_code][$from]['qty'] - $useQty;

                        if($remainder <= 0)
                            unset($_SESSION['cart'][$item_code][$from]);
                        else
                            $_SESSION['cart'][$item_code][$from]['qty'] = $remainder;
                    }
                    else {
                        $_SESSION['cart'][$item_code][$to]  =   $_SESSION['cart'][$item_code][$from];
                        unset($_SESSION['cart'][$item_code][$from]);
                    }
                }
            }

        public  function removeFromCart($item_code)
            {
                if(isset($_SESSION['cart'][$item_code]))
                    unset($_SESSION['cart'][$item_code]);
            }

        public  function getCurrency()
            {
                return $this->currency;
            }
    }
// This is just meant to pull some data from your db related
// to your products. You may see some value in expanding this out
class Products
    {
        private $con;

        public  function __construct($mysqli)
            {
                // You need to include the query function
                require_once(FUNCTION_DIR.'/function.query.php');
                $this->con  =   $mysqli;
            }

        public  function getProductDetails($itemCodes = false)
            {
                if(empty($itemCodes))
                    return query("SELECT * FROM items ORDER BY id ASC",$this->con);
                else {
                    foreach($itemCodes as $codes) {
                        $val    =   preg_replace('/[^a-zA-Z0-9]/','',$codes);
                        $val    =   trim($val);

                        if(empty($val))
                            continue;

                        $sql[]  =   "`product_code` = '{$val}'";
                    }

                    return query("SELECT * FROM `items` WHERE ".implode(" OR ",$sql)." ORDER BY `id` ASC",$this->con);
                }
            }

        public  function getHouseSpecials()
            {
                return query("SELECT * FROM items WHERE product_type = 'house_specials' ORDER BY id ASC",$this->con);
            }
    }
// This is just a fast and dirty way to run a query and return an array
// It's meant to return, so it's really only good for select statements
function query($sql,$mysqli)
    {
        $results    =   $mysqli->query($sql);

        if(!$results)
            return 0;

        while($obj = $results->fetch_object()) {
            $row[]  =   $obj;
        }

        return (!empty($row))? $row : array();
    }
// If you don't have some sort of config file with some simple defines and such
// I would encourage it so it's easier to keep track of files and allow
// for the re-use of common elements

//*** Include your database file ***//

// Create some defines (if you don't already have some)
define('ROOT_DIR',__DIR__);
define('CLASS_DIR',ROOT_DIR.'/core/classes');
define('FUNCTION_DIR',ROOT_DIR.'/core/functions');
// Autoload classes
spl_autoload_register(function($class){
    if(class_exists($class))
        return;
    if(is_file($file = CLASS_DIR.'/class.'.$class.'.php'))
        include_once($file);
});
// Include config
require_once(__DIR__.'/config.php');
// Include the required functions to render
require_once(FUNCTION_DIR.'/function.cartView.php');
require_once(FUNCTION_DIR.'/function.renderCart.php');
// Create instance of products
$pEngine    =   new Products($mysqli);
// Create shopping cart
$shopEngine =   new ShoppingCart('$');
// Do the cart actions if prompted
if(!empty($_REQUEST['add']))
    $shopEngine->addToCart($_REQUEST);
elseif(!empty($_REQUEST['change'])) {
    $to     =   $_REQUEST['productSize'];
    $from   =   ($to == 'pt')? 'qt' : 'pt';
    $shopEngine->changeItemSize($_REQUEST['product_code'],$from,$to);
}
elseif(!empty($_REQUEST['remove']))
    $shopEngine->removeFromCart($_REQUEST['product_code']);

// Quick render of the cart
// This is retrieved from your products list
// This is just an example
$prods = $pEngine->getHouseSpecials();
foreach($prods as $items) {
    echo cartView((object)$items);
}
// Quick demonstration of the session
if(!empty($_SESSION['cart'])) {
    $details    =   $pEngine->getProductDetails(array_keys($_SESSION['cart']));

    // you would just render the cart here
    // This function does not work, I didn't have time
    // to finish it...use print_r($details) to see what it returns
    // echo renderCart($mysqli,$details,'$');

    foreach($itemsInCart as $item_codes) {
        if(isset($_SESSION['cart'][$item_codes]['qt']))
            echo '<a href="?change=true&product_code='.$item_codes.'&productSize=pt">Item Change to Small</a></br />';
        if(isset($_SESSION['cart'][$item_codes]['pt']))
            echo '<a href="?change=true&product_code='.$item_codes.'&productSize=qt">Item Change to Large</a></br />';

        echo '<a href="?remove=true&product_code='.$item_codes.'">Remove</a></br />';
    }
}
/index.php

      $results = $mysqli->query("SELECT * FROM items WHERE product_type='house_specials' ORDER BY id ASC");
      if ($results) {
      while($obj = $results->fetch_object()) {
        echo '<div class="product">'; 
        echo '<form method="post" action="cart_update.php">';
        echo '<div class="product-content"><h3>'.$obj->product_name.'</h3>';
        echo '<div class="product-desc">'.$obj->product_desc.'</div>';
        echo '<div class="product-info">';
        echo 'PT '.$currency.$obj->price.'<input id="pt" type="radio" name="productSize" value="'.$obj->price.'" checked="checked" />';
        echo 'QT '.$currency.$obj->priceBig.'<input id="qt" class="size_butt" type="radio" name="productSize" value="'.$obj->priceBig.'"/>';
        // echo 'Price '.$currency.$obj->price.' | ';
        echo 'Qty <input class="qtyBox" type="number" min="0" name="product_qty" value="1" size="3" min="0" />';
        echo '<button class="add_to_cart">Add To Cart</button>';
        echo '</div></div>';
        echo '<input type="hidden" name="product_code" value="'.$obj->product_code.'" />';
        echo '<input type="hidden" name="type" value="add" />';
        echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
        echo '</form>';
        echo '</div>';
      }

  }

<h2>Your Cart</h2>
<?php
if(isset($_SESSION["items"]))
{
    $total = 0;
    echo '<ol>';
    if (isset($_SESSION["items"])) {

    foreach ($_SESSION["items"] as $cart_itm)
    {
        echo '<li class="cart-itm">';
        echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">&times;</a></span>';
        echo '<h3>'.$cart_itm["name"].'</h3>';
        echo '<div class="p-code">P code : '.$cart_itm["code"].'</div>';
        echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
        echo '<div class="p-price">Price :'.$currency.$cart_itm["price"].'</div>';
        echo '</li>';
        $subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
        $total = ($total + $subtotal);
    }
    echo '</ol>';
    echo '<span class="check-out-txt"><strong>Total : '.$currency.$total.'</strong> <a href="view_cart.php"><button>Check-out!</button></a></span>';
    echo '<span class="empty-cart"><a href="cart_update.php?emptycart=1&return_url='.$current_url.'"><button>Empty Cart</button></a></span>';
  }
} else {
   echo 'Your cart is empty';
}
// This class is meant to be help keep your cart consistent and contained
// It has all the very basics
class   ShoppingCart
    {
        private $currency;

        public  function __construct($currency = '$')
            {
                $this->setCurrency($currency);
            }

        public  function setCurrency($currency)
            {
                $this->currency =   $currency;
            }

        public  function addToCart($item)
            {
                if(!isset($_SESSION['cart']))
                    $_SESSION['cart']   =   array();

                $item_code      =   filter_var($item['product_code'], FILTER_SANITIZE_STRING);
                $qty            =   (!is_numeric($item["product_qty"]))? 1 : $item["product_qty"];
                $price['qt']    =   $item["priceBig"];
                $price['pt']    =   $item["price"];
                $size           =   (!empty($item['productSize']))? $item['productSize'] : 'pt';

                if(isset($_SESSION['cart'][$item_code]))
                    $_SESSION['cart'][$item_code][$size]['qty']     +=  $qty;
                else {
                    $_SESSION['cart'][$item_code]['price']  =   $price;
                    $_SESSION['cart'][$item_code][$size]['qty']     =   $qty;
                }
            }

        public  function changeItemSize($item_code,$from = 'pt',$to = 'qt',$qty = false)
            {
                if(isset($_SESSION['cart'][$item_code][$from])) {
                    if(isset($_SESSION['cart'][$item_code][$to])){
                        $useQty =   (!$qty)? $_SESSION['cart'][$item_code][$from]['qty'] : $qty;
                        $_SESSION['cart'][$item_code][$to]['qty']   +=  $useQty;

                        $remainder  =   $_SESSION['cart'][$item_code][$from]['qty'] - $useQty;

                        if($remainder <= 0)
                            unset($_SESSION['cart'][$item_code][$from]);
                        else
                            $_SESSION['cart'][$item_code][$from]['qty'] = $remainder;
                    }
                    else {
                        $_SESSION['cart'][$item_code][$to]  =   $_SESSION['cart'][$item_code][$from];
                        unset($_SESSION['cart'][$item_code][$from]);
                    }
                }
            }

        public  function removeFromCart($item_code)
            {
                if(isset($_SESSION['cart'][$item_code]))
                    unset($_SESSION['cart'][$item_code]);
            }

        public  function getCurrency()
            {
                return $this->currency;
            }
    }
// This is just meant to pull some data from your db related
// to your products. You may see some value in expanding this out
class Products
    {
        private $con;

        public  function __construct($mysqli)
            {
                // You need to include the query function
                require_once(FUNCTION_DIR.'/function.query.php');
                $this->con  =   $mysqli;
            }

        public  function getProductDetails($itemCodes = false)
            {
                if(empty($itemCodes))
                    return query("SELECT * FROM items ORDER BY id ASC",$this->con);
                else {
                    foreach($itemCodes as $codes) {
                        $val    =   preg_replace('/[^a-zA-Z0-9]/','',$codes);
                        $val    =   trim($val);

                        if(empty($val))
                            continue;

                        $sql[]  =   "`product_code` = '{$val}'";
                    }

                    return query("SELECT * FROM `items` WHERE ".implode(" OR ",$sql)." ORDER BY `id` ASC",$this->con);
                }
            }

        public  function getHouseSpecials()
            {
                return query("SELECT * FROM items WHERE product_type = 'house_specials' ORDER BY id ASC",$this->con);
            }
    }
// This is just a fast and dirty way to run a query and return an array
// It's meant to return, so it's really only good for select statements
function query($sql,$mysqli)
    {
        $results    =   $mysqli->query($sql);

        if(!$results)
            return 0;

        while($obj = $results->fetch_object()) {
            $row[]  =   $obj;
        }

        return (!empty($row))? $row : array();
    }
// If you don't have some sort of config file with some simple defines and such
// I would encourage it so it's easier to keep track of files and allow
// for the re-use of common elements

//*** Include your database file ***//

// Create some defines (if you don't already have some)
define('ROOT_DIR',__DIR__);
define('CLASS_DIR',ROOT_DIR.'/core/classes');
define('FUNCTION_DIR',ROOT_DIR.'/core/functions');
// Autoload classes
spl_autoload_register(function($class){
    if(class_exists($class))
        return;
    if(is_file($file = CLASS_DIR.'/class.'.$class.'.php'))
        include_once($file);
});
// Include config
require_once(__DIR__.'/config.php');
// Include the required functions to render
require_once(FUNCTION_DIR.'/function.cartView.php');
require_once(FUNCTION_DIR.'/function.renderCart.php');
// Create instance of products
$pEngine    =   new Products($mysqli);
// Create shopping cart
$shopEngine =   new ShoppingCart('$');
// Do the cart actions if prompted
if(!empty($_REQUEST['add']))
    $shopEngine->addToCart($_REQUEST);
elseif(!empty($_REQUEST['change'])) {
    $to     =   $_REQUEST['productSize'];
    $from   =   ($to == 'pt')? 'qt' : 'pt';
    $shopEngine->changeItemSize($_REQUEST['product_code'],$from,$to);
}
elseif(!empty($_REQUEST['remove']))
    $shopEngine->removeFromCart($_REQUEST['product_code']);

// Quick render of the cart
// This is retrieved from your products list
// This is just an example
$prods = $pEngine->getHouseSpecials();
foreach($prods as $items) {
    echo cartView((object)$items);
}
// Quick demonstration of the session
if(!empty($_SESSION['cart'])) {
    $details    =   $pEngine->getProductDetails(array_keys($_SESSION['cart']));

    // you would just render the cart here
    // This function does not work, I didn't have time
    // to finish it...use print_r($details) to see what it returns
    // echo renderCart($mysqli,$details,'$');

    foreach($itemsInCart as $item_codes) {
        if(isset($_SESSION['cart'][$item_codes]['qt']))
            echo '<a href="?change=true&product_code='.$item_codes.'&productSize=pt">Item Change to Small</a></br />';
        if(isset($_SESSION['cart'][$item_codes]['pt']))
            echo '<a href="?change=true&product_code='.$item_codes.'&productSize=qt">Item Change to Large</a></br />';

        echo '<a href="?remove=true&product_code='.$item_codes.'">Remove</a></br />';
    }
}
//包含配置
需要一次(uuu DIR_uuu.'/config.php');
//包括渲染所需的函数
需要一次(FUNCTION_DIR.'/FUNCTION.cartView.php');
需要一次(FUNCTION_DIR.'/FUNCTION.renderCart.php');
//创建产品实例
$pEngine=新产品($mysqli);
//创建购物车
$shopEngine=新购物车(“$”);
//如果出现提示,请执行购物车操作
如果(!empty($_请求['add']))
$shopEngine->addToCart($\u请求);
elseif(!empty($\u请求['change'])){
$to=$_请求['productSize'];
$from=($to='pt')?'qt':'pt';
$shopEngine->changeItemSize($\请求['product\ u代码],$from,$to);
}
elseif(!empty($\u请求['remove']))
$shopEngine->removeFromCart($_请求['product_code']);
//快速渲染购物车
//这是从您的产品列表中检索到的
//这只是一个例子
$prods=$pEngine->getHouseSpecials();
foreach($prods作为$items){
echo cartView((对象)$项);
}
//会议的快速演示
如果(!empty($\u会话['cart'])){
$details=$pEngine->getProductDetails(数组_键($_会话['cart']);
//您只需在此处渲染购物车即可
//这个功能不起作用,我没有时间
//要完成它,请使用print\r($details)查看它返回的内容
//echo renderCart($mysqli,$details,“$”);
foreach($itemsInCart作为$item_代码){
如果(isset($_会话['cart'][$item_代码]['qt']))
回音“
”; 如果(isset($\会话['cart'][$item\u代码]['pt'])) 回音“
”; 回音“
”; } }
您能告诉我如何将您的代码添加到我的代码中吗?因为我现在有点迷路了。谢谢是的,我会在今晚晚些时候做。我现在不在我的电脑前非常感谢你@Rasclatt