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 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题