php實(shí)現(xiàn)產(chǎn)品加入購物車功能(1)
今天在練習(xí)購物車以及提交訂單,寫的有點(diǎn)頭暈,順便也整理一下,這個(gè)購物車相對(duì)來說比較簡(jiǎn)單,用于短暫存儲(chǔ),并沒有存儲(chǔ)到數(shù)據(jù)庫,購物車對(duì)于愛網(wǎng)購的人來說簡(jiǎn)直是熟悉的不能再熟悉了,在寫購物車之前,我們首先要構(gòu)思一下,我們需要先從數(shù)據(jù)庫中調(diào)出一張表格,這里我用的是fruit表,其次是登錄表,我用的是login表,用來調(diào)用戶名和密碼的,所有的都準(zhǔn)備好之后就要考慮放入購物車是會(huì)有三種情況的:
第一種情況:購物車?yán)锩媸裁炊紱]有
第二種情況:購物車?yán)锩嬉呀?jīng)有此產(chǎn)品了,再次加入 這種情況下考慮到的是 數(shù)量要+1
第三種情況:購物車?yán)锩嬗挟a(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>
登錄頁面寫好之后,需要進(jìn)入處理頁面,從數(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"登錄失敗";
}
登錄頁面如圖所示:

下面要進(jìn)入主頁面了,從數(shù)據(jù)庫中把所有的水果信息調(diào)出來,然后我們?cè)賮韺?shí)現(xiàn)加入購物車這一項(xiàng)功能。
<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>代號(hào)</td>
<td>水果名稱</td>
<td>水果價(jià)格</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> //這里的購買相當(dāng)于添加購物車的功能
</tr>";
}
?>
<?php
//這里顯示的是 購物車有多少產(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"購物車有<mark>{$zhonglei}</mark>種商品,總價(jià)格為<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.購物車是空的,第一次點(diǎn)擊添加購物車
$arr = array(
array($ids,1)
);
$_SESSION["gwc"]=$arr;
}
else
{
//不是第一次點(diǎn)擊
//判斷購物車中是否存在該商品
$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>商品單價(jià)</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>
這樣進(jìn)入購物車頁面顯示如圖所示:

這只是比較簡(jiǎn)單的加入購物車,但是中間還有很多環(huán)節(jié)沒有完善好,比如說加入購物車后,數(shù)據(jù)庫中的產(chǎn)品數(shù)量減少、購物車中產(chǎn)品的刪除等操作還沒有做,后續(xù)補(bǔ)上。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
php實(shí)現(xiàn)簡(jiǎn)單爬蟲的開發(fā)
本文給大家分享的是如何使用php開發(fā)簡(jiǎn)單的網(wǎng)頁爬蟲的思路以及代碼,非常的簡(jiǎn)單,有需要的小伙伴可以參考下2016-03-03
yii2中添加驗(yàn)證碼的實(shí)現(xiàn)方法
這篇文章主要介紹了yii2中添加驗(yàn)證碼的實(shí)現(xiàn)方法,實(shí)例分析了Yii2中實(shí)現(xiàn)添加驗(yàn)證碼的具體步驟與相關(guān)功能代碼,需要的朋友可以參考下2016-01-01
thinkPHP5.0框架開發(fā)規(guī)范簡(jiǎn)介
這篇文章主要介紹了thinkPHP5.0框架開發(fā)規(guī)范,簡(jiǎn)單分析了thinkPHP5.0各種變量、常量、文件、目錄、類庫等命名規(guī)范與注意事項(xiàng),需要的朋友可以參考下2017-03-03
php項(xiàng)目中百度 UEditor 簡(jiǎn)單安裝調(diào)試和調(diào)用
這篇文章主要介紹了php項(xiàng)目中百度 UEditor 簡(jiǎn)單安裝調(diào)試和調(diào)用的相關(guān)資料,需要的朋友可以參考下2015-07-07
thinkphp實(shí)現(xiàn)面包屑導(dǎo)航(當(dāng)前位置)例子分享
今天把博客一些細(xì)節(jié)完善了一下,其中修改了一下欄目頁和文章頁中的“當(dāng)前位置”。2014-05-05
Yii2使用dropdownlist實(shí)現(xiàn)地區(qū)三級(jí)聯(lián)動(dòng)功能的方法
這篇文章主要介紹了Yii2使用dropdownlist實(shí)現(xiàn)地區(qū)三級(jí)聯(lián)動(dòng)功能的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了dropdownlist下拉列表實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)調(diào)用的具體步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-07-07
dhtmlxTree目錄樹增加右鍵菜單以及拖拽排序的實(shí)現(xiàn)方法
本篇文章介紹了,dhtmlxTree目錄樹增加右鍵菜單以及拖拽排序的實(shí)現(xiàn)方法。需要的朋友參考下2013-04-04
Zend Framework教程之分發(fā)器Zend_Controller_Dispatcher用法詳解
這篇文章主要介紹了Zend Framework教程之分發(fā)器Zend_Controller_Dispatcher用法,結(jié)合實(shí)例形式詳細(xì)分析了分發(fā)器Zend_Controller_Dispatcher的結(jié)構(gòu),功能,使用技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-03-03

