![]() |
|
首页 │ Apache │ Linux│ Java│ MySQL│ 注册│帮助 | |||
这是一个显示购物车的类,别的地方都没有任何问题.唯一的问题,就是我想通过Jscript来实现一个检验用户输入的商品数量是否大于库存数量,如果大于库存数量的话就弹出一个窗口,提示"**商品库存不足,请顾客修改数量". 大家可以直接查看//显示购物车的函数.function showcart().这个函数里我先从数据库里取出每个商品的库存数量$reserve1,2,3....和购物车中的商品总数$count,并把它们传给Jscrip,可问题是: 比如第一个商品的库存量是5,用户输入的数量是6的话,结果并没有弹出窗口提示库存数量$reserve1.这是怎么回事啊?! 在线等待各位大侠指导,先谢过!! <? session_start(); //本文件是购物车类的定义 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) { if($this->count==0) { session_register("cart"); } //设定标志变量 $flag="FALSE"; for($i=1;$i<=$this->count;$i++) { //检查在购物车里是否已有相同商品 //如果有的话,设定标志为真 //相应的的商品数量加一 if($this->cartitem[$i]["productid"]==$productid) { $this->cartitem[$i]["quantity"]++; $flag="TRUE"; } } //如果标志变量为假,说明在购物车里没有相同的商品 //向购物车里面加入商品名,id,价格和数量. if($flag=="FALSE") { $connect=mysql_connect("localhost","Wdwlbsm"); mysql_select_db("toyskingdomwg"); $query="select Productid,Price,Productname,Reserve from product where Productid='$productid'"; $result=mysql_query($query); $res=mysql_fetch_object($result); $price=$res->Price; $productname=$res->Productname; $productid=$res->Productid; $reserve=$res->Reserve; mysql_close($connect); $this->count++; $this->cartitem[$this->count]["productid"]=$productid; $this->cartitem[$this->count]["reserve"]=$reserve; $this->cartitem[$this->count]["quantity"]=1; $this->cartitem[$this->count]["price"]=$price; $this->cartitem[$this->count]["productname"]=$productname; } } //删除购物车中某一商品的函数 function remove_item($productid) { //设置标志变量 $flag=FALSE; for($i=1;$i<=$this->count;$i++) { //寻找删除商品的id. if($this->cartitem[$i]["productid"]==$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 set_owner() { $this->owner=$userid; } //显示购物车的函数. function showcart() { include"head.php"; echo'
![]() ![]() |



