Home >Backend Development >PHP Tutorial >How to use PHP Developer City to implement store rating statistics function
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.
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:
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";
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!