搜尋
首頁後端開發php教程php如何實作多功能購物網站的程式碼案例

下面小編就為大家帶來一篇PHP實現一個多功能購物網站的案例。小編覺得蠻不錯的,現在就分享給大家,也給大家做個參考。一起跟著小編過來看看吧

一、需要實現的頁面:

Index.aspx:瀏覽商品頁面,顯示商品列表,用戶可以點選「加入購物車」。

ViewCart.aspx:查看購物車頁面,顯示已購買的商品信息,可以點擊“刪除“和“提交添加訂單購買”商品

ViewAccount.aspx:查看個人帳戶餘額

Login.aspx:登入頁面

二、實作功能:

##1.顯示商品清單

2.實現購買功能,購買的時候動態顯示購物車中的商品數量和商品總價格

3.點擊查看購物車後,顯示已購買的商品。注意「購買數量」列,如果對一種商品點擊購買多次,其「購買數量」不斷增加。

4.刪除購物車中已購買的商品。

如果某商品的「購買數量」為1時,則點擊「刪除」時,直接從購物車中刪除該商品;

如果商品的「購買數量」大於1時,點選一次「刪除」時,把其購買數量減1。直到商品購買數量為1時,再點選刪除時,刪除該商品

5.在查看完購物車後還可以點選「瀏覽商品」繼續購買。並在上面顯示已購買的商品數量和總價格。

6.在「查看購物車「後,可以提交訂單。

但在提交訂單時,須完成以下功能:

(a)檢查使用者是否已登錄,未登入則轉到Login.aspx頁面

(b)檢查使用者帳戶餘額是否能夠滿足本次夠買

(c)檢查庫存數量是否符合本次夠買

(d)如果以上條件都滿足則

# i.從使用者帳戶中扣除本次購買的總價格

ii.從商品庫存中扣除本次每種商品的購買數量

iii.向訂單表和訂單內容表中加入本次購買的商品資訊

7.點選查看帳戶,可以查看該使用者的帳戶餘額

操作碼如下:

1.先做一個登入頁面:loginpage.php


<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title></title>
  <script src="bootstrap/js/jquery-1.11.2.min.js"></script>
  <script src="bootstrap/js/bootstrap.min.js"></script>
  <link href="bootstrap/css/bootstrap.min.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css"/>
 </head>
 <style>
  .title{
   margin-left: 750px;
   margin-top: 150px;
  }
  .quanju{
   margin-left: 650px;
   margin-top: -460px;
  }
  .name,.pwd{
   max-width: 120px;
  }
  .yangshi1{
   margin-top: 200px;
  }
  .header{
   width: 100%;
   height: 80px;
   background: #e0e0e0;
  }
  .ps{
   margin-left: 100px;
   margin-top: -100px;
  }
 </style>
 <body>
  <form class="form-horizontal" role="form" action="dengluchuli.php" method="post">
 <p class="header">
  <img  src="/static/imghwm/default1.png"  data-src="img/logo.png"  class="lazy"      style="max-width:90%"  style="max-width:90%" / alt="php如何實作多功能購物網站的程式碼案例" >
  <p >果 蔬 网</p>
 </p>
 <h3 id="用户登录">用户登录</h3> 
 <img  class="ps lazy"  src="/static/imghwm/default1.png"  data-src="./img/果蔬专场.jpg"      style="max-width:90%"  style="max-width:90%"  / alt="php如何實作多功能購物網站的程式碼案例" >
 <p class="quanju">
   <p class="form-group yangshi1">
    <label for="firstname" class="col-sm-2 control-label">用户名:</label>
    <p class="col-sm-10">
     <input type="text" class="form-control name" name="uid" placeholder="请输入用户名">
    </p>
   </p>
   <p class="form-group yangshi2">
    <label for="lastname" class="col-sm-2 control-label">密码:</label>
    <p class="col-sm-10">
     <input type="text" class="form-control pwd" name="pwd" placeholder="请输入密码">
    </p>
   </p>
   <p class="form-group">
    <p class="col-sm-offset-2 col-sm-10">
     <p class="checkbox">
      <label>
      <input type="checkbox">
      保存密码 </label>
      <label>
      <input type="checkbox">
      下次自动登录 </label>
     </p>
    </p>
   </p>
   <p class="form-group">
    <p class="col-sm-offset-2 col-sm-10">
     <button type="submit" class="btn btn-warning" value="登录" onclick="return login()" >
     登录
     </button>
     
    </p>
   </p>
  </p> 
 </form>
 </body>
 <script>
  function login(){
   var uid = document.getElementsByTagName("input")[0].value;
   if(uid==""){
    alert("请输入用户名!");
    return false;
   }
   var pwd = document.getElementsByTagName("input")[1].value;
   if(pwd==""){
    alert("请输入密码!");
    return false;
   }
  }  
 </script>
