怎么样调用来显示呢
我总是显示一条.
大家觉得echo "$this->s_id,$this->s_name,$this->s_price,$this->s_num,$this->s_total,$this->s_count";有没有错?

解决方案 »

  1.   

    修改s_rcart() 为
    function s_rcart() //将session数据回到数组中
    {
    $this->s_id  =  $_SESSION['s_id'];
    $this->s_name  =  $_SESSION['s_name'];
    $this->s_price  =  $_SESSION['s_price'];
    $this->s_num  =  $_SESSION['s_num'];
    $this->s_total  =  $_SESSION['s_total']; $this->s_count   =  count($_SESSION['s_id']);
       for($i=0;$i<$this->s_count;$i++)
    echo "$this->s_id[$i],$this->s_name[$i],$this->s_price[$i],$this->s_num[$i],$this->s_total[$i],$this->s_count[$i]<br>";
    }
      

  2.   

    <?
       //include ("include/conn.php");
       //在此之前要获得相关变量.
    require("Cart.inc");
    session_start();
    $s_id=$_POST['bianhao'];
    $s_name=$_POST['mingcheng'];
    $s_price=$_POST['jiage'];
    $s_num=$_POST['shuliang'];if(!isset($_SESSION['cart'])) {
       $cart = new Cart;
      $_SESSION['cart'] = $cart;
    }   $cart->add_cart($s_id,$s_name,$s_price,$s_num);
       $cart->s_cart(); ?>
    我这样调用对吗.
    但是怎么会报错呢?
    g111[0],sdfsdfsad[0],32[0],1[0],Array[0],1[0]Fatal error: [] operator not supported for strings in /apache/htdocs/prog/supervisor/addgift/Cart.inc on line 36
      

  3.   


    echo "{$this->s_id[$i]},{$this->s_name[$i]},{$this->s_price[$i]},{$this->s_num[$i]},{$this->s_total[$i]},{$this->s_count[$i]}<br>";
      

  4.   

    xuzuning(唠叨)老大,还是有错.
    你能不能帮我修改一下这个类.然后做个添加的实例,小弟在这里先谢谢了.
      

  5.   

    各位大哥帮帮忙.用php session做购物车我没有什么经验.
    能不能帮我修改一下这个类.然后做个可以添加商品的实例,小弟在这里先谢谢了.
      

  6.   

    <?//Cart.incclass Cart
    {
    var $s_id = array();
    var $s_name = array();
    var $s_price = array();
    var $s_num = array();
    var $s_total = array();
    var $s_count;
    var $i;

    function s_rcart() //显示购物车
    {
    $this->s_count   =  count($this->s_id);
    for($i=0;$i<$this->s_count;$i++)
    echo "{$this->s_id[$i]},{$this->s_name[$i]},{$this->s_price[$i]},{$this->s_num[$i]},{$this->s_total[$i]},{$this->s_count[$i]}<br>";
    }
    function add_cart($s_id,$s_name,$s_price,$s_num) //加入车
    {
    $this->s_rcart();
    if(!$this->in_cart($s_id))
    {
    $this->s_id[]     = $s_id;
    $this->s_name[]   = $s_name;
    $this->s_price[]  = $s_price;
    $this->s_num[]    = $s_num;
    $this->s_total[]  = $s_num*$s_price;
    }
    }

    function If_in_cart($s_id)                            //是否在车里
    {
    for($i=0;$i<=$this->s_count;$i++)
    {
    if($this->s_id[$i]==$s_id)
    return true;
    }
    return false;
    }

    function in_cart_of($s_id)                            //在车里的id
    {
    for($i=0;$i<=$this->s_count;$i++)
    {
    if($this->s_id[$i]==$s_id)
    return $i;
    }
    }
    function in_cart($s_id)                                   //是不是在车里,如在则在更改数量及总额
    {
    if($this->If_in_cart($s_id))
    {
    $i=$this->in_cart_of($s_id);
    $this->s_num[$i]=$this->s_num[$i]+1;
    $this->s_total[$i]=$this->s_num[$i]*$this->s_price[$i];
    return true;

    }
    return false;
    }

    function edit_cart($s_id,$s_name,$s_price,$s_num) //修改
    {
    $this->s_rcart();
    $i=$this->in_cart_of($s_id); $this->s_id[$i]     = $s_id;
    $this->s_name[$i]   = $s_name;
    $this->s_price[$i]  = $s_price;
    $this->s_num[$i]    = $s_num;
    $this->s_total[$i]  = $s_num*$s_price; $this->s_update();
    }


    function s_remove($s_id) //删除某物
    {
    $this->s_rcart();
    for($i=0;$i<=$this->s_count;$i++)
    {
    if($this->s_id[$i]==$s_id)
    {
    unset($this->s_id[$i]);
    unset($this->s_name[$i]);
    unset($this->s_price[$i]);
    unset($this->s_num[$i]);
    unset($this->s_total[$i]);
    }
    }
    $this->s_update();
    }


    function s_reset() //清空
    {
    unset($this->s_id);
    unset($this->s_name);
    unset($this->s_price);
    unset($this->s_num);
    unset($this->s_total);

    unset($_SESSION['s_id']);
    unset($_SESSION['s_name']);
    unset($_SESSION['s_price']);
    unset($_SESSION['s_num']);
    unset($_SESSION['s_total']);
    }

    }
    /*************************************************************
    调用
    */
    session_start();
    if(!isset($_SESSION['cart']))
      $_SESSION['cart'] = new Cart;$cart =& $_SESSION['cart'];
    $s_id = "002";
    $s_name = "test2";
    $s_price = 10;
    $s_num = 1;
    $cart->add_cart($s_id,$s_name,$s_price,$s_num);print_r($cart);
    ?>
      

  7.   

    我添加了几条。怎么显示的还是一条呀!
     xuzuning(唠叨) 和各位兄台,怎么办?
      

  8.   

    改变以下数据,刷新
    $s_id = "002";
    $s_name = "test2";
    $s_price = 10;
    $s_num = 1;
      

  9.   

    unset($_SESSION['s_id']);这样不好吧,标准些好:$_SESSION = array();
    session_unset();
    session_destroy();
      

  10.   

    哦.我试一试,谢谢 xuzuning(唠叨)老大的教诲.
    还有没有其他要注意的地方呢?
      

  11.   

    郁闷,现在读出来的居然是:
    cart Object ( [s_id] => Array ( [0] => g111 ) [s_name] => Array ( [0] => sdfsdfsad ) [s_price] => Array ( [0] => 32 ) [s_num] => Array ( [0] => 1 ) [s_total] => Array ( [0] => 32 ) [s_count] => 0 )
      

  12.   

    当我在次添加的时候,就出现这样的错误.
    Fatal error: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition cart of the object you are trying to operate on was loaded _before_ the session was started .
      

  13.   

    现在已经解决了那个错误.
    Fatal error: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition cart of the object you are trying to operate on was loaded _before_ the session was started .
    不好意思.我没有在session开始之前调用上面的那个类.
    但是现在读出来的怎么是:
    cart Object ( [s_id] => Array ( [0] => g111 ) [s_name] => Array ( [0] => sdfsdfsad ) [s_price] => Array ( [0] => 32 ) [s_num] => Array ( [0] => 1 ) [s_total] => Array ( [0] => 32 ) [s_count] => 0 )
    呢?
    我是这样定义那个显示函数的:
    function s_rcart() //显示购物车
    {
    $this->s_id  =  $_SESSION['s_id'];
    $this->s_name  =  $_SESSION['s_name'];
    $this->s_price  =  $_SESSION['s_price'];
    $this->s_num  =  $_SESSION['s_num'];
    $this->s_total  =  $_SESSION['s_total']; $this->s_count   =  count($_SESSION['s_id']);
    //$this->s_count   =  count($this->s_id);
    echo "<table width=727 align=center border=1><tr><td>商品 id </td><td>商品名稱</td><td>商品價格</td><td>商品數量</td><td>商品總價格</td><td>商品總價格</td><td>刪除與否</td></tr>";
    for($i=0;$i<$this->s_count;$i++)
    echo "<tr><td>{$this->s_id[$i]}</td><td>{$this->s_name[$i]}</td><td>{$this->s_price[$i]}</td><td>{$this->s_num[$i]}</td><td>{$this->s_total[$i]}</td><td>{$this->s_count[$i]}<td><input type=submit name=Submit2 value=刪除></td></tr>";
    echo "</table>";
    } 这样对吗?