欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP實(shí)現(xiàn)一個多功能購物網(wǎng)站的案例

 更新時間:2017年09月13日 08:26:49   作者:WORSHIP亞薩  
下面小編就為大家?guī)硪黄狿HP實(shí)現(xiàn)一個多功能購物網(wǎng)站的案例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

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

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

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

Login.aspx:登錄頁面

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

1.顯示商品列表

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

7.點(diǎn)擊查看賬戶,可以查看該用戶的賬戶余額

操作代碼如下:

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>
 <h3 class="title">用戶登錄</h3> 
 <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ù)庫的登錄帳號和密碼,驗(yàn)證帳號,密碼,然后跳到主頁:shopping_list.php

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>
  <h2 >水果列表</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];//購買數(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; 
}

效果如圖:

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 >購物車清單</h2>  
  <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>

效果如圖:

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實(shí)現(xiàn)一個多功能購物網(wǎng)站的案例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • PHP 修改SESSION的生存時間案例詳解

    PHP 修改SESSION的生存時間案例詳解

    這篇文章主要介紹了PHP 修改SESSION的生存時間案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • PHP 繪制網(wǎng)站登錄首頁圖片驗(yàn)證碼

    PHP 繪制網(wǎng)站登錄首頁圖片驗(yàn)證碼

    幾乎所有的網(wǎng)站登錄頁都會有驗(yàn)證碼,驗(yàn)證碼是一種安全保護(hù)機(jī)制,用于防止垃圾注冊機(jī)大量注冊用戶賬號占用服務(wù)器內(nèi)存從而使服務(wù)器癱瘓。接下來通過本文給大家介紹PHP 繪制網(wǎng)站登錄首頁圖片驗(yàn)證碼,需要的朋友參考下
    2016-04-04
  • 使用ThinkPHP生成縮略圖及顯示

    使用ThinkPHP生成縮略圖及顯示

    這篇文章主要為大家詳細(xì)介紹了使用ThinkPHP生成縮略圖及顯示的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • 詳解HTTP Cookie狀態(tài)管理機(jī)制

    詳解HTTP Cookie狀態(tài)管理機(jī)制

    cookie 最早是網(wǎng)景公司的雇員 Lou Montulli 在1993年3月發(fā)明,后被 W3C 采納,目前 cookie 已經(jīng)成為標(biāo)準(zhǔn),所有的主流瀏覽器如 IE、Chrome、Firefox、Opera 等都支持
    2016-01-01
  • yii實(shí)現(xiàn)使用CUploadedFile上傳文件的方法

    yii實(shí)現(xiàn)使用CUploadedFile上傳文件的方法

    這篇文章主要介紹了yii實(shí)現(xiàn)使用CUploadedFile上傳文件的方法,結(jié)合具體的前端與后端處理代碼實(shí)例分析了CUploadedFile類的使用方法,需要的朋友可以參考下
    2015-12-12
  • YII2框架中excel表格導(dǎo)出的方法詳解

    YII2框架中excel表格導(dǎo)出的方法詳解

    最近在研究PHP的Yii框架,很喜歡,碰到導(dǎo)出Excel的問題,研究了一下,就有了下面這篇文章,這篇文章主要給大家介紹了關(guān)于YII2框架中excel表格導(dǎo)出的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-07-07
  • php在線代理轉(zhuǎn)向代碼

    php在線代理轉(zhuǎn)向代碼

    這個代碼不是自己實(shí)現(xiàn)代理的功能 只是將輸入的Url進(jìn)行簡單的處理之后轉(zhuǎn)向一個代理網(wǎng)站,速度不錯
    2012-05-05
  • 數(shù)字轉(zhuǎn)英文

    數(shù)字轉(zhuǎn)英文

    數(shù)字轉(zhuǎn)英文...
    2006-12-12
  • php實(shí)現(xiàn)用戶登陸簡單實(shí)例

    php實(shí)現(xiàn)用戶登陸簡單實(shí)例

    這篇文章主要介紹了php實(shí)現(xiàn)用戶登陸簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • laravel 時間格式轉(zhuǎn)時間戳的例子

    laravel 時間格式轉(zhuǎn)時間戳的例子

    今天小編就為大家分享一篇laravel 時間格式轉(zhuǎn)時間戳的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10

最新評論