Home >Backend Development >PHP Tutorial >PHP mall development skills: Design rating and comment management functions

PHP mall development skills: Design rating and comment management functions

WBOY
WBOYOriginal
2023-07-29 08:52:51817browse

PHP mall development skills: Design rating and comment management functions

Introduction:
With the rapid development of the Internet, e-commerce has become one of the main ways for people to shop. In order to attract more users, the mall website must provide complete rating and comment management functions so that users can evaluate products and other users can view and refer to these evaluations. This article will introduce how to use the rating and review management functions of PHP Developer City, and attach a working example.

1. Database design:
First, we need to design a database to store information related to product ratings and comments. We can create two tables: one to store product rating information (ratings) and the other to store product review information (comments).

  1. Form 1 - ratings:
    The table will contain the following fields:
  2. id: the unique identifier of the rating
  3. product_id: the unique identifier of the product , used to associate products and ratings
  4. user_id: the user's unique identifier, used to associate users and ratings
  5. rating: rating score (usually an integer between 0-5)
  6. created_at: Timestamp when the rating was created
  7. Form 2 - comments:
    The form will contain the following fields:
  8. id: The unique identifier of the comment
  9. product_id: The unique identifier of the product, used to associate products and comments
  10. user_id: The unique identifier of the user, used to associate users and comments
  11. comment: Comment content
  12. created_at: Timestamp of comment creation

2. Front-end development:
Next, we need to display rating and comment information on the front-end page of the mall website, and be able to receive user ratings and Comment.

  1. Display rating information:
    We can use HTML and CSS to design the page layout, and use PHP to get the rating information from the database and display it on the page. Here is a simple sample code:
<div class="rating">
    <?php
    $productId = $_GET['product_id']; // 通过URL参数获取商品ID
    // 查询评分表格中对应商品ID的所有评分
    $query = "SELECT rating FROM ratings WHERE product_id = $productId";
    $result = mysqli_query($connection, $query);
    
    $totalRatings = mysqli_num_rows($result); // 获取评分总数
    $averageRating = 0;
    while ($row = mysqli_fetch_assoc($result)) {
        // 计算所有评分的平均值
        $averageRating += $row['rating'];
    }
    $averageRating = $averageRating / $totalRatings;
    ?>
    <h3>商品评分:<?php echo $averageRating; ?></h3>
    <p>评分人数:<?php echo $totalRatings; ?></p>
</div>
  1. Display review information:
    Similarly, we can use HTML and CSS to design the page layout, and use PHP to get the review information from the database , and display it on the page. The following is a simple sample code:
<div class="comments">
    <?php
    // 查询评论表格中对应商品ID的所有评论
    $query = "SELECT comment FROM comments WHERE product_id = $productId";
    $result = mysqli_query($connection, $query);
    
    while ($row = mysqli_fetch_assoc($result)) {
        // 显示每条评论
        echo "<p>" . $row['comment'] . "</p>";
    }
    ?>
</div>

3. Back-end development:
Next, we need to process the ratings and comments submitted by users.

  1. Processing ratings:
    When a user rates a product, we need to save the rating information to the database. Here is a simple sample code:
$productId = $_POST['product_id']; // 通过表单提交获取商品ID
$userId = $_SESSION['user_id']; // 获取当前用户的ID
$rating = $_POST['rating']; // 获取用户提交的评分分数

$query = "INSERT INTO ratings (product_id, user_id, rating) VALUES ($productId, $userId, $rating)";
mysqli_query($connection, $query);
  1. Handling comments:
    When a user submits a comment, we need to save the comment information to the database. The following is a simple sample code:
$productId = $_POST['product_id']; // 通过表单提交获取商品ID
$userId = $_SESSION['user_id']; // 获取当前用户的ID
$comment = $_POST['comment']; // 获取用户提交的评论内容

$query = "INSERT INTO comments (product_id, user_id, comment) VALUES ($productId, $userId, '$comment')";
mysqli_query($connection, $query);

Summary:
Through the above code example, we can implement the rating and comment management functions, and allow users to evaluate products. Of course, this is just a simple example and you can expand and optimize it according to your actual needs. I hope this article will be helpful to the rating and comment management functions in PHP mall development!

The above is the detailed content of PHP mall development skills: Design rating and comment management functions. 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