Home > Backend Development > PHP Tutorial > Detailed explanation of the mall product review function developed using PHP

Detailed explanation of the mall product review function developed using PHP

王林
Release: 2023-07-02 06:12:02
Original
800 people have browsed it

Detailed explanation of the mall product review function developed using PHP

In modern e-commerce platforms, the product review function is a very important function. Merchants and consumers can communicate and understand the true situation of products through product reviews, and can also provide valuable opinions and suggestions to merchants. In this article, I will introduce in detail how to use PHP to develop a simple mall product review function, and attach corresponding code examples.

First, we need to create a database to store product review information. You can use MySQL or other relational databases to create a review table. The table structure is as follows:

CREATE TABLE comments (
  id INT AUTO_INCREMENT PRIMARY KEY,
  product_id INT NOT NULL,
  user_id INT NOT NULL,
  content TEXT NOT NULL,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
Copy after login

In this table, we store the review id, product id, user id, review content, creation time and update time. The table structure can be appropriately adjusted according to actual needs.

Next, we need to display product reviews on the product details page and provide the function to submit reviews. The following is an example product details page code:

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

// 查询商品信息
// TODO

// 查询商品评论
$sql = "SELECT * FROM comments WHERE product_id = $product_id";
$result = mysqli_query($conn, $sql);

// 显示商品信息
// TODO

// 显示商品评论
while ($row = mysqli_fetch_assoc($result)) {
    echo "<div class='comment'>";
    echo "<p>{$row['content']}</p>";
    echo "<span class='timestamp'>{$row['created_at']}</span>";
    echo "</div>";
}
?>

<!-- 提交评论表单 -->
<form method="post" action="submit_comment.php">
    <input type="hidden" name="product_id" value="<?php echo $product_id ?>">
    <textarea name="content" placeholder="请输入评论内容"></textarea>
    <button type="submit">提交评论</button>
</form>
Copy after login

In the above code, the product information is first queried based on the passed product id, and then the reviews of the product are queried and displayed on the page. Finally, display a simple comment submission form.

Next, we need to implement the function of submitting comments. Create a file named submit_comment.php and put the following code into it:

<?php
// 获取提交的评论信息
$product_id = $_POST['product_id'];
$user_id = $_SESSION['user_id'];
$content = $_POST['content'];

// 插入评论到数据库
$sql = "INSERT INTO comments (product_id, user_id, content) VALUES ($product_id, $user_id, '$content')";
$result = mysqli_query($conn, $sql);

// 判断评论是否插入成功
if ($result) {
    echo "评论提交成功!";
} else {
    echo "评论提交失败!";
}
?>
Copy after login

In the above code, we first obtain the submitted comment information, including product id, user id and comment content. Then insert the comment information into the database and determine whether the insertion is successful.

Finally, we can also implement functions such as paging and sorting comments. The following is a simple code example to implement paging:

// 设置每页显示的评论数量
$commentPerPage = 10;

// 获取当前页码,默认为第一页
$page = isset($_GET['page']) ? $_GET['page'] : 1;

// 查询评论总数
$sql = "SELECT COUNT(*) AS total FROM comments WHERE product_id = $product_id";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$totalComments = $row['total'];

// 计算总页数
$totalPages = ceil($totalComments / $commentPerPage);

// 查询当前页的评论
$offset = ($page - 1) * $commentPerPage;
$sql = "SELECT * FROM comments WHERE product_id = $product_id LIMIT $offset, $commentPerPage";
$result = mysqli_query($conn, $sql);

// 显示评论
while ($row = mysqli_fetch_assoc($result)) {
    // TODO
}

// 显示分页链接
for ($i = 1; $i <= $totalPages; $i++) {
    echo "<a href='product_detail.php?product_id=$product_id&page=$i'>$i</a> ";
}
Copy after login

The above is a simple implementation example of the mall product review function. Through the above code, we can display product reviews on the product details page and provide the function of submitting reviews. At the same time, functions such as paging and sorting of comments can also be implemented.

Of course, this is just a basic implementation, and there are many details and improvements that can be made. I hope this article can help you understand and use the product review function of PHP Developer City.

The above is the detailed content of Detailed explanation of the mall product review function developed using PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template