简体   繁体   English

网上购物车的top item不会删除?

[英]Top item of online shopping cart will not delete?

Building an online store using php and mysql, items in the cart session will all remove just fine with the exception of whatever the top item is.使用 php 和 mysql 构建在线商店,购物车会话中的项目将全部删除,除了最上面的项目。 Any ideas?有任何想法吗? Code for delete from cart:从购物车中删除的代码:

<?php 
session_start();
    $items = $_SESSION['cart'];
    $cartitems = explode(",", $items);
        if(isset($_GET['remove']) && !empty($_GET['remove'])){
        $delitem = $_GET['remove'];
        unset($cartitems[$delitem]);
        $itemids = implode(",", $cartitems);
        $_SESSION['cart'] = $itemids;
    }
header('location:cart.php');
?>

Add to cart:添加到购物车:

session_start();
if(isset($_GET['id']) & !empty($_GET['id'])){
    if(isset($_SESSION['cart']) && !empty($_SESSION['cart'])){

        $items = $_SESSION['cart'];
        $cartitems = explode(",", $items);
            $items .= "," . $_GET['id'];
            $_SESSION['cart'] = $items;     

        }else{
            $items = $_GET['id'];
            $_SESSION['cart'] = $items;
        }       
    }
?>

I appreciate any help I can get!我很感激我能得到的任何帮助!

It would be much easier to convert your $_SESSION['cart'] to an array rather than joining multiple ID's in a string with separators.将您的$_SESSION['cart']转换为数组会容易得多,而不是将多个 ID 连接到带有分隔符的字符串中。 You can then use array_filter() and array_search() .然后您可以使用array_filter()array_search()

public function addItemToCart($id) {
    # Filter through cart for the ID
    $cartProduct = array_filter($_SESSION['cart'], function($product) use($id) {
        $product = (object) $product;
        return (int) $product->id == (int) $id;
    });

    # If the ID exists, increase quantity
    if (!empty($cartProduct)) {
        $product = (object) $cartProduct[0];
        ++$_SESSION['cart'][array_search(
        (int) $product->id,
        $_SESSION['cart'])]['quantity'];
        return;
    }

    # If the ID does not exist, add new ID
    $_SESSION['cart'][] = ['id' => $id, 'quantity' => 1];
}

function removeItemFromCart($id) {
    # Update cart with the removed item
    $_SESSION['cart'] = array_filter($_SESSION['cart'], function($product) {
        $product = (object) $product;
        return (int) $product->id != (int) $id;
    });
}

Then to access your cart you can use:然后要访问您的购物车,您可以使用:

function getItemsFromCart($callback) {
    if(!is_callable($callback)) return false; # Ensure it is a Closure
    foreach($_SESSION['cart'] as $product) call_user_func($callback, (object) $product); # Pass every array as an object to the function
}

Which can be used like so:可以这样使用:

getItemsFromCart(function($product) {
    # $product will be used for every product inside the cart with ->id and ->quantity
    # Recommend making this a static call to get a connection rather than opening multiple - just demonstration purposes.

    $stmt = (new PDO('dsn', 'user', 'pass', [
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   => false
    ]))->Prepare('SELECT cost FROM myProductTable WHERE productId = ? LIMIT 1');

    $stmt->execute(array((int) $product->id));
    $cost = ((object) $stmt->fetch())->cost * (int) $product->quantity; # Here is your cost :)
});

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

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