僅用PHP4 Session實(shí)現(xiàn)的迷你購(gòu)物籃(一)
Jun 21, 2016 am 09:14 AMsession
介紹
如果你還沒(méi)有用PHP 實(shí)現(xiàn)你自已的購(gòu)物籃,在讀完這篇文章之后你應(yīng)該可以創(chuàng)建一個(gè)了。甚至你可能已
經(jīng)有了一個(gè)購(gòu)物籃,我在這里所提供的一些技巧可能會(huì)幫助你改善你的系統(tǒng)。
我將給你一些提示,關(guān)于如何去做也許才能減少對(duì)購(gòu)物籃表的無(wú)限的查詢(xún),或減少無(wú)限的文本文件填充
你的文件系統(tǒng),因?yàn)閼?yīng)用程序只會(huì)做但不會(huì)刪除它們。
如果你對(duì)這類(lèi)的題目是個(gè)新手,不要害怕。它實(shí)際上相當(dāng)簡(jiǎn)單和有效,你只需要一個(gè)支持php4的主機(jī),
象notepad或vi之類(lèi)的html編輯器,你寶貴的幾分種,和這個(gè)小例子。
想法
在1998年,我有一個(gè)在硬件商店工作的朋友要求我為他們編寫(xiě)一個(gè)在線商店。需要快速和簡(jiǎn)單,同時(shí)還
要有在線管理。當(dāng)我發(fā)著39度高燒的時(shí)候我變得想當(dāng)有創(chuàng)建力,所以我用perl寫(xiě)了那個(gè)東西,使用minisql
作為后端。在整個(gè)處理中我插入了我所提到的迷你購(gòu)物籃。
這個(gè)迷你購(gòu)物籃與平常的購(gòu)物車(chē)只有很小的不同,它會(huì)在每一頁(yè)顯示你的購(gòu)物車(chē)的東西,不需要在放入
一件東西后在購(gòu)物車(chē)來(lái)回跳轉(zhuǎn)。這就是我寫(xiě)這篇文章的初衷。
目標(biāo)
人們喜歡控制,那么為什么不給他們些透明度,以便讓他們相信擁有了控制?這個(gè)迷你購(gòu)物籃演示了一
個(gè)完美的方法:它提供給人們所必須的信息,并且節(jié)省了他們一次點(diǎn)擊,接近了在線商店的3擊范例。
所以,目標(biāo)就是在任何時(shí)候?qū)⑦@個(gè)迷你購(gòu)物籃顯示為你的頁(yè)面上的一部分,在結(jié)尾處提供一個(gè)摘要用以
修改購(gòu)物籃中的物品,接著在最后發(fā)送一個(gè)訂單。
要求
你應(yīng)該知道什么是session。如果不知道,這里有一個(gè)快速的綱要。已經(jīng)有很多文章關(guān)于這個(gè)話題,你
可以從中找到所有你需要的代碼例子。在你的開(kāi)始頁(yè)面打開(kāi)一個(gè)session,使用
session_start();
?>
這樣將創(chuàng)建一個(gè)session名字和一個(gè)session id。你現(xiàn)在既可以使用缺省的通過(guò)基于cookie的設(shè)置session
的方式,通過(guò)一個(gè)表單來(lái)傳遞session id,或把它作為一個(gè)變量附在你的鏈接上(get方法)的方法。不要忘
記在最后釋放session。:)
你的商品應(yīng)該有一個(gè)名字,價(jià)格和唯一標(biāo)識(shí),以便能用在這個(gè)例子中,如果不這樣,你可能需要做一點(diǎn)
修改。這里我使用mysql作為后端以便建立頁(yè)面及其它,為了讓購(gòu)物能工作這不是必須的。:)
你應(yīng)該了解數(shù)組。現(xiàn)在如果有人有時(shí)間利用我的代碼并且創(chuàng)建一個(gè)它的類(lèi),我理所當(dāng)然地要感激得到它。
定義
為了簡(jiǎn)化這個(gè)例子,我使用了四個(gè)獨(dú)立的數(shù)組和一些額外的變量。代碼可以被優(yōu)化。這樣可以很容易地
顯示想法和閱讀代碼。可以使用類(lèi)更好的完成,但是我不能十分肯定是否能夠把對(duì)象保存到session 里。有
人想對(duì)此發(fā)表評(píng)論嗎?
我們還需要一個(gè)項(xiàng)目計(jì)數(shù)器。當(dāng)然可以使用count() 命令來(lái)實(shí)現(xiàn),我只不過(guò)認(rèn)為它足夠好,以便總能知
道有多少的項(xiàng)目,并且它也為循環(huán)提供了一個(gè)好的計(jì)數(shù)器。
工作
讓我們假設(shè)在你的HTML頁(yè)中有一個(gè)商品列表
ID Name Price
1 Mouse 25.00 add
2 Key 100.00 add
3 Car 5000.00 add
4 Game 25.00 add
向你的頁(yè)面增加鏈接
上面的'add'字段應(yīng)該是一個(gè)鏈接,用來(lái)將商品放到迷你購(gòu)物籃中。使用$PHP_SELF將這個(gè)鏈接指向它自
已。然后向它增加商品信息。這里是商品1的例子。
add
商品的名稱(chēng)能夠包含空格,所以將它放在鏈接的最后。Get方法好象對(duì)這個(gè)很挑剔。
準(zhǔn)備迷你購(gòu)物籃
為了代碼重用,讓我們創(chuàng)建一個(gè)額外的文件minibasket.inc。我使用.inc擴(kuò)展名來(lái)標(biāo)識(shí)我的包括文件。
使用這個(gè)外部文件的原因是,盡管你將使用session變量來(lái)傳遞給購(gòu)物籃,代碼仍然需要有效。這個(gè)文件將
包含顯示迷你購(gòu)物籃的代碼,也包話增加項(xiàng)目的函數(shù)。實(shí)現(xiàn)它的最好的地方是將 命令放在你需要的地方。
minibasket.inc的邏輯
花些時(shí)間考慮一下這個(gè)。迷你購(gòu)物籃應(yīng)該是個(gè)什么樣子,它需要些什么特性?這里顯示的迷你購(gòu)物籃應(yīng)
該看上去象這樣:
# Name Price
1 Mouse 25.00
3 Game 75.00
Total 100.00
你可以很容易的通過(guò)使用一個(gè)外部樣式表格式化這個(gè)輸出??墒?,它不應(yīng)該太大。這個(gè)迷你購(gòu)物籃是一
個(gè)信息,然而它不應(yīng)該成為你所顯示頁(yè)面的焦點(diǎn)。
這個(gè)文件的邏輯非常簡(jiǎn)單。
檢查是否需要增加一個(gè)新的項(xiàng)目。
如果true,增加項(xiàng)目
在加入中,它要檢查復(fù)重的記錄,通過(guò)更新數(shù)量和價(jià)格來(lái)更新存在的記錄。
片段1。檢查將要增加的新的項(xiàng)目
這是一個(gè)正常的IF語(yǔ)句,用于檢查$basket變量的值。
if ($basket!=""){
//向籃子中加入項(xiàng)目
}
?>
片段2。在瀏覽器中顯示籃子
if ($ses_basket_items>0){
// 如果在籃子中有項(xiàng)目
for ($basket_counter=0;$basket_counter // 遍歷籃子,打印出每一行
// 你當(dāng)然可以用格表來(lái)格式化顯示
// 需要通過(guò)格式化來(lái)顯示價(jià)格的分值。如果沒(méi)有格式化,則.00是不會(huì)顯示出來(lái)的
$price=sprintf("%01.2f",$ses_basket_price[$basket_counter]);
$amount=$ses_basket_amount[$basket_counter];
$name=$ses_basket_name[$basket_counter];
echo "$amount $name $price";
echo "
\n";
}
} else {
// 在籃子中沒(méi)有商品
// 設(shè)置項(xiàng)目計(jì)數(shù)為0,且清除所有的變量
// 這是一個(gè)清除處理。它防止人們得到舊的數(shù)組
$ses_basket_items=0;
unset($ses_basket_name);
unset($ses_basket_amount);
unset($ses_basket_price);
unset($ses_basket_id);
}
?>
這段代碼不會(huì)產(chǎn)生任何結(jié)果。項(xiàng)目還沒(méi)有被填充,籃子總是空的,所以籃子不會(huì)被顯示。那么讓我們向
這個(gè)籃子中加些項(xiàng)目吧。

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Fujifilm fans were recently very excited at the prospect of the X-T50, since it presented a relaunch of the budget-oriented Fujifilm X-T30 II that had become quite popular in the sub-$1,000 APS-C category. Unfortunately, as the Fujifilm X-T50's launc

