如果客戶從商品目錄中選擇了一件商品,這時(shí)我們傳遞客戶的請(qǐng)求到proxy.asp,同時(shí)傳遞的是動(dòng)作變量"增加商品"。proxy.asp讀取這個(gè)變量,并決定執(zhí)行購(gòu)物推車的哪個(gè)動(dòng)作。這些動(dòng)作包括:增加商品、更新商品數(shù)量、移除商品或者察看購(gòu)物推車。
有些動(dòng)作被作為內(nèi)部調(diào)用。如創(chuàng)建推車之前,我們需要檢查推車是否已經(jīng)存在(checkcart)。在增加、刪除或者更新商品及其數(shù)量時(shí),我們要確認(rèn)此商品是否已經(jīng)存在于我們的購(gòu)物推車之中(checkitem)。下面我們將為購(gòu)物推車設(shè)計(jì)8個(gè)方法,分別是:
viewitem 察看推車
checkcart 檢查推車
createcart 創(chuàng)建推車
additem(id, qty), 增加商品
removeitem(id) 刪除商品
updateitem(id, qty), 更新數(shù)量
removeall 全部清空
checkitem(id) 商品檢查
購(gòu)物推車的設(shè)計(jì)
購(gòu)物推車需要三個(gè)要素:一個(gè)vbscript 5的類class cartkit,一個(gè)多維數(shù)組mudcart,一個(gè)session變量session("cart")。
這個(gè)vbscript 類,我們稱為cartkit,包含了8個(gè)方法,如上表所示。在本文我們只用了id和qty,分別表示商品編號(hào)和數(shù)量。
這樣我們就可以用一個(gè)二維數(shù)組來(lái)表達(dá)這個(gè)推車,象下面這樣:
商品編號(hào) 商品數(shù)量
id-1 23
id-3 10
id-23 6
id-2 1
然后我們把這個(gè)二維數(shù)組保存到session變量中就可以了。
購(gòu)物推車類cartkit的設(shè)計(jì)
請(qǐng)下載:cartkit.asp
createcart設(shè)計(jì):
請(qǐng)看程序代碼:
class cartkit rem 開始類cartkit的定義
sub createcart()
if isarray(session("cart")) = false then
dim mudcart(19,1)
session("cart") = mudcart
end if
end sub
其中:session("cart")保存的就是推車的內(nèi)容。如果推車不存在,我們就定義一個(gè)二維數(shù)組mudcart來(lái)表達(dá)推車,并將它保存到session("cart")中。
checkcart設(shè)計(jì):
本函數(shù)判斷cart是否已經(jīng)創(chuàng)建。比較簡(jiǎn)單。
function checkcart()
if isarray(session("cart")) then
checkcart=true
else
checkcart=false
end if
end function
checkitem設(shè)計(jì):
請(qǐng)看代碼:
function checkitem(id)
if checkcart=true then
varmudcart = session("cart")
for i = lbound(varmudcart)to ubound(varmudcart)
if varmudcart(i,0) = id then
checkitem=true
exit function
elseif varmudcart(i,0) lt;> id then
checkitem=false
end if
next
end if
end function
首先判斷推車是否存在。然后將商品的編號(hào)id和購(gòu)物推車中的商品編號(hào)逐一比較。如果有相等的,返回真。否則為假。
additem(id,qty)設(shè)計(jì):
請(qǐng)參考本文的附件cartkit.asp,這里介紹的函數(shù)我們把它們封裝到一個(gè)名為cartkit的class中。下面的代碼段首先創(chuàng)建了一個(gè)cartkit對(duì)象,然后檢查推車是否已經(jīng)存在。如果不存在,就創(chuàng)建推車并新增商品;否則,檢查商品編號(hào)是否已經(jīng)存在于cart中。如果是,就更新數(shù)量;否則,新增商品。
function additem(id, qty)
set cartobj = new cartkit
varcartstatus = cartobj.checkcart
if varcartstatus=false then
cartobj.createcart
mudcart=session("cart")
mudcart(0,0)=id
mudcart(0,1)=qty
session("cart")=mudcart
exit function
elseif varcartstatus=true then
if cartobj.checkitem(id) = true then
cartobj.updateitem id,qty
elseif cartobj.checkitem(id) = false then
mudcart = session("cart")
for i = lbound(mudcart) to ubound(mudcart)
if mudcart(i,0) = "" then
mudcart(i,0) = id
mudcart(i,1) = qty
session("cart") = mudcart
exit function
end if
next
end if
end if
end function
updateitem設(shè)計(jì):
function updateitem(id, qty)
mudcart = session("cart")
for i = lbound(mudcart) to ubound(mudcart)
if mudcart(i,0) = id then
mudcart(i,1) = qty
session("cart")=mudcart
exit function
end if
next
end function
viewitem設(shè)計(jì):
function viewitem()
mudcart=session("cart")
if isarray(mudcart) then
%>
標(biāo)簽:內(nèi)江 成都 懷化 天津 臺(tái)灣 公主嶺 重慶 麗江
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《用ASP設(shè)計(jì)購(gòu)物推車》,本文關(guān)鍵詞 用,ASP,設(shè)計(jì),購(gòu)物,推車,用,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。