溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用PHP怎么開發(fā)一個多功能購物網(wǎng)站

發(fā)布時間:2021-02-04 17:09:23 來源:億速云 閱讀:230 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章給大家介紹使用PHP怎么開發(fā)一個多功能購物網(wǎng)站,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

一、需要實現(xiàn)的頁面:

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

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

ViewAccount.aspx:查看個人賬戶余額

Login.aspx:登錄頁面

二、實現(xiàn)功能:

1.顯示商品列表

2.實現(xiàn)購買功能,購買的時候動態(tài)顯示購物車中的商品數(shù)量和商品總價格

3.點擊查看購物車后,顯示已購買的商品。注意“購買數(shù)量”列,如果對一種商品點擊購買多次,其“購買數(shù)量”不斷增加。

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

如果某商品的“購買數(shù)量”為1時,則點擊“刪除”時,直接從購物車中刪除該商品;

如果商品的“購買數(shù)量”大于1時,點擊一次“刪除”時,把其購買數(shù)量減1。直到該商品購買數(shù)量為1時,再點擊刪除時,刪除該商品

5.在查看完購物車后還可以點擊“瀏覽商品”繼續(xù)購買。并在上面顯示已購買的商品數(shù)量和總價格。

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

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

(a)檢查用戶是否已登錄,未登錄則轉(zhuǎn)到Login.aspx頁面

(b)檢查用戶賬戶余額是否能夠滿足本次夠買

(c)檢查庫存數(shù)量是否滿足本次夠買

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

i.從用戶賬戶中扣除本次購買的總價格

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

iii.向訂單表和訂單內(nèi)容表中加入本次購買的商品信息

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">
 <div class="header">
  <img src="img/logo.png" width="200" height="50" />
  <div >果&nbsp;蔬&nbsp;網(wǎng)</div>
 </div>
 <h4 class="title">用戶登錄</h4> 
 <img src="./img/果蔬專場.jpg" width="500" height="400" class="ps" />
 <div class="quanju">
   <div class="form-group yangshi1">
    <label for="firstname" class="col-sm-2 control-label">用戶名:</label>
    <div class="col-sm-10">
     <input type="text" class="form-control name" name="uid" placeholder="請輸入用戶名">
    </div>
   </div>
   <div class="form-group yangshi2">
    <label for="lastname" class="col-sm-2 control-label">密碼:</label>
    <div class="col-sm-10">
     <input type="text" class="form-control pwd" name="pwd" placeholder="請輸入密碼">
    </div>
   </div>
   <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
     <div class="checkbox">
      <label>
      <input type="checkbox">
      保存密碼 </label>
      <label>
      <input type="checkbox">
      下次自動登錄 </label>
     </div>
    </div>
   </div>
   <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
     <button type="submit" class="btn btn-warning" value="登錄" onclick="return login()" >
     登錄
     </button>
     
    </div>
   </div>
  </div> 
 </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='{$uid}'";
$arr = $db->query($sql,0);
if($arr[0][2]==$pwd && !empty($pwd)){
 $_SESSION["uid"]=$uid;
 header("location:shopping_list.php");
}else{
 echo "登陸失敗!";
}

這樣就可以和數(shù)據(jù)庫聯(lián)系了,這個是數(shù)據(jù)庫的登錄帳號和密碼,驗證帳號,密碼,然后跳到主頁:shopping_list.php

使用PHP怎么開發(fā)一個多功能購物網(wǎng)站

3.現(xiàn)在做主頁的頁面: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>
  <h3 >水果列表</h3>
 <?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];//購買數(shù)量
   $sql = "select * from fruit where ids='{$v[0]}'";
   $attr = $db->query($sql,0);
   $dj = $attr[0][2];  //單價
   $sum = $sum+$dj*$v[1];   //總價=單價*數(shù)量
  }          
 }  
  echo @"<div style='margin-left: 250px'>購物車中商品總數(shù)為{$numbers}個,商品總價為:{$sum}元</div>";     
 ?> 
   <a href="loginpage.php" rel="external nofollow" >
   登錄
   </a>
  <table class="table table-bordered" >
   <thead>
    <tr>
     <th>代號</th>
     <th>名稱</th>
     <th>價格</th>
     <th>產(chǎn)地</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='shoppingchuli.php?ids={$v[0]}'>加入購物車</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"];