</html>

效果如圖:

##2.在做一個登入的處理頁面:dengluchuli.php

#

<?php
session_start();
$uid = $_POST["uid"];
$pwd = $_POST["pwd"];
require_once "./DBDA.class.php";
$db = new DBDA();
$sql = "select * from login where username=&#39;{$uid}&#39;";
$arr = $db->query($sql,0);
if($arr[0][2]==$pwd && !empty($pwd)){
 $_SESSION["uid"]=$uid;
 header("location:shopping_list.php");
}else{
 echo "登陆失败!";
}

這樣就可以和資料庫聯絡了,這個是資料庫的登入帳號和密碼,驗證帳號,密碼,然後跳到主頁:shopping_list.php

#3.現在做主頁的頁面:shopping_list.php




##

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title></title>
  <script src="bootstrap/js/jquery-1.11.2.min.js"></script>
  <script src="bootstrap/js/bootstrap.min.js"></script>
  <link href="bootstrap/css/bootstrap.min.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css"/>
 </head>
 <body>
  <h2 id="水果列表">水果列表</h2>
 <?php
 session_start();

//1.找出购物车中多少种商品和总价
 $uid = $_SESSION["uid"];
 if(empty($_SESSION["uid"])){
  header("location:loginpage.php");
  exit;
 }
 require_once "./DBDA.class.php";
 $db = new DBDA();
 //如果购物车有商品,取出值
 if(!empty($_SESSION["gwd"])){
  $arr = $_SESSION["gwd"];
  $sum = 0;
  $numbers = count($arr);
  foreach($arr as $k=>$v){
   //$v[0];//水果名称
   //$v[1];//购买数量
   $sql = "select * from fruit where ids=&#39;{$v[0]}&#39;";
   $attr = $db->query($sql,0);
   $dj = $attr[0][2];  //单价
   $sum = $sum+$dj*$v[1];   //总价=单价*数量
  }          
 }  
  echo @"<p style=&#39;margin-left: 250px&#39;>购物车中商品总数为{$numbers}个,商品总价为:{$sum}元</p>";     
 ?> 
   <a href="loginpage.php" rel="external nofollow" >
   登录
   </a>
  <table class="table table-bordered" >
   <thead>
    <tr>
     <th>代号</th>
     <th>名称</th>
     <th>价格</th>
     <th>产地</th>
     <th>库存</th>
     <th>操作</th>
    </tr>
   </thead>
   <tbody>
    <?php
    $sql = "select * from fruit";
    $arr = $db->query($sql,0);
    foreach($arr as $v){
     echo "<tr>
     <td>{$v[0]}</td>
     <td>{$v[1]}</td>
     <td>{$v[2]}</td>
     <td>{$v[3]}</td>
     <td>{$v[4]}</td>
     <td><a href=&#39;shoppingchuli.php?ids={$v[0]}&#39;>加入购物车</a></td>
    </tr>";
    }
    ?>        
   </tbody>
  </table>
 <a href="add_list.php" rel="external nofollow" >查看购物车</a>
 </body>
</html>

4.然後做主頁的處理頁面:shoppingchuli.php


<?php
session_start();
//取到传过来的主键值,并且添加到购物车的SESSION里面
$ids = $_GET["ids"];

//如果是第一次添加购物车,造一个二维数组存到SESSION里面
//如果不是第一次添加,有两种情况
//1.如果该商品购物车里面不存在,造一个一维数组扔到二维里面
//2.如果该商品在购物车存在,让数量加1

