Affichage global du code
1, répertoire du projet :
2, affichage de tous les codes :
conn.inc.php:
<style> .shoppingcartempty {background: #00FF00} </style> <?php header("Content-type:text/html;charset=utf-8"); include "mysqli.php"; $a=isset($_GET["a"])?$_GET["a"]:""; //添加购物车 if($a=="addshoppingcart"){ $buynum=$_POST["buynum"]; $id=$_POST["id"]; // echo "<script>alert($buynum+$id)</script>"; if(!empty($_COOKIE["shoppingcart"])) $shoppingcart=unserialize($_COOKIE["shoppingcart"]); else $shoppingcart=array(); if(isset($id) && isset($buynum)){ $id=intval($id); $buynum=intval($buynum); $shoppingcart[]=array($id,$buynum); } setcookie('shoppingcart',serialize($shoppingcart)); return "true"; } //取消购物车里的一件商品 elseif($a=="delone"){ $key=$_GET["key"]; $shoppingcart=unserialize($_COOKIE["shoppingcart"]); unset($shoppingcart[$key]); if(empty($_COOKIE)){ setcookie($shoppingcart,"",time()-3600); }else{ setcookie("shoppingcart",serialize($shoppingcart)); } header("location:shoppingcart.php"); exit(); } //清空购物车 elseif($a=="empty"){ unset($_COOKIE["shoppingcart"]); setcookie("shoppingcart","",time()-3600); echo "<div class='shoppingcartempty'>您的购物车目前没有商品!3秒后跳回首页......</div>"; header("Refresh:3;url=goods.php"); } elseif($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 = unserialize($_COOKIE["shoppingcart"]); foreach ($shoppingcart as $key => $value) { $keys = array($key); ?> <tr> <td><?php echo $value[0] ?></td> <td height="30"> <?php $sql = "select *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 } } ?>
goods.php:
<?php header("Content-type:text/html;charset=utf-8"); include "mysqli.php"; ?> <div style='width:800px;float:none'> <h1>商品列表</h1> <!-- 显示当前系统时间--> <h3><p id="demo"></p> <script> var myVar=setInterval(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> <select name="gid"> <option value="0">请选择商品</option> <?php function show($fid,$i) { global $mysqli; $sql = "select *from goodstype where fid=$fid"; $result = $mysqli->query($sql); $str=" "; $i++; for($n=1;$n<$i;$n++) { $str .= "---"; } $id=$_GET["gid"]; ?> <?php while ($row = $result->fetch_assoc()) { ?> <option <?php if($id==$row['id']){echo "selected";}?> 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="select" type="submit" value="查询"></select></td></tr> </table> </form> </div> <div style="float: none;width: 600px"> <?php $id=isset($_GET["gid"])?$id=$_GET["gid"]:""; if(!empty($id)){ $sql="select *from goods where goodstypefid=$id or goodstypefstr like '%$id%' and checkinfo=1 and delstate=0"; }else{ $sql="select *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> </table> </div>
goodsshow .php:
<?php header("Content-type:text/html;charset=utf-8"); include "mysqli.php"; ?> <script src="./jquery-1.11.0.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"; }else{ if(data){ alert("添加购物车成功!"); } } } }) } </script> <?php $id=isset($_GET["id"])?$_GET["id"]:1; $sql="update goods set hits=hits+1 where id=".$id; $mysqli->query($sql); $sql="select *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>
mysqli.php:
<?php include 'conn.inc.php'; $mysqli=new mysqli(HOST,USER,PWD,DBNAME); if($mysqli->connect_errno){ die('数据库链接出错'.$mysqli->connect_error); }
shoppingcart.php:
<style> .shoppingcartempty {background: #00FF00} </style> <?php header("Content-type:text/html;charset=utf-8"); include "mysqli.php"; $a=isset($_GET["a"])?$_GET["a"]:""; //添加购物车 if($a=="addshoppingcart"){ $buynum=$_POST["buynum"]; $id=$_POST["id"]; // echo "<script>alert($buynum+$id)</script>"; if(!empty($_COOKIE["shoppingcart"])) $shoppingcart=unserialize($_COOKIE["shoppingcart"]); else $shoppingcart=array(); if(isset($id) && isset($buynum)){ $id=intval($id); $buynum=intval($buynum); $shoppingcart[]=array($id,$buynum); } setcookie('shoppingcart',serialize($shoppingcart)); return "true"; } //取消购物车里的一件商品 elseif($a=="delone"){ $key=$_GET["key"]; $shoppingcart=unserialize($_COOKIE["shoppingcart"]); unset($shoppingcart[$key]); if(empty($_COOKIE)){ setcookie($shoppingcart,"",time()-3600); }else{ setcookie("shoppingcart",serialize($shoppingcart)); } header("location:shoppingcart.php"); exit(); } //清空购物车 elseif($a=="empty"){ unset($_COOKIE["shoppingcart"]); setcookie("shoppingcart","",time()-3600); echo "<div class='shoppingcartempty'>您的购物车目前没有商品!3秒后跳回首页......</div>"; header("Refresh:3;url=goods.php"); } elseif($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 = unserialize($_COOKIE["shoppingcart"]); foreach ($shoppingcart as $key => $value) { $keys = array($key); ?> <tr> <td><?php echo $value[0] ?></td> <td height="30"> <?php $sql = "select *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 } } ?>