2015-06-11 189 views
0

我想計算所有標題的平均評分。我有一個人下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'; 

好心建議我任何解決方案。

+0

我想這應該在數學 – SuperDJ

+0

放在你什麼你的說法意思? – sleekdev

+0

我已經更新了我的代碼友善地建議我任何解決方案。 – sleekdev

回答

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; 
+0

不,我沒有獲得這種方法的平均水平。 – sleekdev

+0

@dev有什麼不對,如果這是你的問題:「如何計算星級平均評分」 – SuperDJ