The problem was found in the springboot project production session-out timeout. The problem is described below: In the test environment, the session-out was configured by changing the application.yaml. After setting different times to verify that the session-out configuration took effect, the expiration time was directly set to 8 hours for release. Arrived in production environment. However, I received feedback from customers at noon that the project expiration time was set to be short. If no operation is performed for half an hour, the session will expire and require repeated logins. Solve the problem of handling the development environment: the springboot project has built-in Tomcat, so the session-out configured in application.yaml in the project is effective. Production environment: Production environment release is

Session failure is usually caused by the session lifetime expiration or server shutdown. The solutions: 1. Extend the lifetime of the session; 2. Use persistent storage; 3. Use cookies; 4. Update the session asynchronously; 5. Use session management middleware.

Problem: Today, we encountered a setting timeout problem in our project, and changes to SpringBoot2’s application.properties never took effect. Solution: The server.* properties are used to control the embedded container used by SpringBoot. SpringBoot will create an instance of the servlet container using one of the ServletWebServerFactory instances. These classes use server.* properties to configure the controlled servlet container (tomcat, jetty, etc.). When the application is deployed as a war file to a Tomcat instance, the server.* properties do not apply. They do not apply,

Solution to the cross-domain problem of PHPSession In the development of front-end and back-end separation, cross-domain requests have become the norm. When dealing with cross-domain issues, we usually involve the use and management of sessions. However, due to browser origin policy restrictions, sessions cannot be shared by default across domains. In order to solve this problem, we need to use some techniques and methods to achieve cross-domain sharing of sessions. 1. The most common use of cookies to share sessions across domains

The Klipsch Flexus Core 300 is the top model in the series and is positioned above the already available Flexus Core 200 in the company's soundbar line-up. According to Klipsch, this is the first soundbar in the world whose sound can be adapted to th

1. Implementing SMS login based on session 1.1 SMS login flow chart 1.2 Implementing sending SMS verification code Front-end request description: Description of request method POST request path /user/code request parameter phone (phone number) return value No back-end interface implementation: @Slf4j@ ServicepublicclassUserServiceImplextendsServiceImplimplementsIUserService{@OverridepublicResultsendCode(Stringphone,HttpSessionsession){//1. Verify mobile phone number if

TheFiiOCP13cassetteplayerwasannouncedinJanuary.Now,FiiOisexpandingitsportfoliowithtwonewmodels-onewitharedfrontandonewithatransparentfront.Thelatternotonlyperfectlymatchestheretrocharmoftheangulardesign,butalso