if(empty($_SESSION["gwd"])){
 //如果是第一次添加购物车,造一个二维数组存到SESSION里面
 $arr = array( array($ids,1));
 $_SESSION["gwd"]=$arr;
}else{
 
 $arr=$_SESSION["gwd"];
 if(deep_in_array($ids,$arr)){
  //如果该商品在购物车存在,让数量加1
  foreach($arr as $k=>$v){
   if($v[0]==$ids){
    $arr[$k][1]++;    
   }
  } 
  $_SESSION["gwd"]=$arr;  
 }else{
  //如果该商品购物车里面不存在,造一个一维数组扔到二维里面
  $arr=$_SESSION["gwd"]; 
  $attr=array($ids,1);
  $arr[]=$attr;
  $_SESSION["gwd"]=$arr;
 }
}
header("location:shopping_list.php");

function deep_in_array($value, $array) { 
 foreach($array as $item) { 
  if(!is_array($item)) { 
   if ($item == $value) { 
    return true; 
   } else { 
    continue; 
   } 
  } 
   
  if(in_array($value, $item)) { 
   return true;  
  } else if(deep_in_array($value, $item)) { 
   return true;  
  } 
 } 
 return false; 
}
效果如圖:

5.然後再做查看購物車頁面,能看到購物車中的商品和單價和總價:gouwuche.php

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title></title>
  <script src="bootstrap/js/jquery-1.11.2.min.js"></script>
  <script src="bootstrap/js/bootstrap.min.js"></script>
  <link href="bootstrap/css/bootstrap.min.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css"/>
 </head>
 <?php
 session_start();
 $uid = $_SESSION["uid"];
 if(empty($_SESSION["uid"])){
  header("location:loginpage.php");
  exit;
 } 
 ?>
 <body>
  <h2 id="购物车清单">购物车清单</h2>  
  <table class="table table-bordered" >
   <thead>
    <tr>
     <th>代号</th>
     <th>名称</th>
     <th>价格</th>
     <th>产地</th>
     <th>购买数量</th>
     <th>操作</th>
    </tr>
   </thead>
   <tbody>
    <?php   
    require_once "./DBDA.class.php";
    $db = new DBDA();
    
    if(!empty($_SESSION["gwd"])){
     $arr = $_SESSION["gwd"];
     $sum = 0; 
     $numbers = count($arr);     
     foreach($arr as $k=>$v){
     //$v[0];$v[1];
     $sql = "select * from fruit where ids=&#39;{$v[0]}&#39;";
     $a = $db->query($sql,0);
     //var_dump($v[1]);
     echo "<tr>
     <td>{$v[0]}</td>
     <td>{$a[0][1]}</td>
     <td>{$a[0][2]}</td>
     <td>{$a[0][3]}</td>
     <td>{$v[1]}</td>
     <td><a href=&#39;goodsdel.php?zj={$k}&#39;>删除</a></td>       
    </tr>";
     $dj = $a[0][2];
     $sum = $sum+$dj*$v[1]; 
    }
   }
    //echo "<p style=&#39;margin-left: 250px;&#39;>购物车中商品总数为{$numbers}个,商品总价为:{$sum}元</p>";        
    ?>        
   </tbody>            
  </table>
  <a href="submit_order.php?ids={$v[0]}" rel="external nofollow" >提交订单</a>    
 </body>
</html>

效果如圖:


#6.再做刪除的處理頁面goodsdel .php


