购物车练习
Posted 格调evo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了购物车练习相关的知识,希望对你有一定的参考价值。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script src="../jquery-1.11.2.min.js"></script> <style type="text/css"> #tou{ font-family:"微软雅黑"; font-size:36px; margin-top:20px; margin-left:600px} #main{ width:100%; background-color:#6CC} #right{ width:60%;background-color:#F93; float:left;} #left{ width:20%; font-family:"微软雅黑"; font-size:18px; float:left; margin-left:100px} #dd{ font-family:"微软雅黑"; font-size:18px; margin-left:1000px;} </style> </head> <body> <?php session_start(); if(empty($_SESSION["uid"])) { header("location:login.php"); exit; } include("../DBDA.class.php"); $db=new DBDA(); $sql="select * from fruit"; $attr=$db->Query($sql); $gouwuche=array(); if(!empty($_SESSION["gwc"])) { $gouwuche=$_SESSION["gwc"]; } $shuliang=count($gouwuche);//商品数量 //总价 $zongjia=0; foreach($gouwuche as $v) { $sprice="select Price from fruit where Ids=‘{$v[0]}‘"; $price=$db->StrQuery($sprice); $zongjia=$zongjia+$price*$v[1]; } ?> <div id="tou">购物网</div> <div id="dd"><a href="login.php">登录</a></div> <div id="main"> <div id="left"> <div><a href="index.php">浏览商品</a></div> <div><a href="gouwuche.php">查看购物车</a></div> <div><a href="zhanghu.php">查看账户</a></div> </div> <div id="right"> <div> 购物车中有<?php echo $shuliang ?>件商品,总价<?php echo $zongjia ?>元 </div> <table width="100%" border="1" cellpadding="0" cellspacing="0" id="zhuti"> <tr> <td>代号</td> <td>水果名称</td> <td>价格</td> <td>产地</td> <td>库存</td> <td></td> </tr> <?php foreach($attr as $v) { echo"<tr> <td>{$v[0]}</td> <td>{$v[1]}</td> <td>{$v[2]}</td> <td>{$v[3]}</td> <td>{$v[4]}</td> <td><a href=‘dingdanchuli.php?code={$v[0]}‘>购买</a></td> </tr>"; } ?> </table> </div> </div> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <style type="text/css"> *{ margin:0px auto} #tou{ font-family:"微软雅黑"; font-size:36px; margin-top:20px; margin-left:600px} #dd{ margin-left:530px; margin-top:50px} #mima{ margin-top:10px} #ddd{ margin-left:120px; margin-top:10px} </style> </head> <body> <div id="tou">登录页面</div> <div id="dd"> <form action="loginchuli.php" method="post"> <div>用户名:<input type="text" name="uid" /></div> <div id="mima">密码: <input type="password" name="pwd" /></div> <div id="ddd"><input type="submit" value="登录" /></div> </form> </div> </body> </html>
<?php session_start(); include("../DBDA.class.php"); $db=new DBDA(); $uid=$_POST["uid"]; $pwd=$_POST["pwd"]; $sql="select PassWord from login where UserName=‘{$uid}‘"; $mima=$db->StrQuery($sql); if($pwd==$mima && $pwd !="") { $_SESSION["uid"]=$uid; header("location:index.php"); } else { echo "用户名或密码错误"; }
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script src="../jquery-1.11.2.min.js"></script> <style type="text/css"> #tou{ font-family:"微软雅黑"; font-size:36px; margin-top:20px; margin-left:600px} #main{ width:100%; background-color:#6CC} #right{ width:60%;background-color:#F93; float:left;} #left{ width:20%; font-family:"微软雅黑"; font-size:18px; float:left; margin-left:100px} #dd{ font-family:"微软雅黑"; font-size:18px; margin-left:1000px;} </style> </head> <body> <?php session_start(); if(empty($_SESSION["uid"])) { header("location:login.php"); exit; } include("../DBDA.class.php"); $db=new DBDA(); $attr=array(); if(!empty($_SESSION["gwc"])) { $attr=$_SESSION["gwc"]; } ?> <div id="tou">购物车</div> <div id="dd"><a href="login.php">登录</a></div> <div id="main"> <div id="left"> <div><a href="index.php">浏览商品</a></div> <div><a href="gouwuche.php">查看购物车</a></div> <div><a href="zhanghu.php">查看账户</a></div> </div> <div id="right"> <table width="100%" border="1" cellpadding="0" cellspacing="0" id="zhuti"> <tr> <td>水果名称</td> <td>单价</td> <td>数量</td> <td></td> </tr> <?php foreach($attr as $v) { $smcdj="select * from fruit where Ids=‘{$v[0]}‘"; $sginfo=$db->Query($smcdj); echo "<tr> <td>{$sginfo[0][1]}</td> <td>{$sginfo[0][2]}</td> <td>{$v[1]}</td> <td><a href=‘delete.php?code={$v[0]}‘>删除</a></td> </tr>"; } ?> </table> <div> <a id="tj" href="tijiao.php">提交订单</a> <span id="xinxi"></span> </div> </div> </div> </body> <script type="text/javascript"> $(document).ready(function(e) { $("#tj").click(function(){ var bs=false; $.ajax({ async:false, url:"check.php", dataType:"TEXT", success: function(data){ if(data.trim()=="ok") { bs=true; } else { $("#xinxi").html(data); $("#xinxi").css("color","#F00"); } } }); return bs; }) }); </script> </html>
<?php session_start(); include("../DBDA.class.php"); $db=new DBDA(); //取出购物车信息 $attr=$_SESSION["gwc"]; //取出登录者用户名 $uid=$_SESSION["uid"]; //总价 $zongjia=0; foreach($attr as $v) { $sprice="select Price from fruit where Ids=‘{$v[0]}‘"; $price=$db->StrQuery($sprice); $zongjia=$zongjia+$price*$v[1]; } //余额 $sql="select Account from login where UserName=‘{$uid}‘"; $yue=$db->StrQuery($sql); if($yue>=$zongjia) { //判断库存 $daihao=""; foreach($attr as $v) { //取库存 $skucun="select Numbers from fruit where Ids=‘{$v[0]}‘"; $kucun=$db->StrQuery($skucun); //如果库存小于数量 if($kucun<$v[1]) { $daihao=$v[0]; break; } } if($daihao=="") { echo "ok"; } else { $sname="select Name from fruit where Ids=‘{$daihao}‘"; $name=$db->StrQuery($sname); echo "{$name}库存不足"; } } else { echo "余额不足"; }
<?php session_start(); //$_SESSION["gwc"]; $code=$_GET["code"]; /*$attr=array( array("f001",1) );*/ //第一次点击购买 if(empty($_SESSION["gwc"])) { /*$attr=array();//造二维数组外层数组 $anei=array();//造二维数组里层数组 $anei[]=$code; $anei=1; $attr[]=$anei;*/ $attr=array( array($code,1) ); $_SESSION["gwc"]=$attr; } else { //已经点击过购买 $attr=$_SESSION["gwc"]; //判断该水果是否购买过 $count=0; foreach($attr as $v) { if($v[0]==$code) { $count++; } } //该水果在购物车中存在 if($count>0) { foreach($attr as $k=>$v) { if($v[0]==$code) { $attr[$k][1]=$v[1]+1; } } } else { //该水果在购物车没有出现过 $anei=array($code,1); $attr[]=$anei; } //将修改的数组添加到session里 $_SESSION["gwc"]=$attr; } header("location:index.php");
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script src="../jquery-1.11.2.min.js"></script> <style type="text/css"> #tou{ font-family:"微软雅黑"; font-size:36px; margin-top:20px; margin-left:600px} #main{ width:100%; background-color:#6CC} #right{ width:60%;background-color:#F93; float:left;} #left{ width:20%; font-family:"微软雅黑"; font-size:18px; float:left; margin-left:100px} #dd{ font-family:"微软雅黑"; font-size:18px; margin-left:1000px;} </style> </head> <body> <?php session_start(); if(empty($_SESSION["uid"])) { header("location:login.php"); exit; } include("../DBDA.class.php"); $db=new DBDA(); $uid=$_SESSION["uid"]; $sql="select Account from login where UserName=‘{$uid}‘"; $yue=$db->StrQuery($sql); ?> <div id="tou">购物车</div> <div id="dd"><a href="login.php">登录</a></div> <div id="main"> <div id="left"> <div><a href="index.php">浏览商品</a></div> <div><a href="gouwuche.php">查看购物车</a></div> <div><a href="zhanghu.php">查看账户</a></div> </div> <div id="right"> 您的账户余额为<?php echo $yue ?>元 </div> </table> </div> </div> </body> </script> </html>
<?php session_start(); include("../DBDA.class.php"); $db=new DBDA(); //购物车信息 $attr=$_SESSION["gwc"]; //取登陆者用户名 $uid=$_SESSION["uid"]; //总价 $zongjia = 0; foreach($attr as $v) { $sprice = "select price from fruit where ids=‘{$v[0]}‘"; $price = $db->StrQuery($sprice); $zongjia = $zongjia + $price*$v[1]; } //1.减余额 $sjian="update login set Account=Account-{$zongjia} where UserName=‘{$uid}‘"; $db->Query($sjian,0); //2.减库存 foreach($attr as $v) { $skjian="update fruit set Numbers=Numbers-{$v[1]} where Ids=‘{$v[0]}‘"; $db->Query($skjian,0); } //3.添加订单信息 $ordercode=date("YmdHis"); $time=date("Y-m-d H:i:s"); $sdingdan="insert into orders values(‘{$ordercode}‘,‘{$uid}‘,‘{$time}‘)"; $db->Query($sdingdan,0); //添加订单详情 foreach($attr as $v) { $sxiangqing="insert into orderdetails values(‘‘,‘{$ordercode}‘,‘{$v[0]}‘,‘{$v[1]}‘)"; $db->Query($sxiangqing,0); } header("location:index.php");
<?php session_start(); $attr=$_SESSION["gwc"]; $code=$_GET["code"]; foreach($attr as $k=>$v) { if($v[0]==$code) { //判断数量是否大于一 if($v[1]>1) { $attr[$k][1]=$attr[$k][1]-1; } else { unset($attr[$k]); //unset删除 } } } $_SESSION["gwc"]=$attr; header("location:gouwuche.php");
以上是关于购物车练习的主要内容,如果未能解决你的问题,请参考以下文章