今天在練習(xí)購(gòu)物車(chē)以及提交訂單,寫(xiě)的有點(diǎn)頭暈,順便也整理一下,這個(gè)購(gòu)物車(chē)相對(duì)來(lái)說(shuō)比較簡(jiǎn)單,用于短暫存儲(chǔ),并沒(méi)有存儲(chǔ)到數(shù)據(jù)庫(kù),購(gòu)物車(chē)對(duì)于愛(ài)網(wǎng)購(gòu)的人來(lái)說(shuō)簡(jiǎn)直是熟悉的不能再熟悉了,在寫(xiě)購(gòu)物車(chē)之前,我們首先要構(gòu)思一下,我們需要先從數(shù)據(jù)庫(kù)中調(diào)出一張表格,這里我用的是fruit表,其次是登錄表,我用的是login表,用來(lái)調(diào)用戶名和密碼的,所有的都準(zhǔn)備好之后就要考慮放入購(gòu)物車(chē)是會(huì)有三種情況的:
第一種情況:購(gòu)物車(chē)?yán)锩媸裁炊紱](méi)有
第二種情況:購(gòu)物車(chē)?yán)锩嬉呀?jīng)有此產(chǎn)品了,再次加入 這種情況下考慮到的是 數(shù)量要+1
第三種情況:購(gòu)物車(chē)?yán)锩嬗挟a(chǎn)品了,但是沒(méi)有此產(chǎn)品
相關(guān)學(xué)習(xí)推薦:PHP編程從入門(mén)到精通
下圖是用到的數(shù)據(jù)庫(kù)表格:
下面是登錄頁(yè)面的代碼:
<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>
登錄頁(yè)面寫(xiě)好之后,需要進(jìn)入處理頁(yè)面,從數(shù)據(jù)庫(kù)中調(diào)出用戶名和密碼:
<?php session_start(); //開(kāi)啟session 必須要寫(xiě)到第一行 header("Content-type:text/html;charset=utf-8"); $uid=$_POST["uid"]; //從登錄頁(yè)面獲取到用戶名和密碼 $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)) //判斷所填寫(xiě)的密碼和取到的密碼是一樣的,而且密碼不能為空 { $_SESSION["uid"]=$uid; header("location:main.php"); } else { echo"登錄失敗"; }
登錄頁(yè)面如圖所示:
下面要進(jìn)入主頁(yè)面了,從數(shù)據(jù)庫(kù)中把所有的水果信息調(diào)出來(lái),然后我們?cè)賮?lái)實(shí)現(xiàn)加入購(gòu)物車(chē)這一項(xiàng)功能。
<h2>大蘋(píng)果購(gòu)物網(wǎng)</h2> <?php session_start(); include("DADB.class.php"); $db=new DADB(); ?> <table border="1" width="100%" cellpadding="0" cellspacing="0"> <tr> <td>代號(hào)</td> <td>水果名稱</td> <td>水果價(jià)格</td> <td>原產(chǎn)地</td> <td>貨架</td> <td>庫(kù)存量</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> // 從數(shù)據(jù)庫(kù)調(diào)出我們所需要的內(nèi)容 <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]}'>購(gòu)買(mǎi)</a></td> //這里的購(gòu)買(mǎi)相當(dāng)于添加購(gòu)物車(chē)的功能 </tr>"; } ?> <?php //這里顯示的是 購(gòu)物車(chē)有多少產(chǎn)品,和產(chǎn)品的總價(jià)格 $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"購(gòu)物車(chē)有<mark>{$zhonglei}</mark>種商品,總價(jià)格為<mark>{$sum}</mark>元"; ?> </table> <p> <a href="gouwuche.php" rel="external nofollow" rel="external nofollow" >查看購(gòu)物車(chē)</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>
主頁(yè)面如圖所示:
最重要的就是添加購(gòu)物車(chē)頁(yè)面了
<?php session_start(); $ids = $_GET["ids"]; if(empty($_SESSION["gwc"])) { //1.購(gòu)物車(chē)是空的,第一次點(diǎn)擊添加購(gòu)物車(chē) $arr = array( array($ids,1) ); $_SESSION["gwc"]=$arr; } else { //不是第一次點(diǎn)擊 //判斷購(gòu)物車(chē)中是否存在該商品 $arr = $_SESSION["gwc"]; //先存一下 $chuxian = false; foreach($arr as $v) { if($v[0]==$ids) { $chuxian = true; } } if($chuxian) { //3.如果購(gòu)物車(chē)中有該商品 for($i=0;$i<count($arr);$i++) { if($arr[$i][0]==$ids) { $arr[$i][1]+=1; } } $_SESSION["gwc"] = $arr; } else { //2.如果購(gòu)物車(chē)中沒(méi)有該商品 $asg = array($ids,1); $arr[] = $asg; $_SESSION["gwc"] = $arr; } } header("location:gouwuche.php");
這樣就可以顯示到購(gòu)物車(chē)的頁(yè)面了,購(gòu)物車(chē)的頁(yè)面代碼如下:
<h2>購(gòu)物車(chē)中有以下商品:</h2> <table cellpadding="0" cellspacing="0" border="1" width="100%"> <tr> <td>商品名稱</td> <td>商品單價(jià)</td> <td>購(gòu)買(mǎi)數(shù)量</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?ids={$v[0]}'>刪除</a></td> </tr>";} } ?> </table> <p> <a href="gouwuche.php" rel="external nofollow" rel="external nofollow" >查看購(gòu)物車(chē)</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>
這樣進(jìn)入購(gòu)物車(chē)頁(yè)面顯示如圖所示:
這只是比較簡(jiǎn)單的加入購(gòu)物車(chē),但是中間還有很多環(huán)節(jié)沒(méi)有完善好,比如說(shuō)加入購(gòu)物車(chē)后,數(shù)據(jù)庫(kù)中的產(chǎn)品數(shù)量減少、購(gòu)物車(chē)中產(chǎn)品的刪除等操作還沒(méi)有做,后續(xù)補(bǔ)上。