Mall product display function developed using PHP

PHPz
Release: 2023-07-01 13:40:02
Original
854 people have browsed it

Mall product display function developed using PHP

With the rapid development of e-commerce, developing a powerful shopping mall has become an urgent need for various enterprises. In the mall system, the product display function is a crucial part, which can directly affect the user's understanding of the product and desire to purchase. This article will introduce how to use PHP to develop a simple but powerful mall product display function.

First, we need to prepare a product table, which contains basic information such as product ID, name, price, and description. Assuming we have created a table called "products", we can use the following code example to insert some test data.

// 连接数据库
$conn = new mysqli("localhost", "root", "password", "mydb");
if ($conn->connect_error) {
    die("数据库连接失败: " . $conn->connect_error);
}

// 插入商品数据
$sql = "INSERT INTO products (name, price, description) VALUES ('iPhone 12', 6999, '苹果最新款手机')";
$conn->query($sql);

$sql = "INSERT INTO products (name, price, description) VALUES ('华为Mate 40', 5999, '华为旗舰机型')";
$conn->query($sql);

$sql = "INSERT INTO products (name, price, description) VALUES ('小米10', 3999, '小米旗舰手机')";
$conn->query($sql);

// 关闭数据库连接
$conn->close();
Copy after login

Next, we need to display the product list on the web page. We can achieve this with the help of HTML, CSS and PHP. Here is a simple example.

<!DOCTYPE html>
<html>
<head>
    <title>商品列表</title>
    <style>
        /* CSS 样式 */
        .product {
            margin-bottom: 20px;
        }
        .name {
            font-weight: bold;
        }
        .price {
            color: red;
        }
        .description {
            margin-top: 10px;
        }
    </style>
</head>
<body>
    <h1>商品列表</h1>

    <?php
    // 连接数据库
    $conn = new mysqli("localhost", "root", "password", "mydb");
    if ($conn->connect_error) {
        die("数据库连接失败: " . $conn->connect_error);
    }
    
    // 查询商品数据
    $sql = "SELECT * FROM products";
    $result = $conn->query($sql);
    
    // 遍历结果并输出商品列表
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            echo '<div class="product">';
            echo '<div class="name">' . $row["name"] . '</div>';
            echo '<div class="price">价格:¥' . $row["price"] . '</div>';
            echo '<div class="description">' . $row["description"] . '</div>';
            echo '</div>';
        }
    } else {
        echo "暂无商品";
    }
    
    // 关闭数据库连接
    $conn->close();
    ?>

</body>
</html>
Copy after login

With the above code, we created a simple product list page. The product information on the page is read from the database and displayed through PHP. Users can see the name, price and description of the product on the page.

When we run this page, the product list in the database will be displayed on the page. If there is no product data in the database, "No Products" will be displayed.

The above is a simple but powerful development example of the mall product display function. Based on actual needs, you can also extend the code, such as adding product images, category display, etc. I hope the examples in this article can help you develop a complete mall product display function.

The above is the detailed content of Mall product display function developed using PHP. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!