摘? 要: 開發了基于PHP+MySQL的網上購物系統。該系統采用PHP、DR、MySQL數據庫等技術開發而成。設計開發了包括用戶管理模塊、訂單管理模塊、購物車模塊和商品管理模塊等功能模塊。客戶能在前臺實現商品信息的瀏覽查詢,登錄后能夠將商品加入購物車,生成訂單,管理員能在后臺實現信息的增刪改查。該系統界面清晰美觀,功能比較完善。
關鍵詞: 購物車; 網上購物; 訂單管理; 商品管理
中圖分類號:TP311? ? ? ? ? 文獻標志碼:A? ? ?文章編號:1006-8228(2018)12-22-03
Abstract: A PHP+MySQL based online shopping system is developed in this paper. The system is developed by using PHP technology, DR technology and MySQL database technology. The function modules developed include user management module, order management module, shopping cart module and commodity management module. Users can browse and query the information of commodities in the foreground, add goods to the shopping cart and generate orders after login, and administrators can add, delete, modify and check the information in the background. The system has a clear and beautiful interface, and perfect function.
Key words: shopping cart; online shopping; order management; commodity management
0 引言
近年來,隨著Internet的迅速崛起,互聯網已日益成為收集信息的最佳、最快的渠道,并快速進入傳統的流通領域。互聯網的跨地域性、可交互性、全天候性使其在與傳統媒體行業和傳統貿易行業的競爭中具有不可抗拒的優勢,因而發展十分迅速。在電子商務逐步興起的大環境下,越來越多的人開始選擇網上購物,網上購物可以由商家直接將商品通過物流送達顧客。網上購物具有省時、省事、省心等優點,讓顧客足不出戶就可以購買到自己滿意的商品[1]。
1 系統相關技術基礎
目前開發動態網站的主要技術有ASP.NET、JSP、PHP、ASP等,PHP是一種在服務器端執行的多用途腳本語言。PHP開放源代碼且可嵌入到HTML中,尤其適合動態網站的開發,現在被很多的網站編程人員廣泛應用[3]。
PHP語言流行的主要原因是它的眾多優秀特性:免費開源,自由獲取;跨平臺;速度;功能性。本系統采用Windows+Apache+MySQL+PHP開發而成。
2 系統分析與設計
網上購物系統分為前臺后臺兩個部分,前臺主要包括商品管理模塊、購物車管理模塊和用戶管理模塊,實現商品顯示、商品搜索、商品推薦、購物車管理、訂單管理等功能;后臺主要實現對信息的增刪改查。
在數據庫設計方面,根據該系統的功能,分別設計了用戶表、商品表等數據表,本文中,主要實現的是購物車模塊,涉及到的是商品表,給出商品表的具體信息,如圖1所示。
3 購物車模塊實現
在商品顯示頁面中,單擊購買或者放入購物車按鈕,就可以將商品信息添加到購物車。要實現該功能分兩步[5],分別添加gouwuche1.php和gouwuche2.php頁面[4]。
gouwuche1.php主要實現如下功能:判斷是否已登錄,如果沒有登錄,則提示先登錄后購物;判斷該商品是否已在購物車;判斷該商品是否還有貨;將該商品的id號加入到商品列表中,數量1加入數量列表。具體代碼如下所示[2]:
<?php
session_start();
include("conn.php");
if($_SESSION['uname']=="")
{ echo "<script>alert('請先登錄后購物');history.back();</script>";
exit; } //判斷用戶是否登錄
$id=$_GET['id'];
$sql=mysql_query("select * from tb_shangpin where
id='".$id."'");
$info=mysql_fetch_array($sql);
if($info['shuliang']<=0)
echo "<script>alert('該商品已售完');history.back()
</script>"; //判斷該商品是否有貨
$array=explode("@",$_SESSION['producelist']);
for($i=0;$i<count($array)-1;$i++)
{ if(intval($array[$i])==intval($id))
{ echo '<script>alert("該商品已放入購物");
history.back();</script>';
exit; }} //判斷商品是否已放入購物車
$_SESSION['producelist']=$_SESSION['producelist'].$id."@";
$_SESSION['quatity']=$_SESSION['quatity']."1@";
header("location:gouwuche2.php?qingkong=no");?>
在gouwuche2.php中能夠實現移除商品、修改商品數量和清空購物車功能。代碼如下[2]:
<form name="form1" method="post" >
<table width="557" border="0" align="center" >
<tr> <td height="46" background="images/cart.gif"
colspan="6"></td> </tr>
<?php
$_SESSION['total']="";
if($_GET['qingkong']=="yes") {
$_SESSION['producelist']="";
$_SESSION['quatity']=""; } //實現清空購物車功能
include("conn.php");
$arraygwc=explode("@",$_SESSION['producelist']);
$s=0;
for($i=0;$i<count($arraygwc)-1;$i++)
{ $s+=$arraygwc[$i]; }
if($s==0)
{ echo "<tr>";
echo "<td height='25' colspan='6' bgcolor='#FFFFFF'
align='center'>您的購物車為空!</td>";
echo "</tr>"; }? //判斷購物車是否為空
else { ?>
<tr>
<td height="25" bgcolor="#FFFFFF">商品名稱</td>
<td? bgcolor="#FFFFFF">數量</td>
<td? bgcolor="#FFFFFF">市場價</td>
<td? bgcolor="#FFFFFF">會員價</td>
<td? bgcolor="#FFFFFF">折扣</td>
<td? bgcolor="#FFFFFF">小計</td>
<td? bgcolor="#FFFFFF">操作</td>
</tr>
<?php
$total=0;
$array=explode("@",$_SESSION['producelist']);
$arrayquatity=explode("@",$_SESSION['quatity']);
while(list($name,$value)=each($_POST)) {
for($i=0;$i<count($array)-1;$i++) {
if(($array[$i])==$name) {
$arrayquatity[$i]=$value;
}
}
} //實現更改商品數量功能
$_SESSION['quatity']=implode("@", $arrayquatity);
for($i=0;$i<count($array)-1;$i++) {
$id=$array[$i];
$num=$arrayquatity[$i];
if($id!="") {
$sql=mysql_query("select * from tb_shangpin
where id='".$id."'");
$info=mysql_fetch_array($sql);
$total1=$num*$info['huiyuanjia'];
$total+=$total1;
$_SESSION["total"]=$total;
?>//計算商品總價
<tr>
<td height="25" bgcolor="#FFFFFF"><?php echo $info['mingcheng'];?></td>
<input type="text" name="<?php echo $info['id'];?>" size="2"? value=<?php echo $num;?>>
</td>
<td height="25" bgcolor="#FFFFFF">
<?php echo $info['shichangjia'];?>元</td>
<td height="25" bgcolor="#FFFFFF"><?php echo
$info['huiyuanjia'];?>元</td>
<td height="25" bgcolor="#FFFFFF"><?php echo
@(ceil(($info['huiyuanjia']/$info['shichangjia'])*100))."%";?></td>
<td height="25" bgcolor="#FFFFFF"><?php echo
$info['huiyuanjia']*$num."元";?></td>
<td height="25" bgcolor="#FFFFFF"><a href="#">
刪除</a></td>
</tr> <?php }
} ?>
<tr>
<td height="25" colspan="6" bgcolor="#FFFFFF">
<table width="500" height="25">
<tr> <td width="125">
<input name="submit2" type="submit"? value=
"更改商品數量">
</td>
<td width="125"><a href="gouwusuan1.php?
dingdanhao="">去收銀臺</a>
<a href="#">返回</a></td>
<td width="125"><a href="gouwuche1.php?qingkong=
yes">清空購物車</a></td>
<td width="125">總計:<?php echo $total;?></td>
</tr>? <?php
}? ?>
</table> </form>
運行效果如圖2所示:
4 結束語
隨著越來越多的人選擇網上購物,網上購物系統變得越來越重要[3]。利用PHP+MySQL開發的網上購物系統,具有商品信息瀏覽、商品搜索、購物車等多項功能,較好地滿足了網上購物的需要。在整個開發過程中,采用了PHP技術、DW的框架技術等,界面清晰、美觀,功能完備,具有一定的創新性。系統自運行以來,受到了用戶的普遍歡迎。但由于時間緊迫,系統在頁面布局等方面還有一定的欠缺,接下來要繼續修改和完善該系統。
參考文獻(References):
[1] 楊宇.PHP典型模塊與項目實戰大全[M].清華大學出版社,2012.
[2] 朱珍.PHP網站開發技術[M].電子工業出版社,2014.
[3] 沈蘊梅.電子商務網站建設—PHP+MySQL項目開發教程[M].北京理工大學出版社,2018.
[4] 孫號夕,胡偉,楊中宇.基于PHP和Mysql的銷售管理系統的設計與實現[J].電腦知識與技術,2014.
[5] 徐輝,高榮.“jQuery Web前端開發應用技術”課程教學實踐探索[J].福建電腦,2017.