我对如何计算平均评级系统感到有点困惑。用户完全可以提供费率并将存储在数据库中,如下图所示
因此,想法是averagestar的价值来自平均评级表。例如,prov @ gmail有4个输入速率,平均值= 4,来自16/4。 (总星数/总费率)
从下面显示的代码中,我认为我的逻辑是错误的。 UPDATE平均SQL无效。
$sql= "
INSERT INTO rating VALUES
(DEFAULT,'$prov', '$star','$detail', '$user')
";
$sql2="
DELETE FROM orderform
WHERE confirmation = 4
AND provider_username = '$prov'
AND user_username = '$user'
AND confirmation = 4
";
$sql3= "
SELECT AVG(rating)
FROM rating
WHERE provider_username = '$prov'
";
if (mysqli_query($conn, $sql) && mysqli_query($conn, $sql2)) {
$result = mysqli_query($conn, $sql3);
while($row = mysqli_fetch_array($result)) {
$sql= "
UPDATE theprovider
SET averagerating = '$result'
WHERE provider_username = '$prov'";
if (mysqli_query($conn, $sql)) {
echo 'success';
}
}
有帮助吗?
答案 0 :(得分:0)
你不能SET averagerating='$result'
- 你应该用$ row来更新。
尝试:
$sql= "INSERT INTO rating VALUES (DEFAULT,'$prov', '$star','$detail', '$user')";
$sql2="DELETE FROM orderform WHERE confirmation=4 AND provider_username='$prov' AND user_username='$user' AND confirmation=4";
$sql3= "SELECT AVG(rating) AS myAvg FROM rating WHERE provider_username='$prov' GROUP BY provider_username";
if (mysqli_query($conn, $sql) && mysqli_query($conn, $sql2)) {
$result = mysqli_query($conn, $sql3);
while($row = mysqli_fetch_array($result)) {
$sql= "UPDATE theprovider SET averagerating='".$row["myAvg"]."' WHERE provider_username='$prov'";
if (mysqli_query($conn, $sql)) {
echo 'success';
}
}
编辑:添加了GROUP BY子句。
答案 1 :(得分:0)
AVG
是一个聚合函数,因此应与GROUP BY
一起使用。
SELECT AVG(rating) FROM rating WHERE provider_username = 'prov@gmail.com' GROUP BY provider_username;
答案 2 :(得分:0)
star average rating dynamic from database using php
try : - this code
Datbase -
<?php
$sum=0;
$sel_sups = my_select("SELECT SUM(Rating) AS sum,Count(UserId) AS User FROM tbl_rating WHERE ProductId='".$ProductId."'"); // hard coded in here value UserId And ProductId
$result=json_decode($sel_sups);
if($result->response==1){
foreach($result->result as $myresults){
$value = $myresults->sum;
$sum = $value;
$avg = $sum/$myresults->User;
$no_format = number_format(floor($avg*100)/100,2, '.', '');
$percent = $avg*100/5;
echo '<div class="ratings">
<div class="empty-stars"></div>
<div class="full-stars" style="width:'.$percent.'%">
</div>
</div>'.'<a href="#" data-toggle="modal" data-backdrop="static" data-target="#reviewmodel" style="background: #5F283D;padding:10px;color: #fff;font-size: 12px;margin-left:5%">Write a Review</a>';
echo '<div>
<p style="font-size: 13px;color:#333;margin-left:1%;margin-top: 1%;">Average Rating '.$no_format.'</p>
</div>';
}
}
?>
{{1}}
答案 3 :(得分:0)
这是实现这一目标的简单干净的方法:
<?php if(!empty($answer->AggRating)){ // This is fetched from your db
$rating = (int)$answer->AggRating; // Converting it to integer
$img="";
$i=1;
while($i<=$rating){
$img=$img."<i class='fa fa-star' style='color: yellow;'></i>"; // You can edit your own html star.
$i=$i+1;
for($i=$rating+1; $i<=5; $i++){ // This is for non-star rating
$img=$img."<i class='fa fa-star'></i>";
}
echo $img;
} ?>