Home > Backend Development > PHP Tutorial > How to use PHP to develop a simple online ordering function

How to use PHP to develop a simple online ordering function

WBOY
Release: 2023-09-20 13:12:01
Original
1306 people have browsed it

How to use PHP to develop a simple online ordering function

How to use PHP to develop a simple online ordering function

With the popularity of the Internet and people's pursuit of convenient life, more and more catering companies have begun to provide online Food ordering service. This kind of service not only makes it convenient for customers to place orders anytime and anywhere, but also improves the operational efficiency of catering companies. As a commonly used server-side programming language, PHP can help us develop simple and practical online ordering functions.

Below, I will introduce how to use PHP to develop a simple online ordering function and provide specific code examples.

  1. Database design

Before starting development, we first need to design a database to store dishes, orders and other related information. The following is a simple database design example:

  • Data table 1: menu (menu table)

    • id: dish ID, primary key, auto-increment
    • name: dish name, varchar type
    • price: dish price, decimal type
    • image: dish image, varchar type
    • description: dish description, varchar type
  • Data table 2: order (order table)

    • id: order ID, primary key, auto-increment
    • customer_name: Customer name, varchar type
    • customer_phone: Customer phone number, varchar type
    • total_price: Order total price, decimal type
    • create_time: Order creation time, datetime type
  1. Front-end interface design

Next, we need to design a simple and beautiful front-end interface to display the menu and receive user ordering requests. The following is a simple front-end interface design example:

<!DOCTYPE html>
<html>
<head>
    <title>在线订餐</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div class="container">
        <h1>在线订餐</h1>
        <form action="submit_order.php" method="post">
            <table>
                <tr>
                    <th>菜品</th>
                    <th>价格</th>
                    <th>数量</th>
                </tr>
                <?php
                // 使用PHP从数据库中获取菜单信息,并将其展示在界面上
                $db = new mysqli("localhost", "root", "password", "dbname");
                $query = "SELECT * FROM menu";
                $result = $db->query($query);
                while ($row = $result->fetch_assoc()) {
                    echo "<tr>
                            <td>{$row['name']}</td>
                            <td>{$row['price']}</td>
                            <td><input type='number' name='quantity[]' min='0'></td>
                          </tr>";
                }
                ?>
            </table>
            <h2>联系信息</h2>
            <input type="text" name="customer_name" placeholder="姓名" required>
            <br>
            <input type="tel" name="customer_phone" placeholder="电话号码" required>
            <br>
            <input type="submit" value="提交订单">
        </form>
    </div>
</body>
</html>
Copy after login
  1. Back-end function implementation

Next, we need to implement the back-end functions, including submitting orders and storing order information to the database etc. The following is a simple PHP code example:

<?php
// 获取用户提交的订单信息
$customer_name = $_POST['customer_name'];
$customer_phone = $_POST['customer_phone'];
$quantity = $_POST['quantity'];

// 计算订单总价
$total_price = 0;
for ($i = 0; $i < count($quantity); $i++) {
    $total_price += $quantity[$i] * $menu_price[$i];
}

// 将订单信息存储到数据库
$db = new mysqli("localhost", "root", "password", "dbname");
$query = "INSERT INTO order (customer_name, customer_phone, total_price, create_time) VALUES ('$customer_name', '$customer_phone', '$total_price', NOW())";
$db->query($query);
?>

<!DOCTYPE html>
<html>
<head>
    <title>订单提交成功</title>
</head>
<body>
    <h1>订单提交成功!</h1>
    <p>感谢您的订购!我们会尽快处理您的订单。</p>
</body>
</html>
Copy after login

The above are the detailed steps and code examples on how to use PHP to develop a simple online ordering function. I hope to be helpful!

The above is the detailed content of How to use PHP to develop a simple online ordering function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template