index.php //入口文件
<?php/** * @name index.php * @decs * @author 老猫 <18368091722@163.com> * updated on: 2019/5/30 9:49 */include "goods.php";
conn.inc.php //数据库连接常量设置
<?php/** * @name conn.inc.php * @decs 数据库常量配置 * @author 老猫 <18368091722@163.com> * updated on: 2019/5/30 9:43 */define("host",'13.232.50.253');define("ur",'liuyu');defin瓦解的意思e("pwd",'94492474');define("dbname",'onecms');
mysqli.php //数据库连接
<?php/** * @name mysqli.php * @decs 数据库连接 * @author 老猫 <18368091722@163.com> * updated on: 2019/5/30 9:44 */include 'conn.inc.php';$mysqli=new mysqli(host,ur,pwd,dbname);if($mysqli->connect_errno){ die('数据库连接出现错误,请检查配置'.$mysqli->connect_error);}
goods.php //商品展示
<?php/** * @name goods.php * @decs * @author 老猫 <18368091722@163.com> * updated on: 2019/5/30 9:46 */header("content-type:text/html;chart=utf-8");include "mysqli.php";?><div style='width:800px;float:none'> <h1>商品列表</h1> <!-- 显示当前系统时间--> <h3><p id="demo"></p> <script> var myvar=tinterval(function(){mytimer()},1000); function mytimer() { var d = new date(); document.getelementbyid("demo").innerhtml = d.tolocaletimestring(); } </script> </h3> <form method="get" action=""> <table style="100%" border="1"> <tr> <td> <lect name="gid"> <option value="0">请选择商品</option> <?php /** * @name show * @decs 无限级分类 * 关联model: * @param $fid * @param $i * @author 老猫 <18368091722@163.com> * updated on: 2019/5/30 10:12 */ function show($fid,$i) { global $mysqli;//全球变量关键字,将前面的变量作用域使其在函数内生效 $sql = "lect *from goodstype where fid=$fid"; $result = $mysqli->query($sql); $str=" "; $i++; for($n=1;$n<$i;$n++) { $str .= "---"; } $id=ist($_get["gid"])?$id=$_get["gid"]:""; ?> <?php while ($row = $result->fetch_assoc()) { ?> <option <?php if($id==$row['id']){echo "lected";}?> id="<?php echo $str.$row["classname"] ?>" value="<?php echo $row["id"] ?>"> <?php echo $str.$row["classname"] ?> </option> <?php show($fid=$row["id"],$i); ?> <?php } } show(0,0); ?> <input id="lect" type="submit" value="查询"></lect></td></tr> </table> </form></div><div style="float: none;width: 600px"> <?php $id=ist($_get["gid"])?$id=$_get["gid"]:""; if(!empty($id)){ $sql="lect *from goods where goodstypefid=$id or goodstypefstr like '%$id%' and checkinfo=1 and delstate=0"; }el{ $sql="lect *from goods"; } $result=$mysqli->query($sql); ?> <table border="1" cellpadding="3" cellspacing="0" width="60%"> <tr bgcolor="skyblue"> <?php while($row=$result->fetch_assoc()){ ?> <td > <image width="200px" 社区矫正工作总结height="200px" src="<?php echo $row["picurl"]?>"></image> <a title="查看商品详细信息" href="goodsshow.php?id=<?php echo $row["id"]?>"><?php echo $row["title"]?></a> </td> <?php } ?> </tr&小学庆六一活动总结gt; </table></div>
goodsshow.php //商品详情页展示
<?php/** * @name goodsshow.php * @decs * @author 老猫 <18368091722@163.com> * updated on: 2019/5/30 10:15 */header("content-type:text/html;chart=utf-8");include "mysqli.php";?><?php$id=ist($_get["id"])?$_get["id"]:1;$sql="update goods t hits=hits+1 where id=".$id;$mysqli->query($sql);$sql="lect *from goods where id=".$id;$result=$mysqli->query($sql);while($row=$result->fetch_assoc()){?><div> <span><?php echo $row["title"] ?></span> <hr> <image src="<?php echo $row["picurl"] ?>" width="200"></image> <br> 数量:-<input type="text" id="buynum" value="1">+ 价格: <del>市场价:<?php echo $row["marketprice"] ?></del> 出售价:<i style="color: red"><?php echo $row["salesprice"] ?></i> <input type="hidden" id="id" value="<?php echo $row["id"] ?>"> <hr> 内容:<?php echo $row["content"] ?><br> <a href="javascript:;" onclick="buynow()">立刻购买</a> <a href="javascript:;" onclick="addshoppingcart()">加入购物车</a> <?php } ?></div><script src="jquery.min.js"></script> <script> //立即购买 function buynow(){ //先添加到购物车再进行跳转到购买页面 addshoppingcart("buy"); } //添加到购物车 function addshoppingcart(a){ $.ajax({ url:"shoppingcart.php?a=addshoppingcart", type:"post", data:{'buynum':$("#buynum").val(),'id':$("#id").val()}, datatype:"html", success:function 王熙强(data) { if(a=="buy"){ location.href="shoppingcart.php?a=buynow"; }el{ if(data){ alert("添加购物车成功!"); } } } }) }</script>
shoppingcart.php //购物车功能
<?php/** * @name shoppingcart.php * @decs * @author 老猫 <18368091722@163.com> * updated on: 2019/5/30 10:20 */header("content-type:text/html;chart=utf-8");include "mysqli.php";$a=ist($_get["a"])?$_get["a"]:"";//添加购物车if($a=="addshoppingcart"){ $buynum=$_post["buynum"]; $id=$_post["id"];// echo "<script>alert($buynum+$id)</script>"; if(!empty($_cookie["shoppingcart"])) $shoppingcart=unrialize($_cookie["shoppingcart"]); el $shoppingcart=array(); if(ist($id) && ist($buynum)){ $id=intval($id); $buynum=intval($buynum); $shoppingcart[]=array($id,$buynum); } tcookie('shoppingcart',rialize($shoppingcart));//商品属性进行序列化保存到cookie中 return true;}elif($a=="buynow") { //下面写购物车页面 if (!empty($_cookie["shoppingcart"])) { ?> <table width="36%" border="1" cellspacing="0" cellpadding="0"> <tr bgcolor="#87ceeb"> <td width="20%">商品id</td> <td width="35%" height="30">商品名称</td> <td width="25%">购买数量</td> <td width="15%">价格</td> <td width="5%">操作</td> </tr> <tr> <td height="10" colspan="4"></td> </tr> <?php $totalprice = ""; $shoppingcart = unrialize($_cookie["shoppingcart"]); foreach ($shoppingcart as $key => $value) { $keys = array($key); ?> <tr> <td><?php echo $value[0] ?></td> <td height="30"> <?php $sql = "lect *from goods where id=" . intval($value[0]); $result = $mysqli->query($sql); $row = $result->fetch_assoc(); $totalprice += $row["salesprice"] * $value[1]; 古尔邦节是什么意思 echo '<a href="goodsshow.php?cid=' . $row['goodstypeid'] . '&id=' . $row['id'] . '" class="title" target="_blank">' . $row['title'] . '</a>'; ?> </td> <td><?php echo $value[1] ?></td> <td><?php echo $row["salesprice"] * $value[1] ?></td> <td> <a href="shoppingcart.php?a=delone&key=<?php echo $key ?>" onclick="">取消</a></td> </tr> <?php } ?> </table> <hr> <span style="float: left;width: 250px;height: 150px"> 总价格:<?php echo $totalprice ?><a href="">下一步</a> <a href="shoppingcart.php?a=empty">清空购物车</a> </span> <?php }}elif($a=="delone"){ $key=$_get["key"]; $shoppingcart=unrialize($_cookie["shoppingcart"]); unt($shoppingcart[$key]); if(empty($_cookie)){ tcookie($shoppingcart,"",time()-3600); }elif(empty($shoppingcart)){ tcookie("shoppingcart",rialize($shoppingcart)); echo "<div class='shoppingcartempty'>您的购物车目前没有商品!3秒后跳回首页......</div>"; header("refresh:3;url=goods.php"); }el{ tcookie("shoppingcart",rialize($shoppingcart)); header("location:shoppingcart.php?a=buynow"); } exit();}//清空购物车elif($a=="empty"){ //清除整个cookie保存的商品信息 unt($_cookie["shoppingcart"]); tcookie("shoppingcart","",time()-3600); echo "<div class='shoppingcartempty'>您的购物车目前没有商品!3秒后跳回首页......</div>"; header("refresh:3;url=goods.php");}
以上摘自php中文网
本文发布于:2023-04-06 18:45:57,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/0ddcd506cb9bb64095733882b31ddbc7.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:购物车以php原生cookie实现.doc
本文 PDF 下载地址:购物车以php原生cookie实现.pdf
留言与评论(共有 0 条评论) |