3.现在做主页的页面:shopping_list.php
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="bootstrap/js/jquery-1.11.2.min.js"></script> <script src="bootstrap/js/bootstrap.min.js"></script> <link href="bootstrap/css/bootstrap.min.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css"/> </head> <body> <h2 >水果列表</h2> <?php session_start(); //1.找出购物车中多少种商品和总价 $uid = $_SESSION["uid"]; if(empty($_SESSION["uid"])){ header("location:loginpage.php"); exit; } require_once "./DBDA.class.php"; $db = new DBDA(); //如果购物车有商品,取出值 if(!empty($_SESSION["gwd"])){ $arr = $_SESSION["gwd"]; $sum = 0; $numbers = count($arr); foreach($arr as $k=>$v){ //$v[0];//水果名称 //$v[1];//购买数量 $sql = "select * from fruit where ids='{$v[0]}'"; $attr = $db->query($sql,0); $dj = $attr[0][2]; //单价 $sum = $sum+$dj*$v[1]; //总价=单价*数量 } } echo @"<div style='margin-left: 250px'>购物车中商品总数为{$numbers}个,商品总价为:{$sum}元</div>"; ?> <a href="loginpage.php" rel="external nofollow" > 登录 </a> <table class="table table-bordered" > <thead> <tr> <th>代号</th> <th>名称</th> <th>价格</th> <th>产地</th> <th>库存</th> <th>操作</th> </tr> </thead> <tbody> <?php $sql = "select * from fruit"; $arr = $db->query($sql,0); foreach($arr 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='shoppingchuli.php?ids={$v[0]}'>加入购物车</a></td> </tr>"; } ?> </tbody> </table> <a href="add_list.php" rel="external nofollow" >查看购物车</a> </body> </html>
4.然后做主页的处理页面:shoppingchuli.php
<?php session_start(); //取到传过来的主键值,并且添加到购物车的SESSION里面 $ids = $_GET["ids"]; //如果是第一次添加购物车,造一个二维数组存到SESSION里面 //如果不是第一次添加,有两种情况 //1.如果该商品购物车里面不存在,造一个一维数组扔到二维里面 //2.如果该商品在购物车存在,让数量加1 if(empty($_SESSION["gwd"])){ //如果是第一次添加购物车,造一个二维数组存到SESSION里面 $arr = array( array($ids,1)); $_SESSION["gwd"]=$arr; }else{ $arr=$_SESSION["gwd"]; if(deep_in_array($ids,$arr)){ //如果该商品在购物车存在,让数量加1 foreach($arr as $k=>$v){ if($v[0]==$ids){ $arr[$k][1]++; } } $_SESSION["gwd"]=$arr; }else{ //如果该商品购物车里面不存在,造一个一维数组扔到二维里面 $arr=$_SESSION["gwd"]; $attr=array($ids,1); $arr[]=$attr; $_SESSION["gwd"]=$arr; } } header("location:shopping_list.php"); function deep_in_array($value, $array) { foreach($array as $item) { if(!is_array($item)) { if ($item == $value) { return true; } else { continue; } } if(in_array($value, $item)) { return true; } else if(deep_in_array($value, $item)) { return true; } } return false; }
效果如图:
5.然后再做查看购物车页面,能看到购物车中的商品和单价和总价:gouwuche.php
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="bootstrap/js/jquery-1.11.2.min.js"></script> <script src="bootstrap/js/bootstrap.min.js"></script> <link href="bootstrap/css/bootstrap.min.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css"/> </head> <?php session_start(); $uid = $_SESSION["uid"]; if(empty($_SESSION["uid"])){ header("location:loginpage.php"); exit; } ?> <body> <h2 >购物车清单</h2> <table class="table table-bordered" > <thead> <tr> <th>代号</th> <th>名称</th> <th>价格</th> <th>产地</th> <th>购买数量</th> <th>操作</th> </tr> </thead> <tbody> <?php require_once "./DBDA.class.php"; $db = new DBDA(); if(!empty($_SESSION["gwd"])){ $arr = $_SESSION["gwd"]; $sum = 0; $numbers = count($arr); foreach($arr as $k=>$v){ //$v[0];$v[1]; $sql = "select * from fruit where ids='{$v[0]}'"; $a = $db->query($sql,0); //var_dump($v[1]); echo "<tr> <td>{$v[0]}</td> <td>{$a[0][1]}</td> <td>{$a[0][2]}</td> <td>{$a[0][3]}</td> <td>{$v[1]}</td> <td><a href='goodsdel.php?zj={$k}'>删除</a></td> </tr>"; $dj = $a[0][2]; $sum = $sum+$dj*$v[1]; } } //echo "<div style='margin-left: 250px;'>购物车中商品总数为{$numbers}个,商品总价为:{$sum}元</div>"; ?> </tbody> </table> <a href="submit_order.php?ids={$v[0]}" rel="external nofollow" >提交订单</a> </body> </html>
效果如图: