PHP實(shí)現(xiàn)一個(gè)多功能購(gòu)物網(wǎng)站的案例
一、需要實(shí)現(xiàn)的頁(yè)面:
Index.aspx:瀏覽商品頁(yè)面,顯示商品列表,用戶可以點(diǎn)擊“加入購(gòu)物車“。
ViewCart.aspx:查看購(gòu)物車頁(yè)面,顯示已購(gòu)買的商品信息,可以點(diǎn)擊“刪除“和“提交添加訂單購(gòu)買”商品
ViewAccount.aspx:查看個(gè)人賬戶余額
Login.aspx:登錄頁(yè)面
二、實(shí)現(xiàn)功能:
1.顯示商品列表
2.實(shí)現(xiàn)購(gòu)買功能,購(gòu)買的時(shí)候動(dòng)態(tài)顯示購(gòu)物車中的商品數(shù)量和商品總價(jià)格
3.點(diǎn)擊查看購(gòu)物車后,顯示已購(gòu)買的商品。注意“購(gòu)買數(shù)量”列,如果對(duì)一種商品點(diǎn)擊購(gòu)買多次,其“購(gòu)買數(shù)量”不斷增加。
4.刪除購(gòu)物車中已購(gòu)買的商品。
如果某商品的“購(gòu)買數(shù)量”為1時(shí),則點(diǎn)擊“刪除”時(shí),直接從購(gòu)物車中刪除該商品;
如果商品的“購(gòu)買數(shù)量”大于1時(shí),點(diǎn)擊一次“刪除”時(shí),把其購(gòu)買數(shù)量減1。直到該商品購(gòu)買數(shù)量為1時(shí),再點(diǎn)擊刪除時(shí),刪除該商品
5.在查看完購(gòu)物車后還可以點(diǎn)擊“瀏覽商品”繼續(xù)購(gòu)買。并在上面顯示已購(gòu)買的商品數(shù)量和總價(jià)格。
6.在“查看購(gòu)物車“后,可以提交訂單。
但在提交訂單時(shí),須完成以下功能:
(a)檢查用戶是否已登錄,未登錄則轉(zhuǎn)到Login.aspx頁(yè)面
(b)檢查用戶賬戶余額是否能夠滿足本次夠買
(c)檢查庫(kù)存數(shù)量是否滿足本次夠買
(d)如果以上條件都滿足則
i.從用戶賬戶中扣除本次購(gòu)買的總價(jià)格
ii.從商品庫(kù)存中扣除本次每種商品的購(gòu)買數(shù)量
iii.向訂單表和訂單內(nèi)容表中加入本次購(gòu)買的商品信息
7.點(diǎn)擊查看賬戶,可以查看該用戶的賬戶余額
操作代碼如下:
1.首先先做一個(gè)登錄頁(yè)面:loginpage.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> <style> .title{ margin-left: 750px; margin-top: 150px; } .quanju{ margin-left: 650px; margin-top: -460px; } .name,.pwd{ max-width: 120px; } .yangshi1{ margin-top: 200px; } .header{ width: 100%; height: 80px; background: #e0e0e0; } .ps{ margin-left: 100px; margin-top: -100px; } </style> <body> <form class="form-horizontal" role="form" action="dengluchuli.php" method="post"> <div class="header"> <img src="img/logo.png" width="200" height="50" /> <div >果 蔬 網(wǎng)</div> </div> <h3 class="title">用戶登錄</h3> <img src="./img/果蔬專場(chǎng).jpg" width="500" height="400" class="ps" /> <div class="quanju"> <div class="form-group yangshi1"> <label for="firstname" class="col-sm-2 control-label">用戶名:</label> <div class="col-sm-10"> <input type="text" class="form-control name" name="uid" placeholder="請(qǐng)輸入用戶名"> </div> </div> <div class="form-group yangshi2"> <label for="lastname" class="col-sm-2 control-label">密碼:</label> <div class="col-sm-10"> <input type="text" class="form-control pwd" name="pwd" placeholder="請(qǐng)輸入密碼"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <div class="checkbox"> <label> <input type="checkbox"> 保存密碼 </label> <label> <input type="checkbox"> 下次自動(dòng)登錄 </label> </div> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-warning" value="登錄" onclick="return login()" > 登錄 </button> </div> </div> </div> </form> </body> <script> function login(){ var uid = document.getElementsByTagName("input")[0].value; if(uid==""){ alert("請(qǐng)輸入用戶名!"); return false; } var pwd = document.getElementsByTagName("input")[1].value; if(pwd==""){ alert("請(qǐng)輸入密碼!"); return false; } } </script> </html>
效果如圖:
2.在做一個(gè)登錄的處理頁(yè)面:dengluchuli.php
<?php session_start(); $uid = $_POST["uid"]; $pwd = $_POST["pwd"]; require_once "./DBDA.class.php"; $db = new DBDA(); $sql = "select * from login where username='{$uid}'"; $arr = $db->query($sql,0); if($arr[0][2]==$pwd && !empty($pwd)){ $_SESSION["uid"]=$uid; header("location:shopping_list.php"); }else{ echo "登陸失敗!"; }
這樣就可以和數(shù)據(jù)庫(kù)聯(lián)系了,這個(gè)是數(shù)據(jù)庫(kù)的登錄帳號(hào)和密碼,驗(yàn)證帳號(hào),密碼,然后跳到主頁(yè):shopping_list.php
3.現(xiàn)在做主頁(yè)的頁(yè)面: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.找出購(gòu)物車中多少種商品和總價(jià) $uid = $_SESSION["uid"]; if(empty($_SESSION["uid"])){ header("location:loginpage.php"); exit; } require_once "./DBDA.class.php"; $db = new DBDA(); //如果購(gòu)物車有商品,取出值 if(!empty($_SESSION["gwd"])){ $arr = $_SESSION["gwd"]; $sum = 0; $numbers = count($arr); foreach($arr as $k=>$v){ //$v[0];//水果名稱 //$v[1];//購(gòu)買數(shù)量 $sql = "select * from fruit where ids='{$v[0]}'"; $attr = $db->query($sql,0); $dj = $attr[0][2]; //單價(jià) $sum = $sum+$dj*$v[1]; //總價(jià)=單價(jià)*數(shù)量 } } echo @"<div style='margin-left: 250px'>購(gòu)物車中商品總數(shù)為{$numbers}個(gè),商品總價(jià)為:{$sum}元</div>"; ?> <a href="loginpage.php" rel="external nofollow" > 登錄 </a> <table class="table table-bordered" > <thead> <tr> <th>代號(hào)</th> <th>名稱</th> <th>價(jià)格</th> <th>產(chǎn)地</th> <th>庫(kù)存</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]}'>加入購(gòu)物車</a></td> </tr>"; } ?> </tbody> </table> <a href="add_list.php" rel="external nofollow" >查看購(gòu)物車</a> </body> </html>
4.然后做主頁(yè)的處理頁(yè)面:shoppingchuli.php
<?php session_start(); //取到傳過(guò)來(lái)的主鍵值,并且添加到購(gòu)物車的SESSION里面 $ids = $_GET["ids"]; //如果是第一次添加購(gòu)物車,造一個(gè)二維數(shù)組存到SESSION里面 //如果不是第一次添加,有兩種情況 //1.如果該商品購(gòu)物車?yán)锩娌淮嬖?,造一個(gè)一維數(shù)組扔到二維里面 //2.如果該商品在購(gòu)物車存在,讓數(shù)量加1 if(empty($_SESSION["gwd"])){ //如果是第一次添加購(gòu)物車,造一個(gè)二維數(shù)組存到SESSION里面 $arr = array( array($ids,1)); $_SESSION["gwd"]=$arr; }else{ $arr=$_SESSION["gwd"]; if(deep_in_array($ids,$arr)){ //如果該商品在購(gòu)物車存在,讓數(shù)量加1 foreach($arr as $k=>$v){ if($v[0]==$ids){ $arr[$k][1]++; } } $_SESSION["gwd"]=$arr; }else{ //如果該商品購(gòu)物車?yán)锩娌淮嬖?,造一個(gè)一維數(shù)組扔到二維里面 $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.然后再做查看購(gòu)物車頁(yè)面,能看到購(gòu)物車中的商品和單價(jià)和總價(jià):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 >購(gòu)物車清單</h2> <table class="table table-bordered" > <thead> <tr> <th>代號(hào)</th> <th>名稱</th> <th>價(jià)格</th> <th>產(chǎn)地</th> <th>購(gòu)買數(shù)量</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;'>購(gòu)物車中商品總數(shù)為{$numbers}個(gè),商品總價(jià)為:{$sum}元</div>"; ?> </tbody> </table> <a href="submit_order.php?ids={$v[0]}" rel="external nofollow" >提交訂單</a> </body> </html>
效果如圖:
6.再做刪除的處理頁(yè)面goodsdel.php
<?php session_start(); $zj = $_GET["zj"]; //如果該水果數(shù)量大于1,減1 //如果該水果數(shù)量等于1 移除 $arr = $_SESSION["gwd"]; if($arr[$zj][1]>1){ $arr[$zj][1]=$arr[$zj][1]-1; }else{ unset($arr[$zj]); //清除數(shù)組 $arr=array_values($arr); //重新索引數(shù)組 } $_SESSION["gwd"] = $arr; header("location:add_list.php");
7..然后做提交頁(yè)面 :tijiao.php
<?php session_start(); $ids = $_GET["ids"]; //查看余額 $uid = $_SESSION["uid"]; require_once "./DBDA.class.php"; $db = new DBDA(); $sql = "select account from login where username='{$uid}'"; $arr = $db->query($sql,0); $aye = $arr[0][0];//余額 //var_dump($aye); if(!empty($_SESSION["gwd"])){ $arr = $_SESSION["gwd"]; $sum = 0; //$numbers = count($arr); foreach($arr as $v){ $sql = "select * from fruit where ids='{$v[0]}'"; $price = $db->query($sql,0); $dj = $price[0][2]; $sum = $sum+$dj*$v[1]; } }else{ echo "您還未購(gòu)買商品!"; //header("shopping_list.php"); exit; } //判斷余額是否滿足購(gòu)買 if($aye>=$sum){ //判斷庫(kù)存 foreach($arr as $v){ $skc = "select name,numbers from fruit where ids='{$v[0]}'"; $akc = $db->query($sql,0); var_dump($akc); $kc = $akc[0][4];//庫(kù)存 //var_dump($kc); if($kc<$v[1]){ echo "庫(kù)存不足!"; exit; } } //提交訂單 //賬戶扣除余額 $skye = "update login set account=account-{$sum} where username='{$uid}'"; $zhye = $db->query($skye); //扣除庫(kù)存 foreach($arr as $v){ $skckc = "update fruit set numbers=numbers-{$v[1]} where ids='{$v[0]}'"; $sykc = $db->query($skckc); } //添加訂單 $ddh = date("Y-m-d H:i:s"); $time = time(); $stjd = "insert into orders values('{$time}','{$uid}','{$ddh}')"; $wcdh = $db->query($stjd); //添加訂單詳情 foreach($arr as $v){ $ddxq = "insert into orderdetails values('','{$ddh}','{$v[0]}','{$v[1]}')"; $axq = $db->query($ddxq); } }else{ echo "余額不足,請(qǐng)充值!"; exit; } header("location:shopping_list.php");
用戶賬戶余額已經(jīng)減少:
以上這篇PHP實(shí)現(xiàn)一個(gè)多功能購(gòu)物網(wǎng)站的案例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
PHP 繪制網(wǎng)站登錄首頁(yè)圖片驗(yàn)證碼
幾乎所有的網(wǎng)站登錄頁(yè)都會(huì)有驗(yàn)證碼,驗(yàn)證碼是一種安全保護(hù)機(jī)制,用于防止垃圾注冊(cè)機(jī)大量注冊(cè)用戶賬號(hào)占用服務(wù)器內(nèi)存從而使服務(wù)器癱瘓。接下來(lái)通過(guò)本文給大家介紹PHP 繪制網(wǎng)站登錄首頁(yè)圖片驗(yàn)證碼,需要的朋友參考下2016-04-04yii實(shí)現(xiàn)使用CUploadedFile上傳文件的方法
這篇文章主要介紹了yii實(shí)現(xiàn)使用CUploadedFile上傳文件的方法,結(jié)合具體的前端與后端處理代碼實(shí)例分析了CUploadedFile類的使用方法,需要的朋友可以參考下2015-12-12php實(shí)現(xiàn)用戶登陸簡(jiǎn)單實(shí)例
這篇文章主要介紹了php實(shí)現(xiàn)用戶登陸簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04laravel 時(shí)間格式轉(zhuǎn)時(shí)間戳的例子
今天小編就為大家分享一篇laravel 時(shí)間格式轉(zhuǎn)時(shí)間戳的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10