Home Backend Development PHP Tutorial PHP implements a multi-functional shopping website

PHP implements a multi-functional shopping website

May 18, 2018 am 09:43 AM
php Multifunction shopping website

This article mainly introduces how to use PHP to implement a multi-functional shopping website. Interested friends can refer to it. I hope it will be helpful to everyone.

1. Pages that need to be implemented:

Index.aspx: Browse the product page, display the product list, the user can click "Add to shopping cart" ".

ViewCart.aspx: View the shopping cart page, which displays purchased product information. You can click "Delete" and "Submit to add an order to purchase" the product

ViewAccount.aspx: View personal account balance

Login.aspx: Login page

2. Implementation functions:

1. Display product list

2. Implement the purchase function, and dynamically display the quantity of goods in the shopping cart and the total price of the goods when purchasing

3. After clicking to view the shopping cart, the purchased goods will be displayed. Pay attention to the "Purchase Quantity" column. If you click to buy a product multiple times, its "Purchase Quantity" will continue to increase.

4. Delete the purchased items in the shopping cart.

If the "Purchase Quantity" of a product is 1, when you click "Delete", the product will be deleted directly from the shopping cart;

If the "Purchase Quantity" of the product is greater than 1 , when you click "Delete" once, the purchase quantity will be reduced by 1. When the purchase quantity of the product reaches 1, and then click Delete, the product will be deleted

5. After viewing the shopping cart, you can also click "Browse Products" to continue purchasing. And the quantity of goods purchased and the total price are displayed above.

6. After "View Shopping Cart", you can submit the order.

But when submitting an order, the following functions must be completed:

(a) Check whether the user is logged in. If not logged in, go to the Login.aspx page

(b) Check whether the user account balance is sufficient for this purchase

(c) Check whether the inventory quantity is sufficient for this purchase

(d) If all the above conditions are met

i. Deduct the total price of this purchase from the user account

ii. Deduct the purchase quantity of each product from the product inventory

iii. Add it to the order table and order content table Add the product information of this purchase

7. Click View Account to view the user’s account balance

The operation code is as follows:

1. First make a login page: 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="img/logo.png" width="200" height="50" />
  <p >果 蔬 网</p>
 </p>
 <h3 class="title">用户登录</h3> 
 <img src="./img/果蔬专场.jpg" width="500" height="400" class="ps" />
 <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>
Copy after login

The effect is as shown:

2. Create a login processing page: 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 "登陆失败!";
}
Copy after login

This way you can contact the database. This is the login account and password for the database. Verify the account and password, and then jump to the homepage: shopping_list.php

3. Now make the homepage page: 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];//购买数量
   $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>
Copy after login

4. Then do the processing page of the homepage: 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; 
}
Copy after login

The effect is as shown:

5. Then check the shopping cart page and you can see the shopping cart The products, unit price and total price in: 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>产地</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>
Copy after login

The effect is as shown:

6. Then do the deletion processing page 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");
Copy after login

7. Then do the submission page: 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");
Copy after login

The user account balance has been reduced:



Related recommendations:

PHP implements shopping website

Detailed explanation of examples of PHP implementing shopping website

php code example of how to implement a multi-functional shopping website

##

The above is the detailed content of PHP implements a multi-functional shopping website. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1668
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles