Home  >  Article  >  Backend Development  >  How to use PHP Developer City to implement store rating statistics function

How to use PHP Developer City to implement store rating statistics function

WBOY
WBOYOriginal
2023-07-01 22:41:111384browse

How to use PHP Developer City to implement store rating statistics function

With the rapid development of e-commerce, more and more people choose to shop online. When shopping online, consumers often refer to other users' reviews of the merchant to judge the merchant's reputation and product quality. Therefore, the store rating statistics function of merchants is very important for attracting users and increasing sales. This article will introduce how to use PHP Developer City to implement the store rating statistics function.

  1. Create database

First, we need to create a database to store the merchant's evaluation information. You can use MySQL or other relational databases to create a database named "shop_rating", and then create a table named "rating" in the database. The table needs to contain the following fields:

  • id: The unique identifier of the evaluation, set as the primary key to grow automatically.
  • shop_id: The unique identifier of the merchant.
  • rating: The user's rating of the merchant, usually 1-5 stars.
  • comment: User’s comments about the merchant.
  • timestamp: The timestamp of the evaluation, used to sort and display the latest evaluation.
  1. Implementation of merchant rating statistics function

In the store page of the mall, we need to display the merchant's rating statistics. In order to implement this function, we can write a PHP function to query the database and calculate the merchant's rating.

First, we need to write a PHP function to query all evaluation information of a merchant in the database. The code example is as follows:

function getShopRatings($shop_id) {
    // 连接数据库
    $conn = new mysqli("localhost", "username", "password", "shop_rating");
    
    // 查询商家的所有评价信息
    $sql = "SELECT * FROM rating WHERE shop_id = '$shop_id'";
    $result = $conn->query($sql);
    
    // 返回评价信息数组
    $ratings = array();
    while ($row = $result->fetch_assoc()) {
        $ratings[] = $row;
    }
    
    return $ratings;
}

Then, we can write another PHP function to calculate the merchant's rating. The code example is as follows:

function calculateShopRating($shop_id) {
    $ratings = getShopRatings($shop_id);
    
    // 计算商家评分的平均值
    $total_rating = 0;
    foreach ($ratings as $rating) {
        $total_rating += $rating['rating'];
    }
    $avg_rating = $total_rating / count($ratings);
    
    return $avg_rating;
}

Finally, call these two functions in the merchant's store page to display the merchant's rating statistics. The code example is as follows:

$shop_id = $_GET['shop_id'];
$avg_rating = calculateShopRating($shop_id);

echo "店铺评分:$avg_rating";
  1. Implementation of user evaluation function

In order to allow users to evaluate merchants, we also need to write a PHP function to insert evaluation information into the database middle. The code example is as follows:

function addShopRating($shop_id, $rating, $comment) {
    // 连接数据库
    $conn = new mysqli("localhost", "username", "password", "shop_rating");
    
    // 插入评价信息
    $sql = "INSERT INTO rating (shop_id, rating, comment, timestamp) VALUES ('$shop_id', '$rating', '$comment', NOW())";
    $conn->query($sql);
    
    // 返回插入成功的评价ID
    return $conn->insert_id;
}

In the merchant's store page, we can write a form to collect user reviews and call the above function to insert the review information. The code example is as follows:

<form action="submit_rating.php" method="post">
    <input type="hidden" name="shop_id" value="<?php echo $shop_id; ?>">
    <label for="rating">评分:</label>
    <select name="rating" id="rating">
        <option value="1">1星</option>
        <option value="2">2星</option>
        <option value="3">3星</option>
        <option value="4">4星</option>
        <option value="5">5星</option>
    </select>
    <br>
    <label for="comment">评论:</label>
    <textarea name="comment" id="comment" rows="5"></textarea>
    <br>
    <input type="submit" value="提交评价">
</form>

In the "submit_rating.php" file, we can read the evaluation information in the form and call the above function to insert the evaluation information into the database.

Through the above steps, we can use the PHP Developer City to implement the store rating statistics function. Users can evaluate merchants, and the average rating and evaluation information of the merchant will be displayed on the store page. This not only improves the credibility and sales of merchants, but also makes it easier for users to choose high-quality goods and services.

The above is the detailed content of How to use PHP Developer City to implement store rating statistics function. 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