<?//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;
当我在次添加的时候,就出现这样的错误. 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 .
现在已经解决了那个错误. 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>"; } 这样对吗?
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>";
}
//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
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>";
你能不能帮我修改一下这个类.然后做个添加的实例,小弟在这里先谢谢了.
能不能帮我修改一下这个类.然后做个可以添加商品的实例,小弟在这里先谢谢了.
{
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);
?>
xuzuning(唠叨) 和各位兄台,怎么办?
$s_id = "002";
$s_name = "test2";
$s_price = 10;
$s_num = 1;
session_unset();
session_destroy();
还有没有其他要注意的地方呢?
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 )
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 .
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>";
} 这样对吗?