//如果是第一次添加購物車,造一個二維數(shù)組存到SESSION里面
//如果不是第一次添加,有兩種情況
//1.如果該商品購物車?yán)锩娌淮嬖冢煲粋€一維數(shù)組扔到二維里面
//2.如果該商品在購物車存在,讓數(shù)量加1

if(empty($_SESSION["gwd"])){
 //如果是第一次添加購物車,造一個二維數(shù)組存到SESSION里面
 $arr = array( array($ids,1));
 $_SESSION["gwd"]=$arr;
}else{
 
 $arr=$_SESSION["gwd"];
 if(deep_in_array($ids,$arr)){
  //如果該商品在購物車存在,讓數(shù)量加1
  foreach($arr as $k=>$v){
   if($v[0]==$ids){
    $arr[$k][1]++;    
   }
  } 
  $_SESSION["gwd"]=$arr;  
 }else{
  //如果該商品購物車?yán)锩娌淮嬖?,造一個一維數(shù)組扔到二維里面
  $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; 
}

效果如圖:

使用PHP怎么開發(fā)一個多功能購物網(wǎng)站

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>
  <h3 >購物車清單</h3>  
  <table class="table table-bordered" >
   <thead>
    <tr>
     <th>代號</th>
     <th>名稱</th>
     <th>價格</th>
     <th>產(chǎn)地</th>
     <th>購買數(shù)量</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='{$v[0]}'";
     $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='goodsdel.php?zj={$k}'>刪除</a></td>       
    </tr>";
     $dj = $a[0][2];
     $sum = $sum+$dj*$v[1]; 
    }
   }
    //echo "<div style='margin-left: 250px;'>購物車中商品總數(shù)為{$numbers}個,商品總價為:{$sum}元</div>";        
    ?>        
   </tbody>            
  </table>
  <a href="submit_order.php?ids={$v[0]}" rel="external nofollow" >提交訂單</a>    
 </body>
</html>

效果如圖:

使用PHP怎么開發(fā)一個多功能購物網(wǎng)站

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

<?php
session_start();
$zj = $_GET["zj"];
//如果該水果數(shù)量大于1,減1
//如果該水果數(shù)量等于1 移除
$arr = $_SESSION["gwd"];
if($arr[$zj][1]>1){
 $arr[$zj][1]=$arr[$zj][1]-1;
}else{
 unset($arr[$zj]); //清除數(shù)組
 $arr=array_values($arr); //重新索引數(shù)組
}
$_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='{$uid}'";
$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='{$v[0]}'";
  $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='{$v[0]}'";
  $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='{$uid}'";
 $zhye = $db->query($skye);
 
 //扣除庫存
 foreach($arr as $v){
 $skckc = "update fruit set numbers=numbers-{$v[1]} where ids='{$v[0]}'";
 $sykc = $db->query($skckc);
 }
 //添加訂單
 $ddh = date("Y-m-d H:i:s");
 $time = time();
 $stjd = "insert into orders values('{$time}','{$uid}','{$ddh}')";
 $wcdh = $db->query($stjd);
 //添加訂單詳情
 foreach($arr as $v){
  $ddxq = "insert into orderdetails values('','{$ddh}','{$v[0]}','{$v[1]}')";
  $axq = $db->query($ddxq);
 } 
}else{
 echo "余額不足,請充值!";
 exit;
}
header("location:shopping_list.php");

用戶賬戶余額已經(jīng)減少:


使用PHP怎么開發(fā)一個多功能購物網(wǎng)站

關(guān)于使用PHP怎么開發(fā)一個多功能購物網(wǎng)站就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI