-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomment.php
More file actions
109 lines (98 loc) · 3.22 KB
/
comment.php
File metadata and controls
109 lines (98 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="comment.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<?php
// 資料庫連線細節(請使用您的實際認證)
$servername = "localhost";
$username = "root";
$password = "2002";
$dbname = "comment";
// 建立連線
$conn = new mysqli($servername, $username, $password, $dbname);
// 檢查連線
if ($conn->connect_error) {
die("連線失敗:" . $conn->connect_error);
}
// SQL 查詢以獲取假數據(有限 - 考慮使用專門的工具來獲取更真實的數據)
$sql = "SELECT
FLOOR(RAND() * 5) + 1 AS rating,
FLOOR(RAND() * 100) + 1 AS count
FROM
(SELECT 1 AS dummy) AS dummy_table
GROUP BY
rating
ORDER BY
rating DESC;";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$reviews = array();
while($row = $result->fetch_assoc()) {
$reviews[] = array(
'stars' => $row["rating"],
'count' => $row["count"],
);
}
} else {
echo "找不到數據";
}
$conn->close();
// 根據檢索到的數據計算平均評分(如果需要,請替換您的邏輯)
$totalReviews = 0;
$totalRating = 0;
foreach ($reviews as $review) {
$totalReviews += $review['count'];
$totalRating += $review['stars'] * $review['count'];
}
$averageRating = $totalReviews > 0 ? round($totalRating / $totalReviews, 1) : 0;
?>
<div class="heading">使用者評分</div>
<span class="fa fa-star checked"></span>
<span class="fa fa-star checked"></span>
<span class="fa fa-star checked"></span>
<span class="fa fa-star checked"></span>
<span class="fa fa-star"></span>
<p><?php echo $averageRating; ?> 平均分,基於 <?php echo $totalReviews; ?> 則評論。</p>
<hr style="border:3px solid #fffceb">
<div class="row">
<?php foreach ($reviews as $review): ?>
<div class="side">
<div><?php echo $review['stars']; ?> 星</div>
</div>
<div class="middle">
<div class="bar-container">
<div class="bar-<?php echo $review['stars']; ?>"></div>
</div>
</div>
<div class="side right">
<div><?php echo $review['count']; ?></div>
</div>
<?php endforeach; ?>
</div>
<div class="comment-section">
<h2>留言</h2>
<form class="comment-form">
<textarea name="comment" id="comment" placeholder="請輸入您的評論"></textarea>
<button type="submit">提交</button>
</form>
<h3>評論</h3>
<ul class="comment-list">
<li class="comment-item">
<div class="user-info">
<img src="user-avatar.jpg" class="fa fa-solid fa-user" alt="用戶頭像">
<div class="name-date">
<p class="name">John Doe</p>
<p class="date">2022年5月15日</p>
</div>
</div>
<p class="comment">這是一個很棒的產品!我強烈推薦給任何人。</p>
</li>
</ul>
</div>
</body>
</html>