【问题标题】:Deleting a single element from a PHP session cart [closed]从 PHP 会话购物车中删除单个元素 [关闭]
【发布时间】:2013-07-27 20:48:01
【问题描述】:

我在一个网站上设置了一个购物车,它使用一个名为 cart.php 的文件来完成所有工作。 “添加”功能完美运行,我也可以清空购物车,但不能删除单个项目

删除链接如下所示:

<a href='cart.php?action=delete&id=$cartId'>delete</a> 

它会创建一个类似这样的链接:cart.php?action=delete&amp;id=1

文件cart.php在这里:

<?php
require_once('Connections/ships.php');
// Include functions
require_once('inc/functions.inc.php');
// Start the session
session_start();
// Process actions
$cart = $_SESSION['cart'];
$action = $_GET['action'];

$items = explode(',',$cart);


    if (count($items) > 5) 
    {
    header("Location: shipinfo_full.php") ;
    }
    else 
    {
    switch ($action) 
        {
        case 'add':
        if ($cart) 
            {
            $cart .= ','.$_GET['ship_id'];
            } 
            else 
            {
            $cart = $_GET['ship_id'];
            }
            header("Location: info_added.php?ship_id=" .  $_GET['ship_id']) ;
            break;
            case 'delete':
            if ($cart) 
            {
            $items = explode(',',$cart);
            $newcart = '';
            foreach ($items as $item) 
                {
                if ($_GET['ship_id'] != $item) 
                    {
                    if ($newcart != '') 
                        {
                        $newcart .= ','.$item;
                        } 
                        else 
                        {
                        $newcart = $item;
                        }
                    }
                }
                $cart = $newcart;

            }
            header("Location: info.php?ship_id=" .  $_GET['ship_id']) ;
            break;
            $cart = $newcart;
            break;
        }
        $_SESSION['cart'] = $cart;

    }
?>

有什么想法可以删除单个项目吗?

【问题讨论】:

  • 你的命名是否一致?在 OP 中,您将其称为“id”,但随后您的代码正在寻找“ship_id”...
  • 嗨,这不是 Stack Overflow 应该如何工作的。你应该带来一些的知识和努力。据我所知,这是您复制和粘贴代码的第四个问题,本质上是要求社区为您完善和修复它。
  • 感谢汤姆 - 真的很有帮助! Pekka,在我做事的同时努力学习,所以如果你想建议我尝试的地方,很乐意倾听

标签: php session cart shopping


【解决方案1】:

看看这个:

case 'delete':
    if ($cart) 
    {
        $items = explode(',',$cart);
        $newcart = array();
        foreach ($items as $item) 
        {
            if ($_GET['ship_id'] != $item) 
            {
                $newcart[] = $item;

            }
        }
        $_SESSION['cart'] = implode(',', $newcart);

    }
    header("Location: info.php?ship_id=" .  $_GET['ship_id']) ;
break;

它将用除$_GET['ship_id'] 之外的所有项目填充newcart 数组。还有一件事,在重定向之前填充会话。

【讨论】:

    【解决方案2】:

    您可以通过将项目存储在会话内的数组中来以更好的方式编写它,如下所示

    $_SESSION['cart'] = array(); // cart is initially empty
    

    现在将商品添加到购物车

    $_SESSION['cart'][] = array('name' => 'some name', 'price' => 100);
    

    从购物车中删除商品

    unset($_SESSION['cart'][22]); // assuming that 22 is the item ID
    

    列出项目

    $cart = $_SESSION['cart'];
    forearch($cart as $item){
        echo $item['name']; }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-12
      • 2015-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多