doutao1282 2015-10-14 14:53
浏览 20

如何使这个PHP功能更有效

Basically i am trying to do a simple 5 star rating system, using font awesome classes for the stars.

Full star

<i class="fa fa-star"></i>

No star

<i class="fa fa-star-0"></i>

Half star

<i class="fa fa-star-half-0"></i>

So i loop through each review for my service like seen below:

$i=0;
$reviewRating = 0;

foreach($service['reviews'] as $review){
   $i++;
   $reviewRating += $review['rating'];
}

From this i now have the amount of reviews and the total of all review ratings added together.

I pass both of these values into my function as seen below:

function get_rating_stars($reviewRating,$i){
    //rounding number to nearest .5
    $rating = round($reviewRating/$i * 2) / 2;
    $cnt = 0;
    $class = "";//class active gives the star a colour, if empty it shows as a empty star.
    $zeros = false;//this is for when we have gone past the .5 meaning any further stars have to be a zero(empty star).
    $starList ="";//This will be returned when built up

    while($cnt < 5){//because there will always be 5 stars showing, regardless to full half or empty
        if($zeros){
            $starList.= '<li><i class="fa fa-star-o"></i></li>';
            $cnt++;
        }else{
            if($rating == 0.5){
                $starList.= '<li class="active"><i class="fa fa-star-half-o"></i></li>';
                $zeros = true;//now we have output the half star all following will be empty
                $cnt++;
            }else{
                //the first loop will normally start here unless rating starts at 0.5
                if($rating > 0.5){$class = "active";}else{$class = "";}
                $starList.= '<li class="'. $class  .'"><i class="fa fa-star"></i></li>';
                $cnt++;
                $rating--;
            }
        }
    }
    return $starList;
}

So this function i made does do the trick and works exactly how i want it too, however that it has to repeat this process for every single service on the page. So when i loop through all services i loop through each review and do this, it just seems a bit over kill.

This function could theoretically get called 20 times , as i can show up to 20 services on one page.

Does anyone know how i can make this more efficient?

  • 写回答

1条回答 默认 最新

  • dongyuan2652 2015-10-14 15:19
    关注

    I wrote this function (from yours). Although if it works then yours should be fine.

    I have no idea if mine is faster than yours either so it's merely an alternative. (Plus I think mine is a little easier to read, but that's just preference)

    function get_rating_stars($reviewRating,$i){
        // Calculate Rating
        $rating         = max(0, min(5, round($reviewRating/$i * 2) / 2));
        $halfStar       = (int)$rating != $rating;
        $arrayResult    = array_fill(0, $rating, '<li class="active"><i class="fa fa-star"></i></li>'); // Full Stars
        if($halfStar){
            $arrayResult[] = '<li class="active"><i class="fa fa-star-half-o"></i></li>'; // One half Star
        }
        $arrayResult    = array_pad($arrayResult, 5, '<li><i class="fa fa-star-o"></i></li>');  // Empty Start
        return implode('', $arrayResult);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错