如何计算星级平均评级

时间:2015-06-11 16:53:41

标签: php

我想计算所有标题的平均评分。我有6人在一个人下面,每个人都有5星。我希望显示所有评级(例如4.2或4)

这是我的代码

<li><p>Overall Experience</p>  
<?php 
    $starsLeft = 5 - $count_all_guest_star[0]->overall_experience_star;
    if($count_all_guest_star[0]->overall_experience_star>0):
       for( $i=1; $i<= $count_all_guest_star[0]->overall_experience_star; $i++) 
          { ?>
              <img src="<?php echo base_url(); ?>assets/img/on-stars.gif"/>
             <?php
          }
    endif;

    if ($starsLeft > 0) { // if there are any more stars left
       for ($i = 1; $i <= $starsLeft; $i++) {  // go through each remaining star
                                        // show it empty
       ?>
       <img src="<?php echo base_url(); ?>assets/img/off-stars.gif"/>     
       <?php }
     }
        ?>
    </li>
    <li><p>Communication</p>   
    <?php 
    $com_starsLeft = 5 - $count_all_guest_star[0]->communication_star;
        if( $count_all_guest_star[0]->communication_star > 0):
            for( $j=1; $j<= $count_all_guest_star[0]->communication_star; $j++) 
            {
            ?>
         <img src="<?php echo base_url(); ?>assets/img/on-stars.gif"/>
               <?php
         }
         endif;
         if ($com_starsLeft > 0) { // if there are any more stars left
             for ($i = 1; $i <= $com_starsLeft; $i++) {  // go through each remaining star
                                        // show it empty
             ?>
             <img src="<?php echo base_url(); ?>assets/img/off-stars.gif"/>     
             <?php }
          }
        ?></li>



                       $AverageRating = ((1*$starsLeft)+(2*$com_starsLeft)+(3*$acc_starsLeft)+(4*$clean_starsLeft)+(5*$pick_starsLeft) + (6*$pick_starsLeft))/6;

echo '$AverageRating';

请告诉我任何解决方案。

1 个答案:

答案 0 :(得分:0)

我认为你可以像这样计算平均值

$avg = ($count_all_guest_star[0]->overall_experience_star + 
        $count_all_guest_star[0]->communication_star +
        $count_all_guest_star[0]->heading3 +
        $count_all_guest_star[0]->heading4 +
        $count_all_guest_star[0]->heading5 + 
        $count_all_guest_star[0]->heading6) / 6; 

然后,您可以使用相同的方法来显示星星。

$avg_stars_left = 5 - $avg;