首先写登陆页面,代码如下:
<body> <h1>登入页面</h1> <form action="dengrucl.php" method="post"> <div>帐号:<input type="text" name="zhang"/></div> <div>密码:<input type="text" name="mi"/></div> <input type="submit" value="登入"/> </form> </body>
处理页面代码:
<?php session_start(); include ("../DBDA.class.php"); $db = new DBDA(); $zhang = $_POST["zhang"]; $pwd= $_POST["pwd"]; $sql = "select mi from yonghu WHERE zhang = '{$zhang}'"; $arr = $db->Query($sql); if(!empty($zhang)&&!empty($mi)&&$mi = $arr){ $_SESSION["zhang"] = $zhang; header("location:zym.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="../bootstrap/js/jquery-1.11.2.min.js"></script> <script src="../bootstrap/js/bootstrap.min.js"></script> <link href="../bootstrap/css/bootstrap.min.css" /> </head> <body> <h1>水果列表</h1> <?php session_start(); ?> <table width="50%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>代号</td> <td>名称</td> <td>单价</td> <td>产地</td> <td>库存</td> <td>操作</td> </tr>
</table> </body> </html>
处理代码:
<?php
require "../DBDA.class.php";
$db = new DBDA();
$sql = "select * from fruit";
$arr = $db -> query($sql);
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='gwc_chuli.php?ids={$v[0]}'>加入购物车</a></td>
</tr>";
}
?>
购物车
if(empty($_SESSION["gwc"])) { //如果是第一次添加购物车,造一个二维数组存到SESSION里面 $arr = array( array($ids,1) ); $_SESSION["gwc"] = $arr; } else { $arr = $_SESSION["gwc"]; $bs = false; //是否出现 foreach($arr as $v) { if($v[0]==$ids) { $bs = true; } } if($bs) { //2.如果该商品在购物车存在,让数量加1 foreach($arr as $k=>$v) { if($v[0] == $ids) { $arr[$k][1]++; //这里我们用一个变量K来取值和赋值,因为只是取到的值给了$v来代表数组的值,其实数组的值并不会因为$v改变二改变 } } $_SESSION["gwc"] = $arr; } else { //1.如果该商品购物车里面不存在,造一个一维数组扔到二维里面 $attr = array($ids,1); $arr[] = $attr; $_SESSION["gwc"] = $arr; } } header("location:gwc_list.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="../bootstrap/js/jquery-1.11.2.min.js"></script> <script src="../bootstrap/js/bootstrap.min.js"></script> <link href="../bootstrap/css/bootstrap.min.css" /> </head> <body> <h1>查看购物车</h1> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>代号</td> <td>名称</td> <td>单价</td> <td>数量</td> <td>操作</td> </tr> <?php session_start(); $arr = $_SESSION["gwc"]; require "DataBase.class.php"; $db = new DateBase(); $sum = 0; foreach ($arr as $k => $v) { $v[0]; $v[1]; $sql = "select * from fruit where ids='{$v[0]}'"; $af = $db -> query($sql); echo "<tr> <td>{$v[0]}</td> <td>{$af[0][1]}</td> <td>{$af[0][2]}</td> <td>{$v[1]}</td> <td><a href='gwc_del.php?sy={$k}'>删除</a></td> </tr>"; $sum = $sum + $v[1] * $af[0][2]; } ?> </table> <div> <?php $gs = count($arr); //购物车里面有几个商品 echo "该购物车里面总共有{$gs}种商品,商品的总价为:{$sum}元"; ?> </div> </body> </html>
删除物品
<?php session_start(); $sy = $_GET["sy"]; //如果该水果数量大于1,减1 //如果该水果数量等于1 移除 $arr = $_SESSION["gwc"]; if ($arr[$sy][1] > 1) { //如果该水果数量大于1,减1 $arr[$sy][1]--; } else { unset($arr[$sy]); $arr = array_values($arr); } $_SESSION["gwc"] = $arr; header("location:gwc_chakan.php");