接着以前那篇 「電子商城項目開發(後臺功能模塊開發)」javascript
登錄頁面login.phpphp
<?php require '../tools.func.php'; require '../db.func.php'; // 判斷當前是否爲post提交 if (!empty($_POST['username'])) { $action = htmlentities($_GET['action']); $prefix = getDBPrefix(); if ($action == 'reg') { $username = htmlentities($_POST['username']); $password = md5(htmlentities($_POST['password'])); $email = htmlentities($_POST['email']); $created_at = date('Y-m-d H:i:s'); $sql = "INSERT INTO {$prefix}user(username, password, email, created_at) VALUES('$username', '$password', '$email', '$created_at')"; if (execute($sql)) { setInfo('註冊成功'); } else { setInfo('註冊失敗'); } } elseif ($action == 'login') { $username = htmlentities($_POST['username']); $password = md5(htmlentities($_POST['password'])); $sql = "SELECT id, username FROM {$prefix}user WHERE username = '$username' AND password = '$password'"; $res = queryOne($sql); if ($res) { setSession('shop', ['username' => $username, 'id' => $res['id']]); header('location: index.php'); } else { setInfo('用戶名或者密碼錯誤'); } } } // 判斷操做爲login 仍是 reg // 若是是reg,要接收post數據,插入新數據 // 若是是login,要查詢數據庫,判斷用戶名或者密碼是否正確,正確的話寫入session // 顯示結果 require 'header.php'; ?> <!-- Start Login Register Area --> <div class="htc__login__register bg__white ptb--130"> <div class="container"> <div class="row"> <div class="col-md-6 col-md-offset-3"> <ul class="login__register__menu" role="tablist"> <li role="presentation" class="login active"><a href="#login" role="tab" data-toggle="tab">登陸</a></li> <li role="presentation" class="register"><a href="#register" role="tab" data-toggle="tab">註冊</a></li> </ul> </div> </div> <!-- Start Login Register Content --> <div class="row"> <div class="col-md-6 col-md-offset-3"> <div class="htc__login__register__wrap"> <!-- Start Single Content --> <div id="login" role="tabpanel" class="single__tabs__panel tab-pane fade in active"> <p><?php if (hasInfo()) echo getInfo(); ?></p> <form id="loginform" class="login" method="post" action="login.php?action=login"> <input type="text" name="username" placeholder="User Name*"> <input type="password" name="password" placeholder="Password*"> </form> <div class="htc__login__btn mt--30"> <a href="javascript:document.getElementById('loginform').submit();">登陸</a> </div> </div> <!-- End Single Content --> <!-- Start Single Content --> <div id="register" role="tabpanel" class="single__tabs__panel tab-pane fade"> <p><?php if (hasInfo()) echo getInfo(); ?></p> <form id="regform" class="login" action="login.php?action=reg" method="post"> <input type="text" name="username" placeholder="Name*"> <input type="email" name="email" placeholder="Email*"> <input type="password" name="password" placeholder="Password*"> </form> <div class="htc__login__btn"> <a href="javascript:document.getElementById('regform').submit();">註冊</a> </div> </div> <!-- End Single Content --> </div> </div> </div> <!-- End Login Register Content --> </div> </div> <!-- End Login Register Area --> <div class="only-banner ptb--10 bg__white"> </div> <?php require 'footer.php'; ?>
header.phpcss
<?php $username = getSession('username', 'shop'); $cart_header = []; if (!empty($username)) { $uid = getSession('id', 'shop'); // 從數據表查詢購物車數據 $prefix = getDBPrefix(); $sql = "SELECT id, price, quantity, products FROM {$prefix}cart WHERE uid = '$uid'"; $cart_header = queryOne($sql); $cart_header['products'] = json_decode($cart_header['products'], true); } else { // 從session查詢購物車數據 $cart_header = getSession('cart', 'shop'); } ?> <!doctype html> <html class="no-js" lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <title>慕課商城</title> <meta name="description" content=""> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Place favicon.ico in the root directory --> <link rel="apple-touch-icon" href="apple-touch-icon.png"> <!-- All css files are included here. --> <!-- Bootstrap fremwork main css --> <link rel="stylesheet" href="assets/css/bootstrap.min.css"> <!-- Owl Carousel main css --> <link rel="stylesheet" href="assets/css/owl.carousel.min.css"> <link rel="stylesheet" href="assets/css/owl.theme.default.min.css"> <!-- This core.css file contents all plugings css file. --> <link rel="stylesheet" href="assets/css/core.css"> <!-- Theme shortcodes/elements style --> <link rel="stylesheet" href="assets/css/shortcode/shortcodes.css"> <!-- Theme main style --> <link rel="stylesheet" href="assets/css/style.css"> <!-- Responsive css --> <link rel="stylesheet" href="assets/css/responsive.css"> <!-- User style --> <link rel="stylesheet" href="assets/css/custom.css"> <!-- Modernizr JS --> <script src="assets/js/vendor/modernizr-2.8.3.min.js"></script> </head> <body> <!-- Body main wrapper start --> <div class="wrapper fixed__footer"> <!-- Start Header Style --> <header id="header" class="htc-header header--3 bg__white"> <!-- Start Mainmenu Area --> <div id="sticky-header-with-topbar" class="mainmenu__area sticky__header"> <div class="container"> <div class="row"> <div class="col-md-2 col-lg-2 col-sm-3 col-xs-3"> <div class="logo"> <a href="index.php"> <img src="assets/images/logo/logo.png" alt="logo"> </a> </div> </div> <!-- Start MAinmenu Ares --> <div class="col-md-8 col-lg-8 col-sm-6 col-xs-6"> <nav class="mainmenu__nav hidden-xs hidden-sm"> <ul class="main__menu"> <li><a href="index.php">首頁</a></li> </ul> </nav> </div> <!-- End MAinmenu Ares --> <div class="col-md-4 col-sm-4 col-xs-3"> <ul class="menu-extra"> <li><a href="login.php"><span class="ti-user"></span> </a> <?php if (empty($username)): ?> <a href="login.php">註冊</a> <?php else: ?> 歡迎回來, <?php echo $username; ?> <a href="logout.php">退出</a> <?php endif; ?> </li> <li class="cart__menu"><span class="ti-shopping-cart"></span></li> </ul> </div> </div> </div> </div> <!-- End Mainmenu Area --> </header> <!-- End Header Style --> <div class="body__overlay"></div> <!-- Start Offset Wrapper --> <div class="offset__wrapper"> <!-- Start Offset MEnu --> <div class="offsetmenu"> <div class="offsetmenu__inner"> <div class="offsetmenu__close__btn"> <a href="#"><i class="zmdi zmdi-close"></i></a> </div> <div class="off__contact"> <div class="logo"> <a href="index.php"> <img src="assets/images/logo/logo.png" alt="logo"> </a> </div> </div> </div> </div> <!-- End Offset MEnu --> <!-- Start Cart Panel --> <div class="shopping__cart"> <div class="shopping__cart__inner"> <div class="offsetmenu__close__btn"> <a href="#"><i class="zmdi zmdi-close"></i></a> </div> <div class="shp__cart__wrap"> <?php if (! empty($cart_header)): ?> <?php foreach ($cart_header['products'] as $pid => $header_pro): ?> <div class="shp__single__product"> <div class="shp__pro__thumb"> <a href="#"> <img src="assets/uploads/default.jpeg" alt="product images"> </a> </div> <div class="shp__pro__details"> <h2><a href="product_details.php?id=<?php echo $pid; ?>"></a><?php echo $header_pro['product']['name']; ?></h2> <span class="quantity">數量: <?php echo $header_pro['quantity']; ?></span> <span class="shp__price">¥<?php echo $header_pro['product']['price']; ?></span> </div> <div class="remove__btn"> <a href="#" title="Remove this item"><i class="zmdi zmdi-close"></i></a> </div> </div> <?php endforeach; ?> </div> <ul class="shoping__total"> <li class="subtotal">總計:</li> <li class="total__price">¥<?php echo $cart_header['price']; ?></li> </ul> <?php endif; ?> <ul class="shopping__btn"> <li><a href="cart.php">查看購物車</a></li> </ul> </div> </div> <!-- End Cart Panel --> </div> <!-- End Offset Wrapper -->
footer.phphtml
<!-- Start Footer Area --> <footer class="htc__foooter__area gray-bg"> <div class="container"> <!-- Start Copyright Area --> <div class="htc__copyright__area"> <div class="row"> <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12"> <div class="copyright__inner"> <ul class="footer__menu"> </ul> </div> </div> </div> </div> <!-- End Copyright Area --> </div> </footer> <!-- End Footer Area --> </div> <!-- Body main wrapper end --> <!-- Placed js at the end of the document so the pages load faster --> <!-- jquery latest version --> <script src="assets/js/vendor/jquery-1.12.0.min.js"></script> <!-- Bootstrap framework js --> <script src="assets/js/bootstrap.min.js"></script> <!-- All js plugins included in this file. --> <script src="assets/js/plugins.js"></script> <script src="assets/js/slick.min.js"></script> <script src="assets/js/owl.carousel.min.js"></script> <!-- Waypoints.min.js. --> <script src="assets/js/waypoints.min.js"></script> <!-- Main js file that contents all jQuery plugins activation. --> <script src="assets/js/main.js"></script> </body> </html>
登錄後進入首頁index.phpjava
<?php require '../tools.func.php'; require '../db.func.php'; // 1. 查詢全部商品 imooc_product // 2. 寫sql語句 $prefix = getDBPrefix(); $sql = "SELECT id,name,price FROM {$prefix}product ORDER BY created_at DESC"; $data = query($sql); // 3. 遍歷結果 require 'header.php'; ?> <!-- Start Our Product Area --> <section class="htc__product__area bg__white"> <div class="container"> <div class="row"> <div class="col-md-12"> <div class="product-categories-all"> <div class="product-categories-title" style="border-bottom: 1px solid rgba(129, 129, 129, 0.2)"> <h3>全部商品</h3> </div> </div> </div> </div> <div class="row"> <?php foreach ($data as $product): ?> <div class="col-md-3 single__pro col-lg-3 cat--1 col-sm-4 col-xs-12"> <div style="margin-top: 20px" class="product foo"> <div class="product__inner"> <div class="pro__thumb"> <a href="product_details.php?id=<?php echo $product['id']; ?>"> <img src="assets/uploads/default.jpeg" alt="product images"> </a> </div> <div class="product__hover__info"> <ul class="product__action"> <li><a title="加入購物車" href="cart_add.php?product_id=<?php echo $product['id']; ?>&quantity=1"><span class="ti-shopping-cart"></span></a></li> <li><a title="查看詳情" href="product_details.php?id=<?php echo $product['id']; ?>">查看詳情</a></li> </ul> </div> </div> <div class="product__details"> <h2 style="margin-left: 20px;text-align: center;"><a href="product_details.php?id=<?php echo $product['id']; ?>"><?php echo $product['name']; ?></a></h2> <ul class="product__price"> <li class="new__price" style="margin: 0 auto;">¥<?php echo $product['price']; ?></li> </ul> </div> </div> </div> <?php endforeach; ?> </div> </div> </section> <!-- End Our Product Area --> <div class="only-banner ptb--10 bg__white"> </div> <?php require 'footer.php'; ?>
登出頁面logout.phpjquery
<?php /** * Created by PhpStorm. * Date: 2019/1/24 * Time: 12:32 */ require '../tools.func.php'; deleteSession('shop'); header('location: index.php');
商品詳情頁product_details.phpsql
<?php require '../tools.func.php'; require '../db.func.php'; // 1. 接收get id $id = intval($_GET['id']); // 2. 判斷 get id,若是id不存,跳回到首頁 if (empty($id)) { header('location: index.php'); } // 3. 根據ID 查詢對應的商品 $prefix = getDBPrefix(); $sql = "SELECT id, name, price, description FROM {$prefix}product WHERE id = '$id'"; $current_product = queryOne($sql); // 4. 將數據寫入到頁面當中 require 'header.php'; ?> <!-- Start Product Details --> <section class="htc__product__details pt--120 pb--100 bg__white"> <div class="container"> <div class="row"> <div class="col-md-4 col-lg-4 col-sm-12 col-xs-12"> <div class="product__details__container"> <div class="product__big__images" style="width: 100%;max-width: 100%"> <div class="portfolio-full-image tab-content"> <div role="tabpanel" class="tab-pane fade in active"> <img width="100%" src="assets/uploads/default.jpeg" alt="full-image"> </div> </div> </div> </div> </div> <div class="col-md-8 col-lg-8 col-sm-12 col-xs-12 smt-30 xmt-30"> <div class="htc__product__details__inner"> <div class="pro__detl__title"> <h2><?php echo $current_product['name']; ?></h2> </div> <div class="pro__details"> <p><?php echo mb_substr($current_product['description'], 0, 50, 'utf-8') . '...'; ?></p> </div> <ul class="pro__dtl__prize"> <li>¥<?php echo $current_product['price']; ?></li> </ul> <div class="product-action-wrap"> <div class="prodict-statas"><span>數量 :</span></div> <div class="product-quantity"> <form id='myform' method='GET' action='cart_add.php'> <input type="hidden" name="product_id" value="<?php echo $id; ?>"> <div class="product-quantity"> <div class="cart-plus-minus"> <input class="cart-plus-minus-box" type="text" name="quantity" value="1"> </div> </div> </form> </div> </div> <ul class="pro__dtl__btn"> <li class="buy__now__btn"><a href="javascript:document.getElementById('myform').submit();">加入購物車</a></li> </ul> </div> </div> </div> </div> </section> <!-- End Product Details --> <!-- Start Product tab --> <section class="htc__product__details__tab bg__white pb--120"> <div class="container"> <div class="row"> <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12"> <ul class="product__deatils__tab mb--60" role="tablist"> <li role="presentation" class="active"> <a href="#description" role="tab" data-toggle="tab">商品描述</a> </li> </ul> </div> </div> <div class="row"> <div class="col-md-12"> <div class="product__details__tab__content"> <!-- Start Single Content --> <div role="tabpanel" id="description" class="product__tab__content fade in active"> <div class="product__description__wrap"> <div class="product__desc"> <h2 class="title__6">詳情</h2> <p><?php echo $current_product['description']; ?></p> </div> </div> </div> <!-- End Single Content --> </div> </div> </div> </div> </section> <!-- End Product tab --> <div class="only-banner ptb--10 bg__white"> </div> <?php require 'footer.php'; ?>
購物車頁面cart.php數據庫
<?php require '../tools.func.php'; require '../db.func.php'; require 'auth.php'; // 1. 從數據表當中查詢購物車數據 $uid = getSession('id', 'shop'); $prefix = getDBPrefix(); $sql = "SELECT id,price,products,quantity FROM {$prefix}cart WHERE uid = '$uid'"; $cart_page_data = queryOne($sql); $cart_page_data['products'] = json_decode($cart_page_data['products'], true); // 2. 遍歷數據到頁面 require 'header.php'; ?> <div class="cart-main-area bg__white"> <div class="container"> <div class="row"> <div class="col-md-12 col-sm-12 col-xs-12"> <?php if (hasInfo()) echo getInfo(); ?> <form action="#"> <div class="table-content table-responsive"> <table> <thead> <tr> <th class="product-thumbnail"></th> <th class="product-name">商品名稱</th> <th class="product-price">單價</th> <th class="product-quantity">數量</th> <th class="product-subtotal">總計</th> <th class="product-remove">編輯</th> </tr> </thead> <tbody> <?php if (!empty($cart_page_data)): ?> <?php foreach ($cart_page_data['products'] as $pid => $cart_product): ?> <tr> <td class="product-thumbnail"> <a href="product_details.php?id=<?php echo $pid ?>"> <img src="assets/uploads/default.jpeg" alt="product img"/> </a> </td> <td class="product-name"> <a href="product_details.php?id=<?php echo $pid ?>"> <?php echo $cart_product['product']['name']; ?> </a> </td> <td class="product-price"> <span class="amount">¥<?php echo $cart_product['product']['price']; ?></span> </td> <td class="product-quantity"> <input type="number" disabled value="<?php echo $cart_product['quantity']; ?>"/> </td> <td class="product-subtotal">¥<?php echo $cart_product['product']['price'] * $cart_product['quantity']; ?></td> <td class="product-remove"><a href="cart_del.php?product_id=<?php echo $pid; ?>">X</a></td> </tr> <?php endforeach; ?> <?php endif; ?> </tbody> </table> </div> <div class="row"> <div class="col-md-8 col-sm-7 col-xs-12"> <div class="buttons-cart"> <a href="index.php">繼續購物</a> </div> </div> <div class="col-md-4 col-sm-5 col-xs-12"> <div class="cart_totals"> <table> <tbody> <tr class="cart-subtotal"> <th>小計</th> <td><span class="amount">¥<?php echo isset($cart_page_data['price']) ? $cart_page_data['price'] : '0.0' ?></span></td> </tr> <tr class="shipping"> <th>快遞</th> <td> <ul id="shipping_method"> <li> <input type="radio" checked/> <label> 包郵 </label> </li> <li></li> </ul> </td> </tr> <tr class="order-total"> <th>總價</th> <td> <strong><span class="amount">¥<?php echo isset($cart_page_data['price']) ? $cart_page_data['price'] : '0.0' ?></span></strong> </td> </tr> </tbody> </table> <div class="wc-proceed-to-checkout" style="clear: both;"> <a href="checkout.php">去付款</a> </div> </div> </div> </div> </form> </div> </div> </div> </div> <div class="only-banner ptb--10 bg__white"> </div> <?php require 'footer.php'; ?>
查看購物車前確認登錄狀態auth.phpjson
<?php /** * Created by PhpStorm. * Date: 2019/1/24 * Time: 14:47 */ if (empty(getSession('username', 'shop'))) { header('location: login.php'); exit; }
增長商品添加到購物車cart_add.phpbootstrap
<?php /** * Created by PhpStorm. * Date: 2019/1/24 * Time: 13:18 */ require '../tools.func.php'; require '../db.func.php'; // 1. 接收商品 id $product_id = intval($_GET['product_id']); // 2. 接收數量 $quantity = intval($_GET['quantity']); if (empty($product_id) || empty($quantity)) { header('location: index.php'); exit; } $prefix = getDBPrefix(); $sql = "SELECT id, name, price, code, description FROM {$prefix}product WHERE id = '$product_id'"; $product = queryOne($sql); // 3. 判斷當前用戶是否已經登陸 if (!empty(getSession('username', 'shop'))) { // 若是已經登陸,將購物車數據存入到 數據表( // id, // products[ // 2 => [quantity => 4, product => [id => ,price =>,]], // 3 => [quantity => 2, product => []], // 4 => // ], // price, quantity, uid) $uid = getSession('id', 'shop'); $sql = "SELECT id,products,price,quantity,uid FROM {$prefix}cart WHERE uid = '$uid'"; $cart = queryOne($sql); if (!empty($cart)) { // 更新記錄 $products = json_decode($cart['products'], true); if (array_key_exists($product_id, $products)) { $products[$product_id]['quantity'] += $quantity; } else { $products[$product_id] = ['quantity' => $quantity, 'product' => $product]; } $products = addslashes(json_encode($products)); $cart_price = $cart['price'] + $quantity * $product['price']; $cart_quantity = $cart['quantity'] + $quantity; $sql = "UPDATE {$prefix}cart SET products = '$products', price = '$cart_price', quantity = '$cart_quantity' WHERE id = '{$cart['id']}'"; execute($sql); } else { // 生成 products, price, quantity, uid, created_at $products[$product_id] = ['quantity' => $quantity, 'product' => $product]; $price = $product['price'] * $quantity; $created_at = date('Y-m-d H:i:s'); // 添加新的數據 $products = addslashes(json_encode($products)); // \' \" $sql = "INSERT INTO {$prefix}cart(products, price, quantity, uid, created_at) VALUES('$products', '$price', '$quantity', '$uid', '$created_at')"; execute($sql); } header('location: cart.php'); // json [{2: {quantity: 4, product: {id: , price: }}}, {}] } else { // 若是沒有登陸,將購物車數據存入到 session $cart = getSession('cart', 'shop'); if (!empty($cart)) { // 更新session // 判斷當前購物車當中是否擁有當前商品 if (array_key_exists($product_id, $cart['products'])) { // 若是有,更新當前商品的數量,購物車總價,總數量 $cart['products'][$product_id]['quantity'] += $quantity; } else { // 若是沒有,新加入一個商品信息到購物車當中,更新購物車總價,總數量 $cart['products'][$product_id] = ['quantity' => $quantity, 'product' => $product]; } $cart['price'] += $quantity * $product['price']; $cart['quantity'] += $quantity; setSession('cart', $cart, 'shop'); } else { // 寫入session $products = [ $product_id => [ 'quantity' => $quantity, 'product' => $product ] ]; $cart_data = [ 'products' => $products, 'price' => $quantity * $product['price'], 'quantity' => $quantity ]; setSession('cart', $cart_data, 'shop'); } header('location: index.php'); }
刪除購物車商品cart_del.php
<?php /** * Created by PhpStorm. * Date: 2019/1/24 * Time: 15:06 */ require '../tools.func.php'; require '../db.func.php'; require 'auth.php'; $product_id = intval($_GET['product_id']); if (empty($product_id)) { header('location: cart.php'); exit; } // 0. auth // 1. 查詢當前用戶的購物車 $prefix = getDBPrefix(); $uid = getSession('id', 'shop'); $sql = "SELECT id,price,quantity,products FROM {$prefix}cart WHERE uid = '$uid'"; $cart = queryOne($sql); // 2. 將購物車當中對應id的商品刪除,更新總價格,總數量 $cart['products'] = json_decode($cart['products'], true); $cart['price'] -= $cart['products'][$product_id]['quantity'] * $cart['products'][$product_id]['product']['price']; $cart['quantity'] -= $cart['products'][$product_id]['quantity']; unset($cart['products'][$product_id]); $cart['products'] = addslashes(json_encode($cart['products'])); $sql = "UPDATE {$prefix}cart SET price = '{$cart['price']}', quantity = '{$cart['quantity']}', products = '{$cart['products']}' WHERE uid = '$uid'"; if (execute($sql)) { setInfo('刪除成功'); } else { setInfo('刪除失敗'); } header('location: cart.php');
模擬下單頁面checkout.php
<?php /** * Created by PhpStorm. * Date: 2019/1/24 * Time: 15:34 */ require '../tools.func.php'; require '../db.func.php'; require 'auth.php'; // 1. 獲取當前用戶的購物車數據 $uid = getSession('id', 'shop'); $prefix = getDBPrefix(); $sql = "SELECT id, price, quantity, products, uid FROM {$prefix}cart WHERE uid = '$uid'"; $cart = queryOne($sql); if (empty($cart)) { header('location: cart.php'); exit; } // 2. 將購物車數據寫入到訂單表當中 (id, price, quantity, products, created_at, uid) $price = $cart['price']; $quantity = $cart['quantity']; $products = $cart['products']; $created_at = date('Y-m-d H:i:s'); $sql = "INSERT INTO {$prefix}order(price, quantity, products, created_at, uid) VALUES('$price', '$quantity', '$products', '$created_at', '$uid')"; if (execute($sql)) { setInfo('下單成功'); } else { setInfo('下單失敗'); } header('location: order_status.php');
下單成功頁面order_status.php
require 'header.php'; ?> <div class="cart-main-area bg__white"> <div class="container"> <div class="row"> <div class="col-md-12 col-sm-12 col-xs-12" style="text-align: center;padding: 50px;"> <h1 style="color: green;"><?php if (hasInfo()) echo getInfo(); ?></h1> </div> </div> </div> </div> <div class="only-banner ptb--10 bg__white"> </div> <?php require 'footer.php'; ?>