![]() |
|
首页 │ Apache │ Linux│ Java│ MySQL│ 注册│帮助 | |||
这是showproduct.php页: <? include "cart.php"; //包含购物车类的文件 .........通过数据库查找物品,按"放入购物车"按钮后,$job的值改为"addproduct".(这部分能正常显示,没问题,省略,关键是下面按"放入购物车"后,只能显示购物车,但里面没有刚添加的物品) //如果参数为添加商品,执行操作. if($job=="addproduct") { if(isset($cart)) { $cart->add_item($productid); } else { $cart=new cart; $cart->add_item($productid); <---------cart.php里显示添加商品的函数,$productid为商品号. $cart->showcart(); <---------cart.php里显示购物车的函数 } } ........... ?> 这是cart.php页有关添加商品,和显示购物车函数的部分: <? //本文件是购物车类的定义 class Cart { var $Cartitem; //此变量用来存放商品数据,实质是二维数组 var $count; //商品数量 var $owner; //用户id var $time; //时间 var $cartid; //购物车编号 //初始化结构函数 function Cart() { $this->count=0; if(isset($userid)) { $this->owner=$userid; } 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++) { //检查在购物车里是否已有相同商品 //如果有的话,设定标志为真 //相应的的商品数量加一 if($this->cartitem[$i]["productid"]==$productid) { $this->cartitem[$i]["quantity"]++; $flag=TRUE; break; } } //如果标志变量为假,说明在购物车里没有相同的商品 //向购物车里面加入商品名,id,价格和数量. if($flag=FALSE) { session_register("cart"); $connect=mysql_connect("localhost","Wdwlbsm"); mysql_select_db("ebusiness"); $query="select productid,price,productname from product where productid='$productid'"; $result=mysql_query($query); $res=mysql_fetch_object($result); $price=$res->price; $productname=$res->productname; $productid=$res->productid; mysql_close($connect); $this->count++; $this->cartitem[$count]["productid"]=$productid; $this->cartitem[$count]["quantity"]=1; $this->cartitem[$count]["price"]=$price; $this->cartitem[$count]["productname"]=$productname; } } //显示购物车的函数. function showcart() { echo'
'; } } ?> 为什么它显示不出我添加了的商品呢?!
