哦,对不起啊,就是代码出来问题,系统提示是
Fatal error: Call to a member function showCart() on a non-object in F:\xampp\htdocs\php\d_showall.php on line 24
Fatal error: Call to a member function showCart() on a non-object in F:\xampp\htdocs\php\d_showall.php on line 24
class Cart {
var $count;
var $owner;
var $cartid;
var $cartitem = array(); //初始化结构函数
function Cart(){
$this->count = 0;
if(isset($id)){
$this -> owner = $id;
}else{
$this -> owner = 0;
}
$this->time = date("Y-m-d");
$this->cartid = time();
}
//添加商品
function add_item($productId){
//设置标志
$flag = false;
for($i =1; $i <= $this ->count; $i++){
//检查在购物车中是否已经有相同的商品
//如何有的话,设置标志为真
//相应商品数量增加1
if($this ->cartitem[$i]["p_id"] = $productId){
$this ->cartitem[$i]["p_quantity"]++;
$flag = true;
break;
} }
//如果标志变量为假,说明在购物车中没有相同的商品
//向购物车中加入该商品相关信息
if(!$flag){
require("db.php");
$str = "select p_name,p_price from product where p_id =".$productId;
$result = mysql_query($str);
$res = mysql_fetch_object($result);
$pname = $res -> p_name;
$price = $res -> p_price;
$this -> count++;
//添加商品数据
$this -> cartitem[$this -> count]["p_id"] = $produceId;
$this -> cartitem[$this -> count]["p_name"] = $pname;
$this -> cartitem[$this -> count]["p_price"] = $price;
$this -> cartitem[$this -> count]["p_quantity"] = 1;
$flag = true;
}
}
//删除购物车中的某一商品
function removeItem($productId){
//设置标志变量
$flag = false;
for($i = 1; $i <=$this ->count;$i++){
//循环寻找删除商品ID
if($this -> cartitem[$i]["p_id"] == $productId) {
$j = $i;
$flag = true;
break;
}
}
if($flag){
//执行删除操作,实际上就是被删除后的每一项往前移动
for($i = $j; $i <= $this ->count -1;$i++){
$this ->cartitem[$i] = $this -> cartitem[$i+1];
}
$this ->count--;
}
}
//设定用户ID函数,其作用就是用于用户在购物后才登录的情况
function setOwner($id){
$this -> owner = $id;
}
//显示购物车
function showCart($id){
echo " <form method = 'post' action = 'd_showall.php'>
<table border = '1' width = '500'>
<tr align = 'center'>
<td width = '100' height = '119'>商品名称 </td>
<td width = '100' height = '119'>数量 </td>
<td width = '100' height = '119'>商品价格 </td>
<td width = '100' height = '119'>商品总计 </td>
<td width = '100' height = '119'>删除 </td>
</tr>
<tr align = 'center'>";
$total = 0;
for($i = 1; $i = $this -> count; $i++){
echo " <td width = '100'>".$this -> cartitem[$i]["p_name"]." </td>";
echo " <td width = '100'> <input type = 'text' name = 'a".$i."' size='7' maxlength = '7' value ='".$this -> cartitem[$i][p_quantity]."'> </td>";
echo " <td width = '100'>".$this -> cartitem[$i]["p_price"]." </td>";
$sum = $this -> cartitem[$i]["p_quantity"]*$this ->cartitem[$i]["p_price"];
$total = $total + $sum;
echo " <td width = '100'>".$total." </td>";
echo " <td width = '100'> <a href = 'd_showall.php?job = del&id = ".$this -> cartitem[$i]["p_id"]."'>删除 </a> </td>";
echo " </tr>";
}
echo " </table>";
echo " <p>总金额:".$total."元 </p>";
echo " <p> <input type ='hidden' name = 'job' value = 'update'> <input type = 'submit' name = 'submit' value ='更改商品数量'> <a href ='".$_SERVER['PHP_SELF']."'>刷新购物车 </a> <a href = 'd_default.php'>继续购物 </a> <a href = 'check.php?total =".$total."&id = ".$id."'>去结算中心 </a> <a href = 'd_showall.php?job = clear'>清空购物车 </a> </p>";
echo " </form>";
}
//更新购物车
function updateCart($cartInfo){
$delcount = 0; //用来统计要删除的商品数目
for($i = 1; $i = $this -> count; $i++){
//改变商品数量
$this -> cartitem[$i]["p_quantity"] = $cartInfo[$i];
//如果数量为0,记录下来
if($cartInfo[$i] == 0){
$delcount ++;
$del[$delcount] == $this -> cartitem[$i]["p_id"];
}
}
//删除数量为0的商品
for($j = 1; $j <= $delcount; $j++){
$this ->removeItem($del[$j]);
}
}
}
?>
//购物车显示与更新
include_once("d_cart.php");
session_start();
$cart = new Cart();
if(!isset($_SESSION["id"])){
$_SESSION["id"] = $_GET["id"];
}
//如果job参数不是clear ,执行下面的语句
if($job != "clear"){
//如果job = del,则执行删除操作
if($job == "del"){
$cart -> removeItem($id);
}
//如果job=update,则更新
if($job == "update"){
echo "begin update".$cart ->count;
for($i = 1;$i <= $cart ->count; $i++){
$a = "a".$i;
$cartInfo[$i] == $$a;
}
$cart -> updateCart($cartInfo);
}
//显示购物车
$cart ->showCart($id);
print_r($id);
exit;
}else{
//清空操作
unset($_SESSION["cart"]);
echo "您的购物车已被清空!";
} ?>