本文主要介紹了php實現購物車功能(以大蘋果購物網為例)的實現方法,具有很好的參考價值。下面跟著小編一起來看下吧
首先是幾個簡單的登入頁面
<body><form action="chuli.php" method="post"> <p style="margin-left: 500px; margin-top: 200px; height: 250px; width: 250px; border: 1px dashed black"> <p style="margin-left: 100px; "><h3>登入</h3></p> <p style="margin-top: 20px">使用者名稱:<input type="text" name="uid"/></p><br/> <p>密 碼:<input type="password" name="pwd"/></p><br/> <p style="margin-left: 180px"><input type="submit" value="登入"/></p> </p></form></body>
登入頁面寫好之後,需要進入處理頁面,從資料庫中調出使用者名稱和密碼:
<?phpsession_start(); //開啟session 必須要寫到第一行header("Content-type:text/html;charset=utf-8");$uid=$_POST["uid"]; //從登入頁面擷取到使用者名稱和密碼$pwd=$_POST["pwd"];include("DADB.class.php");$db=new DADB();$sql="select password from login where username='{$uid}'";$arr=$db->Query($sql);if($arr[0][0]==$pwd && !empty($pwd)) //判斷所填寫的密碼和取到的密碼是一樣的,而且密碼不可為空{ $_SESSION["uid"]=$uid; header("location:main.php");}else{ echo"登入失敗";}
這個顯示的是登入頁面
下面要進入首頁面了,從資料庫中把所有的水果資訊調出來,然後我們再來實現加入購物車這一項功能
<h2>大蘋果購物網</h2><?phpsession_start();include("DADB.class.php");$db=new DADB();?><table border="1" width="100%" cellpadding="0" cellspacing="0"> <tr> <td>代號</td> <td>水果名稱</td> <td>水果價格</td> <td>原產地</td> <td>貨架</td> <td>庫存量</td> <td></td> </tr> <?php $uid=$_SESSION["uid"]; $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>{$v[5]}</td> <td><a href='add.php?ids={$v[0]}'>購買</a></td> //這裡的購買相當於添加購物車的功能 </tr>"; } ?> <?php //這裡顯示的是 購物車有多少產品,和產品的總價格 $ann=array(); if(!empty($_SESSION["gwc"])) { $ann=$_SESSION["gwc"]; } $zhonglei = count($ann); $sum=0; foreach($ann as $k) { $sql1="select price from fruit where ids='{$v[0]}'"; $danjia=$db->Query($sql1); foreach($danjia as $n) { $sum=$sum + $n[0]*$k[1]; } } echo"購物車有<mark>{$zhonglei}</mark>種商品,總價格為<mark>{$sum}</mark>元"; ?></table><p><a href="gouwuche.php" rel="external nofollow" rel="external nofollow" >查看購物車</a><a href="main.php" rel="external nofollow" rel="external nofollow" >瀏覽商品</a><a href="zhanghu.php" rel="external nofollow" rel="external nofollow" >查看賬戶</a> </p></body>
首頁面顯示圖
接下來是添加購物車頁面
<?phpsession_start();$ids = $_GET["ids"];if(empty($_SESSION["gwc"])){ //1.購物車是空的,第一次點擊添加購物車 $arr = array( array($ids,1) ); $_SESSION["gwc"]=$arr;}else{ //不是第一次點擊 //判斷購物車中是否存在該商品 $arr = $_SESSION["gwc"]; //先存一下 $chuxian = false; foreach($arr as $v) { if($v[0]==$ids) { $chuxian = true; } } if($chuxian) { //3.如果購物車中有該商品 for($i=0;$i<count($arr);$i++) { if($arr[$i][0]==$ids) { $arr[$i][1]+=1; } } $_SESSION["gwc"] = $arr; } else { //2.如果購物車中沒有該商品 $asg = array($ids,1); $arr[] = $asg; $_SESSION["gwc"] = $arr; }}header("location:gouwuche.php");
然後先是購物車主介面,如下
<h2>購物車中有以下商品:</h2><table cellpadding="0" cellspacing="0" border="1" width="100%"> <tr> <td>商品名稱</td> <td>商品單價</td> <td>購買數量</td> <td></td> </tr> <?php session_start(); //$uid=$_SESSION["uid"]; $arr=array(); if(!empty($_SESSION["gwc"])) { $arr=$_SESSION["gwc"]; } include("DADB.class.php"); $db=new DADB(); foreach($arr as $v) { global $db; $sql="select * from fruit where ids='{$v[0]}'"; $att=$db -> Query($sql,1); foreach($att as $n) { echo"<tr> <td>{$n[1]}</td> <td>{$n[2]}</td> <td>{$v[1]}</td> <td><a href='shanchu.php?sy={$k}'>刪除</a></td></tr>";} } ?> </table> <p> <a href="gouwuche.php" rel="external nofollow" rel="external nofollow" >查看購物車</a> <a href="main.php" rel="external nofollow" rel="external nofollow" >瀏覽商品</a> <a href="zhanghu.php" rel="external nofollow" rel="external nofollow" >查看賬戶</a> </p> 14 15 </body>
緊接著我們就到了刪除頁面,當購物車只有一件商品和大於一件商品時做處理
<?phpsession_start();$sy = $_GET["sy"]; //根據索引找到該資料$arr = $_SESSION["gwc"];$arr[$sy]; //要刪除的資料//如果數量不為1,數量減1if($arr[$sy][1]>1){ $arr[$sy][1] = $arr[$sy][1]-1;}else //如果數量為1,移除{ unset($arr[$sy]);}$_SESSION["gwc"] = $arr; //最後存一下購物車的內容header("location:gouwuche.php");
至於提交頁面,我們要想到餘額,庫存等因素,所以比較繁瑣,
不i怕,上代碼。
<?php session_start();header("Content-type:text/html;charset=utf-8"); //防止出現亂碼$uid=$_SESSION["uid"];//先查一下賬戶餘額include("DADB.class.php");$db=new DADB();$ysql="select account from login where username='{$uid}'";$yarr=$db->Query($ysql);$yarr[0][0];//總額//購物車的總價格,前面有寫過$arr=array();if (!empty($_SESSION["gwc"])){ $arr=$_SESSION["gwc"];}$sum=0;foreach($arr as $v){ $v[1];//購物車中產品的數量 $psql="select price from fruit WHERE ids='{$v[0]}'"; $parr=$db->Query($psql); foreach($parr as $k) { $k[0];//產品的單價 $sum+=$k[0]*$v[1]; }}//判斷餘額是否滿足購買if($yarr[0][0]>=$sum){//餘額滿足,要判斷庫存 foreach($arr as $v) { $ksql="select number from fruit where ids='{$v[0]}'"; $karr=$db->Query($ksql); $karr[0][0];//這是庫存 if($karr[0][0]<$v[1]) //表示庫存不足,這時要給顧客提示庫存不足 { echo"庫存不足"; exit; } } //判斷之後需要提交訂單了 //賬戶扣除餘額 $kcsql="update login set account=account-{$sum} where username='{$uid}'"; $db->Query($kcsql,0);//這裡是修改語句,所以要加上0 //扣除庫存 foreach($arr as $v) { $kcksql="update fruit set number=number-$v[1] where ids='{$v[0]}'"; $db->Query($kcksql,0); }//所有的工作都做完了,這時我們就該提交訂單了// 這裡我在資料庫中做了兩張表,把提交的訂單添加到表中就可以儲存了//添加訂單$ddh = date("YmdHis");$time = date("Y-m-d H:i:s");$sdd = "insert into orders values('{$ddh}','{$uid}','{$time}')";$db->Query($sdd,0);//添加訂單詳情 foreach($arr as $v) { $sddxq = "insert into orderdetails values('','{$ddh}','{$v[0]}','{$v[1]}')"; $db->Query($sddxq,0); }}else{ echo "餘額不足"; exit;}?>
以上就是本文的全部內容,希望對大家的學習有所協助。