php實現(xiàn)簡單加入購物車功能
今天在練習購物車以及提交訂單,寫的有點頭暈,順便也整理一下,這個購物車相對來說比較簡單,用于短暫存儲,并沒有存儲到數(shù)據(jù)庫,
購物車對于愛網(wǎng)購的人來說簡直是熟悉的不能再熟悉了,在寫購物車之前,我們首先要構思一下,我們需要先從數(shù)據(jù)庫中調(diào)出一張表格,這里
我用的是fruit表,其次是登錄表,我用的是login表,用來調(diào)用戶名和密碼的,所有的都準備好之后就要考慮放入購物車是會有三種情況的:
第一種情況:購物車里面什么都沒有
第二種情況:購物車里面已經(jīng)有此產(chǎn)品了,再次加入 這種情況下考慮到的是 數(shù)量要+1
第三種情況:購物車里面有產(chǎn)品了,但是沒有此產(chǎn)品
下圖是用到的數(shù)據(jù)庫表格:
下面是登錄頁面的代碼:
<body> <form action="chuli.php" method="post"> <div style="margin-left: 500px; margin-top: 200px; height: 250px; width: 250px; border: 1px dashed black"> <div style="margin-left: 100px; "><h3>登錄</h3></div> <div style="margin-top: 20px">用戶名:<input type="text" name="uid"/></div><br/> <div>密 碼:<input type="password" name="pwd"/></div><br/> <div style="margin-left: 180px"><input type="submit" value="登錄"/></div> </div> </form> </body>
登錄頁面寫好之后,需要進入處理頁面,從數(shù)據(jù)庫中調(diào)出用戶名和密碼:
<?php session_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"登錄失敗"; }
登錄頁面如圖所示:
下面要進入主頁面了,從數(shù)據(jù)庫中把所有的水果信息調(diào)出來,然后我們再來實現(xiàn)加入購物車這一項功能
<h2>大蘋果購物網(wǎng)</h2> <?php session_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>原產(chǎn)地</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> // 從數(shù)據(jù)庫調(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]}'>購買</a></td> //這里的購買相當于添加購物車的功能 </tr>"; } ?> <?php //這里顯示的是 購物車有多少產(chǎn)品,和產(chǎn)品的總價格 $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> <div> <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> </div> </body>
主頁面如圖所示:
最重要的就是添加購物車頁面了
<?php session_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>購買數(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> <div> <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> </div> 14 15 </body>
這樣進入購物車頁面顯示如圖所示:
這只是比較簡單的加入購物車,但是中間還有很多環(huán)節(jié)沒有完善好,比如說加入購物車后,數(shù)據(jù)庫中的產(chǎn)品數(shù)量減少、購物車中產(chǎn)品的刪除等操作還沒有做,后續(xù)再補上,現(xiàn)在腦子有點亂。。。
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關文章
php使用strpos判斷字符串中數(shù)字類型子字符串出錯的解決方法
這篇文章主要介紹了php使用strpos判斷字符串中數(shù)字類型子字符串出錯的解決方法,結合具體問題分析了strpos函數(shù)針對數(shù)字類型子字符串進行判斷時的注意事項及類型轉(zhuǎn)換處理技巧,需要的朋友可以參考下2017-04-04PHP實現(xiàn)動態(tài)添加XML中數(shù)據(jù)的方法
這篇文章主要介紹了PHP實現(xiàn)動態(tài)添加XML中數(shù)據(jù)的方法,結合實例形式分析了php操作xml格式數(shù)據(jù)類的定義及簡單使用技巧,需要的朋友可以參考下2018-03-03PHP面向?qū)ο蟪绦蛟O計(OOP)之方法重寫(override)操作示例
這篇文章主要介紹了PHP面向?qū)ο蟪绦蛟O計(OOP)之方法重寫(override)操作,簡單描述了php面向?qū)ο蟪绦蛟O計中方法重寫的原理,并結合實例形式分析了php方法重寫相關實現(xiàn)技巧與注意事項,需要的朋友可以參考下2018-12-12PHP數(shù)組 為文章加關鍵字連接 文章內(nèi)容自動加鏈接
PHP給文章加關鍵字連接,像163文章內(nèi)容自動加鏈接效果,其實很多php網(wǎng)站管理系統(tǒng)里面都有,可以參考里面的代碼。2011-12-12