Home  >  Article  >  Backend Development  >  Implementation method of product rating statistics function in PHP developer city

Implementation method of product rating statistics function in PHP developer city

PHPz
PHPzOriginal
2023-06-29 09:38:44961browse

How to implement the product rating statistics function in PHP Developer City

With the development of e-commerce, more mall websites have begun to pay attention to product reviews and ratings to provide users with more real shopping references. In the PHP Developer City, implementing the product rating statistics function is a very important part. This article will introduce a method to implement product rating statistics.

1. Database design

First, we need to design the database table related to product rating statistics. Generally speaking, at least one product table and one rating table are required.

The product table (product) contains the following fields:

  • id: product ID
  • name: product name
  • price: product price
  • ...

The rating table (rating) contains the following fields:

  • id: Rating ID
  • product_id: Product ID
  • user_id: User ID
  • rating: Rating (usually an integer from 1 to 5)
  • timestamp: Rating time
  • ...

2. Product details page

In the product details page, we need to display the rating statistics of the product. The code is as follows:

<?php
// 获取商品ID
$product_id = $_GET['product_id'];

// 查询商品信息
$query = "SELECT * FROM product WHERE id = $product_id";
$result = mysqli_query($conn, $query);
$product = mysqli_fetch_array($result);

// 查询评分统计信息
$query = "SELECT AVG(rating) AS average_rating, COUNT(*) AS total_ratings FROM rating WHERE product_id = $product_id";
$result = mysqli_query($conn, $query);
$rating_info = mysqli_fetch_array($result);

// 输出评分统计信息
echo "商品名称:" . $product['name'] . "<br>";
echo "商品价格:" . $product['price'] . "<br>";
echo "平均评分:" . $rating_info['average_rating'] . "<br>";
echo "总评分数:" . $rating_info['total_ratings'] . "<br>";
?>

3. Rating function

In the product details page, we also need to add a rating function so that users can rate the product. The code is as follows:

<?php
// 处理用户提交的评分表单
if(isset($_POST['rating'])) {
  // 获取用户ID
  $user_id = $_SESSION['user_id'];
  
  // 获取商品ID
  $product_id = $_GET['product_id'];
  
  // 获取评分
  $rating = $_POST['rating'];
  
  // 插入评分记录
  $query = "INSERT INTO rating (product_id, user_id, rating) VALUES ($product_id, $user_id, $rating)";
  mysqli_query($conn, $query);
  
  // 刷新当前页面
  header("Location: product.php?product_id=$product_id");
  exit();
}

// 输出评分表单
echo "<form action='product.php?product_id={$product['id']}' method='post'>";
echo "评分:<input type='number' name='rating' min='1' max='5' required>";
echo "<input type='submit' value='提交'>";
echo "</form>";
?>

4. Rating statistics update

After the user rates the product, the rating statistics of the product need to be updated. After inserting the rating record, you can add the following code:

// 更新评分统计信息
$query = "UPDATE product SET average_rating = (SELECT AVG(rating) FROM rating WHERE product_id = $product_id), total_ratings = (SELECT COUNT(*) FROM rating WHERE product_id = $product_id) WHERE id = $product_id";
mysqli_query($conn, $query);

5. Front-end display

The front-end display can be beautified and styled according to actual needs, and can be implemented using JavaScript and CSS. You can achieve dynamic display of ratings, such as star ratings, by using front-end frameworks such as jQuery.

Summary:

Through the above steps, we can realize the product rating statistical function. First, design the database table structure, and then display the rating statistics and rating form on the product details page. At the same time, the method of updating the rating statistics also needs to be implemented in the background. Finally, beautify and style the front end as needed.

This is a simple method to implement the product rating statistical function, which can be expanded and optimized according to actual project needs, such as considering issues such as deduplication and security. I hope this article can help you implement the product rating statistics function.

The above is the detailed content of Implementation method of product rating statistics function in PHP developer city. 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