贴一个给你..<?php
/**
*  类名: cart
*  描述: 尝试以最简单的方式实现购物车
*  其他: 2004-8-19
*/
class cart
{
    /**
    *   函数名称:   addItem
    *   函数功能:   添加商品
    *   输入参数:   $data ------------- 商品数组
    *   函数返回值: none
    *   其它说明:   因为数据是记录在session中,所以不用返回
    */
    function addItem ( $data )
    {
        if ( is_array ( $data ) && !empty ( $data ) )
        {
            foreach ( $data as $key => $val )
            {
                // 如果商品存在就加数量和价格
                if ( $this->_isExists ( $key ) )
                {
                    $_SESSION['cart'][$key]["count"] += intval ( $val['price'] ) * intval ( $val['num'] );
                    $_SESSION['cart'][$key]["num"]   += intval ( $val['num'] );
                }
                // 否则直接加入
                else
                {
                    $_SESSION['cart'][$key]  = $data[$key];
                    $_SESSION['cart'][$key]["name"]  = $val['name'];
                    $_SESSION['cart'][$key]["price"] = $val['price'];
                    $_SESSION['cart'][$key]["count"] = intval ( $val['price'] )*intval ( $val['num'] );
                    $_SESSION['cart'][$key]["num"]   = intval ( $val['num'] );
                }
            }
        }
    }    /**
    *   函数名称:   _isExists
    *   函数功能:   判断此商品是否存在
    *   输入参数:   $id ---------- 商品ID
    *   函数返回值: bool
    *   其他说明:   2004-8-19
    */
    function _isExists ( $id )
    {
        if ( isset ( $_SESSION['cart'][$id] ) && !empty ( $_SESSION['cart'][$id] ) && array_key_exists ( $id,$_SESSION['cart'] ) )
        {
            Return true;
        }
        else
        {
            Return false;
        }
    }    /**
    *   函数名称:   modItem
    *   函数功能:   修改商品数量
    *   输入参数:   $id -------------- 商品ID
    *              $num ------------- 商品数量
    *   函数返回值: 返回值说明
    *   其他说明:   说明
    */
    function modItem ( $id, $num )
    {
        $arr = $this->getItems ( $id );
        // 如果商品存在就加数量和价格
        if ( $this->_isExists ( $id ) )
        {
            $_SESSION['cart'][$id]["count"] = intval ( $arr['price'] ) * intval ( $num );
            $_SESSION['cart'][$id]["num"]   = intval ( $num );
        }
    }    /**
    *   函数名称:   getItems
    *   函数功能:   取得商品数组
    *   输入参数:   $id --------------- 某商品的ID
    *   函数返回值: array
    *   其它说明:   2004-8-19
    */
    function getItems ( $id = null )
    {
        if ( isset ( $_SESSION['cart'] ) )
        {
            if ( $id == null )
            {
                Return $_SESSION['cart'];
            }
            else
            {
                Return $_SESSION['cart'][$id];
            }
        }
    }    /**
    *   函数名称:   emptyItem
    *   函数功能:   删除商品
    *   输入参数:   $id ----------- 商品ID
    *   函数返回值: bool
    *   其它说明:   2004-8-19
    */
    function emptyItem ( $id = null )
    {
        if ( $id == null )
        {
            unset ( $_SESSION['cart'] );
        }
        else
        {
            unset ( $_SESSION['cart'][$id] );
        }
    }    /**
    *   函数名称:   sum
    *   函数功能:   统计总价
    *   输入参数:   none
    *   函数返回值: int
    *   其它说明:   2004-8-19
    */
    function sum (  )
    {
        $total = 0;
        if ( isset ( $_SESSION['cart'] ) && !empty ( $_SESSION['cart'] ) )
        {
            foreach ( $_SESSION['cart'] as $key => $val )
            {
                $total += $val['count'];
            }
        }
        Return $total;
    }
}
?><?php
/**************************************************************************
Copyright (C), 2004, ustb
文件名: testcart.php
作  者: [偶然]
说  明: 测试购物车
版  本: 1.0
日  期: 2004-8-19
历  史:
<作者>          <时间>          <版本>       <说明>
[偶然]         2004-8-19        1.0         创建模块
**************************************************************************/
session_start();
require_once "cart.class.php";
$cart = new cart();// 添加
if(isset($_POST['action'])&&!empty($_POST['action'])&&$_POST['action']=="additem")
{
    $postitempid   = isset($_POST['itempid'])&&!empty($_POST['itempid'])?$_POST['itempid']:null;
    $postitemname  = isset($_POST['itemname'])&&!empty($_POST['itemname'])?$_POST['itemname']:null;
    $postitemprice = isset($_POST['itemprice'])&&!empty($_POST['itemprice'])?$_POST['itemprice']:null;
    $postitemnum = isset($_POST['itemnum'])&&!empty($_POST['itemnum'])?$_POST['itemnum']:null;
    $arr = array(
        "$postitempid" =>array(
            "name"  => $postitemname,
            "price" => $postitemprice,
            "num"   => $postitemnum
        )
    );
    $cart->addItem($arr);
}
// 删除
if(isset($_GET['act'])&&!empty($_GET['act'])&&$_GET['act']=="del"&&!empty($_GET['id']))
{
    $cart->emptyItem($_GET['id']);
}
// 清空
if(isset($_GET['act'])&&!empty($_GET['act'])&&$_GET['act']=="clean")
{
    $cart->emptyAll();
    header("location: testcart.php");
}// 改
if(isset($_POST['action'])&&$_POST['action']=="修改")
{
 //print_r($_POST);
    $postpnum  = isset($_POST['pnum'])&&!empty($_POST['pnum'])?$_POST['pnum']:null;
    if(!empty($postpnum))
    {
        foreach($postpnum as $key=>$val)
        {
            $cart->modItem($key,$val[0]) ;
        }
    }
}
?>
<a href="?act=clean">清空</a>
<table border=1 width="700" align="center">
<form name="form_cart" method="post" action="">
<?php
// 取得
$getarr = $cart->getItems();
if(is_array($getarr)&&!empty($getarr))
{
    // 列表
    foreach($getarr as $pkey=>$pval)
    {
        if($pkey!=null)
        {
?>
<tr>
    <td>ID:<?=$pkey?></td>
    <td>名称:<?=$pval['name']?></td>
    <td>单价:<?=$pval['price']?></td>    <td>数量:<input type="text" name="pnum[<?=$pkey?>][]" value="<?=$pval['num']?>"></td>
    <td>合计<?=$pval['count']?></td>
    <td><a href="?act=del&id=<?=$pkey?>">删除</a></td>
</tr>
<?php
        }
    }
}
?>
<tr>
<td colspan="6"><input type="submit" name="action" value="修改">总计:<?=$cart->sum()?></td>
</tr>
</form>
</table>
因为只做测试,所以不对数量,ID等参数做严格限制,爱填什么就填什么.
<form name="form_add" method=post action="">
ID:<input type="text" name="itempid">
名称:<input type="text" name="itemname">
单价:<input type="text" name="itemprice">
数量:<input type="text" name="itemnum" value="">
<input type="hidden" name="action" value="additem">
<input type="submit">
</form>

解决方案 »

  1.   

    to:zero 总是报错
    Warning: Cannot use a scalar value as an array in d:\apache\htdocs\cart.class.php on line 31Warning: Cannot use a scalar value as an array in d:\apache\htdocs\cart.class.php on line 32Warning: Cannot use a scalar value as an array in d:\apache\htdocs\cart.class.php on line 33Warning: Cannot use a scalar value as an array in d:\apache\htdocs\cart.class.php on line 34Warning: Cannot use a scalar value as an array in d:\apache\htdocs\cart.class.php on line 35
      

  2.   

    呵呵,我用的不是类,和你标题不附
    看了下楼上贴的 数据结构
        $arr = array(
            "$postitempid" =>array(
                "name"  => $postitemname,
                "price" => $postitemprice,
                "num"   => $postitemnum
            )
    其实最简单的购物车数据结构是 $cart[商品编号]=定购数量
      

  3.   

    sorry ! 这个类是有问题的..不好意思..偶没测试就帖上来了.真是对不起..
      

  4.   

    我有个SESSION购物车的小程序,不过不是类
    要得话给我邮件
    [email protected]
      

  5.   

    http://blog.csdn.net/pwtitle
    有现成的。