Home Backend Development PHP Tutorial PHP operation to implement a multi-functional shopping website

PHP operation to implement a multi-functional shopping website

Sep 28, 2017 am 10:44 AM
php Multifunction shopping website

PHP operation to implement a multi-functional shopping website

1. Pages to be implemented:

Index.aspx:浏览商品页面,显示商品列表,用户可以点击“加入购物车“。
ViewCart.aspx:查看购物车页面,显示已购买的商品信息,可以点击“删除“和“提交添加订单购买”商品
ViewAccount.aspx:查看个人账户余额
Login.aspx:登录页面
Copy after login

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, click "Delete" to delete the product directly from the shopping cart;
If the "Purchase Quantity" of a product is greater than 1, click "Delete" once When , reduce its purchase quantity 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 display the quantity of goods purchased and the total price 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 the user account Whether the balance is sufficient for this purchase

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

(d) If the above conditions are met, then

i. From 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 this purchase to the order table and order content table Purchased product information

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="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" style="margin-top: 10px; margin-left: 100px;" />
        <p style="height: 50px; width: 300px; color: green;float: right; font-size: 50px; margin-right: 350px">果 蔬 网</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 in the figure:

2. Make 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

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

##3. Now create 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="stylesheet" type="text/css"/>
    </head>
    <body>
        <h2 style="margin-left: 550px; margin-top: 80px;">水果列表</h2>
    <?php
    session_start();
Copy after login

//1. Find out how many products are in the shopping cart and the total price

  $uid = $_SESSION["uid"];
    if(empty($_SESSION["uid"])){
        header("location:loginpage.php");
        exit;
    }
    require_once "./DBDA.class.php";
    $db = new DBDA();
Copy after login

//If there are products in the shopping cart, take out the value

 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" style="float: right; margin-top: -25px; margin-right: 330px; color: blueviolet; font-size: 20px;">
            登录
            </a>
        <table class="table table-bordered" style="max-width: 800px; margin-left: 250px;">
            <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" style="margin-left: 250px;">查看购物车</a>
    </body>
</html>
Copy after login

4. Then do the processing page of the home page: 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 in the figure:

5. Then check the shopping cart page, you can see the products in the shopping cart, the unit price and the total price: 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="stylesheet" type="text/css"/>
    </head>
    <?php
    session_start();
    $uid = $_SESSION["uid"];
    if(empty($_SESSION["uid"])){
        header("location:loginpage.php");
        exit;
    }    
    ?>
    <body>
        <h2 style="margin-left: 550px; margin-top: 100px;">购物车清单</h2>        
        <table class="table table-bordered" style="max-width: 800px; margin-left: 250px;">
            <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]}" style="margin-left: 250px;">提交订单</a>                
    </body>
</html>
Copy after login

The effect is as shown:


6. Do the deletion processing page goodsdel.php

<?phpsession_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
<?phpsession_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:


The above is the detailed content of PHP operation to implement 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months 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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles