简体   繁体   中英

how to calculate star average rating

I want to calculate average rating of all heading. I have 6 heading under one person and every person have 5 star. I want to show over all rating (eg 4.2 or 4 )

Here is my code

<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';

kindly advice me any solution.

I think you can compute the average like this

$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; 

You can then use the same method to display stars.

$avg_stars_left = 5 - $avg;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM