Home  >  Article  >  Backend Development  >  How to use PHP to implement product inventory statistical reports

How to use PHP to implement product inventory statistical reports

王林
王林Original
2023-08-18 10:25:071509browse

How to use PHP to implement product inventory statistical reports

How to use PHP to implement product inventory statistical reports

In the actual e-commerce operation process, the statistics of product inventory is a very important link. It can not only help companies understand the inventory status of each product in real time, but also provide a reference for the company's inventory management. This article will introduce how to use PHP language to implement a simple product inventory statistics report.

First, we need to create a PHP file named inventory.php to handle operations related to inventory statistics. In this file, we need to connect to the database, obtain the product list, and calculate the total inventory. The following is a basic code example:

<?php
// 连接数据库
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "inventory";

$conn = new mysqli($servername, $username, $password, $dbname);

// 获取商品列表
$sql = "SELECT * FROM products";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $products = array();

    // 计算库存总量
    $totalQuantity = 0;

    while ($row = $result->fetch_assoc()) {
        $product = array(
            'id' => $row['id'],
            'name' => $row['name'],
            'quantity' => $row['quantity']
        );

        $products[] = $product;
        $totalQuantity += $row['quantity'];
    }

    // 输出报表
    echo "<h1>商品库存统计报表</h1>";
    echo "<table>";
    echo "<tr><th>ID</th><th>名称</th><th>库存数量</th></tr>";

    foreach ($products as $product) {
        echo "<tr>";
        echo "<td>".$product['id']."</td>";
        echo "<td>".$product['name']."</td>";
        echo "<td>".$product['quantity']."</td>";
        echo "</tr>";
    }

    echo "</table>";
    echo "<p>总库存数量:".$totalQuantity."</p>";
} else {
    echo "暂无商品数据";
}

$conn->close();
?>

In the above code, we first connect to the database and execute a SELECT statement to obtain the product list. We then use a loop to iterate through each item, store its information in an array, and accumulate the inventory quantity. Finally, we use HTML tables to present statistical reports and output the total inventory quantity.

It should be noted that the above code is only an example, and assumes that a database named inventory already exists, and there is a table named products, which contains id, name and quantity fields to store product data. . Depending on the actual situation, you need to make appropriate modifications based on your database structure and field naming.

Before using this code, you need to set up a PHP environment on your server and save the above code in a file named inventory.php. You can then view the generated product inventory statistics report by accessing the file.

The above is the detailed content of How to use PHP to implement product inventory statistical reports. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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