麻辣堂|资源主站|开发论坛|在线手册
首页 Apache Linux Java MySQL 注册帮助 
PHP项目开发组是PHP开发资源网于2007组年建成立的项目开发团队,目前核心开发成员有27人, 项目协作成员8名.下设7个开发组,主要承接大/中型网站项目开发任务。

    由于开发任务较多,人员比较紧张,现面向社会招聘全职或者兼职开发人员,不管你是在校大学生,还是全职开发人员,以及SOHO都可以联系本站,我们可以长期合作,并为您带来丰厚的报酬。
  您现在的位置:PHP开发资源网 > 麻辣堂 > 详细资料
待解决
希望有哪位老大能看完我以下的程序,因为这个问题困扰我好些天了,在线等待各位大侠
悬赏分:20 - 2007年08月17日

这是一个显示购物车的类,别的地方都没有任何问题.唯一的问题,就是我想通过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'
'; echo'
'; $total=0; for($i=1;$i<=$this->count;$i++) { echo' '; echo' '; $reserve=$this->cartitem[$i]["reserve"]; echo"<script >var reserve$i= $reserve;</script>"; //生成javascript脚本 echo' '; $sum[$i]=$this->cartitem[$i]["quantity"]*$this->cartitem[$i]["price"]; echo' '; echo' '; echo' '; $total+=$sum[$i]; } $count=$this->count; echo"<script>var count= $count; </script>"; //生成javascript脚本 echo'
Toys Name Quantity Price Total DEL
'.$this->cartitem[$i]["productname"].' '.$this->cartitem[$i]["price"].' USD'.$sum[$i].'cartitem[$i]["productid"].'>Del this Toys
Total Price: '.$total.' USD.
'; include"foot.php"; } //更新购物车的函数. function updatecart($cartinfo) { //这个变量用来统计要删除的商品的数目 $delcount=0; for($i=1;$i<=$this->count;$i++) { //改变商品数量 $this->cartitem[$i]["quantity"]=$cartinfo[$i]; //如果数量为零的话,记录下来 if($cartinfo[$i]==0) { $delcount++; $del[$delcount]=$this->cartitem[$i]["productid"]; } } //删除数量为零的商品 for($j=1;$j<=$delcount;$j++) { $this->remove_item($del[$j]); } } } ?> <script type="text/javascript"> function check_req_fields() { var errmsg = ''; var inputs = document.getElementsByTagName('input'); for (var i = 0; i < inputs.length; i++) { if ('text' == inputs.type) { if (eval('reserve'+(i+1))< inputs.value) { errmsg =eval('reserve'+(i+1)); } } } if (errmsg == '') { return true; } else { window.alert(errmsg); return false; } } </script>

提问者:hthc   08-17 17:05
答复
路过。。。顺便帮顶:)
回答者:玉米づ冰冻可乐 - 瓦岗村民 8-22 09:10
我也来回答:
不管你有没有帮助我们,瓦岗寨8万村民将感谢你。。。。。

为防止灌水,您需要计算一道数学题: 答案:
29 + 36 = ? 请将计算结果填在上面

 
[]
©2007 PhpRes.COM