doushajian2018 2017-04-25 15:18
浏览 99
已采纳

如何用星星输出平均评分?

I have managed to output the average rating in a number format, but i would like to output it by using the star method as well.

This is my code so far that outputs the average rating by using numbers;

<?php

$sql = "
    SELECT round(avg(`rate`),2) AS `average_rate`, count(`rate`) AS `num_of_rating`
    FROM tbl_rating 
    WHERE hotel_id = '$id' ";
$query = mysqli_query($con,$sql);
$rating = mysqli_fetch_assoc($query);
?>

<div>
    <h4 style="color: #1478FF; font-size:30px; font-family:  " >    <?php echo $rating['average_rate'];  ?> Avergae rating </h4>
</div>

For instance;Stars

  • 写回答

1条回答 默认 最新

  • douxu3315 2017-04-26 15:37
    关注

    Please give this a try:

    <?php
    
    $sql = "
        SELECT round(avg(`rate`),2) AS `average_rate`, count(`rate`) AS `num_of_rating`
        FROM tbl_rating 
        WHERE hotel_id = '$id' ";
    $query = mysqli_query($con,$sql);
    $rating = mysqli_fetch_assoc($query);
    ?>
    
    <div>
        <h4 style="color: #1478FF; font-size:30px; font-family:  " >    <?php echo $rating['average_rate'];  ?> Average rating </h4>
    <?php
        $limit = floor($rating['average_rate']);
        $remainder = $rating['average_rate'] % 1.0;
    
        for( $i = 0; $i<$limit; $i++ ) {
            echo ("<img src=\"\\images\\fullstar.jpg\" />");
        }
    
        if ($remainder >= 0.5) echo ("<img src=\"\\images\\halfstar.jpg\" />");
    ?>
    </div>
    

    I'm not sure this will work. I have no way of testing the code, I'm a C# and ColdFusion developer. This is what a few minutes on Google gave me.

    To make this work, you need images of a full star called fullstar.jpg and of a half start called halfstar.jpg. They both need to be in a folder names images in your project root.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化