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
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).
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.
<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>
<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.
$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);
$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!