###
<?php
session_start();
$zj = $_GET["zj"];
//如果该水果数量大于1,减1
//如果该水果数量等于1 移除
$arr = $_SESSION["gwd"];
if($arr[$zj][1]>1){
 $arr[$zj][1]=$arr[$zj][1]-1;
}else{
 unset($arr[$zj]); //清除数组
 $arr=array_values($arr); //重新索引数组
}
$_SESSION["gwd"] = $arr;
header("location:add_list.php");
######7..然後做提交頁面:tijiao.php############## ###
<?php
session_start(); 
$ids = $_GET["ids"];
//查看余额
$uid = $_SESSION["uid"];
require_once "./DBDA.class.php";
$db = new DBDA();
$sql = "select account from login where username=&#39;{$uid}&#39;";
$arr = $db->query($sql,0);
$aye = $arr[0][0];//余额
//var_dump($aye);
if(!empty($_SESSION["gwd"])){
 $arr = $_SESSION["gwd"];
 $sum = 0;
 //$numbers = count($arr);
 foreach($arr as $v){
  $sql = "select * from fruit where ids=&#39;{$v[0]}&#39;";
  $price = $db->query($sql,0);
  $dj = $price[0][2];
  $sum = $sum+$dj*$v[1];
 } 
}else{
 echo "您还未购买商品!";
 //header("shopping_list.php");
 exit;
}
//判断余额是否满足购买
if($aye>=$sum){
 //判断库存
 foreach($arr as $v){
  $skc = "select name,numbers from fruit where ids=&#39;{$v[0]}&#39;";
  $akc = $db->query($sql,0);
  var_dump($akc);
  $kc = $akc[0][4];//库存
  //var_dump($kc);
  
  if($kc<$v[1]){
   echo "库存不足!";
   exit;
  }
 }
 //提交订单
 //账户扣除余额
 $skye = "update login set account=account-{$sum} where username=&#39;{$uid}&#39;";
 $zhye = $db->query($skye);
 
 //扣除库存
 foreach($arr as $v){
 $skckc = "update fruit set numbers=numbers-{$v[1]} where ids=&#39;{$v[0]}&#39;";
 $sykc = $db->query($skckc);
 }
 //添加订单
 $ddh = date("Y-m-d H:i:s");
 $time = time();
 $stjd = "insert into orders values(&#39;{$time}&#39;,&#39;{$uid}&#39;,&#39;{$ddh}&#39;)";
 $wcdh = $db->query($stjd);
 //添加订单详情
 foreach($arr as $v){
  $ddxq = "insert into orderdetails values(&#39;&#39;,&#39;{$ddh}&#39;,&#39;{$v[0]}&#39;,&#39;{$v[1]}&#39;)";
  $axq = $db->query($ddxq);
 } 
}else{
 echo "余额不足,请充值!";
 exit;
}
header("location:shopping_list.php");
###使用者帳號餘額已減少:################

以上是php如何實作多功能購物網站的程式碼案例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
可以在PHP會話中存儲哪些數據?可以在PHP會話中存儲哪些數據?May 02, 2025 am 12:17 AM

phpsessionscanStorestrings,數字,數組和原始物。

您如何開始PHP會話?您如何開始PHP會話?May 02, 2025 am 12:16 AM

tostartaphpsession,usesesses_start()attheScript'Sbeginning.1)placeitbeforeanyOutputtosetThesessionCookie.2)useSessionsforuserDatalikeloginstatusorshoppingcarts.3)regenerateSessiveIdStopreventFentfixationAttacks.s.4)考慮使用AttActAcks.s.s.4)

什麼是會話再生,如何提高安全性?什麼是會話再生,如何提高安全性?May 02, 2025 am 12:15 AM

會話再生是指在用戶進行敏感操作時生成新會話ID並使舊ID失效,以防會話固定攻擊。實現步驟包括:1.檢測敏感操作,2.生成新會話ID,3.銷毀舊會話ID,4.更新用戶端會話信息。

使用PHP會話時有哪些性能考慮?使用PHP會話時有哪些性能考慮?May 02, 2025 am 12:11 AM

PHP会话对应用性能有显著影响。优化方法包括:1.使用数据库存储会话数据,提升响应速度;2.减少会话数据使用,只存储必要信息;3.采用非阻塞会话处理器,提高并发能力;4.调整会话过期时间,平衡用户体验和服务器负担;5.使用持久会话,减少数据读写次数。

PHP會話與Cookie有何不同?PHP會話與Cookie有何不同?May 02, 2025 am 12:03 AM

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

PHP如何識別用戶的會話?PHP如何識別用戶的會話?May 01, 2025 am 12:23 AM

phpIdentifiesauser'ssessionSessionSessionCookiesAndSessionId.1)whiwsession_start()被稱為,phpgeneratesainiquesesesessionIdStoredInacookInAcookInAcienamedInAcienamedphpsessIdontheuser'sbrowser'sbrowser.2)thisIdallowSphptpptpptpptpptpptpptpptoretoreteretrieetrieetrieetrieetrieetrieetreetrieetrieetrieetrieetremthafromtheserver。

確保PHP會議的一些最佳實踐是什麼?確保PHP會議的一些最佳實踐是什麼?May 01, 2025 am 12:22 AM

PHP會話的安全可以通過以下措施實現:1.使用session_regenerate_id()在用戶登錄或重要操作時重新生成會話ID。 2.通過HTTPS協議加密傳輸會話ID。 3.使用session_save_path()指定安全目錄存儲會話數據,並正確設置權限。

PHP會話文件默認存儲在哪裡?PHP會話文件默認存儲在哪裡?May 01, 2025 am 12:15 AM

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!