user537137
user537137

Reputation: 614

Best star rating solution for PHP

I'm wondering what the best and most efficient way to write up the following PHP code would be?

    if ($av == 1) echo '/images/1-star.png';
    if ($av > 1 && < 2) echo '/images/1-half-star.png';
    if ($av == 2) echo '/images/2-star.png';
    if ($av > 2 && < 3) echo '/images/2-half-star.png';

Following this same pattern up to 5 stars.

Upvotes: 4

Views: 8402

Answers (7)

Jinu Joseph Daniel
Jinu Joseph Daniel

Reputation: 6291

This may not be the most efficient way. But I find it clean code. Check this.

function renderStarRating($rating,$maxRating=5) {
    $fullStar = "<li><i class = 'fa fa-star'></i></li>";
    $halfStar = "<li><i class = 'fa fa-star-half-full'></i></li>";
    $emptyStar = "<li><i class = 'fa fa-star-o'></i></li>";
    $rating = $rating <= $maxRating?$rating:$maxRating;

    $fullStarCount = (int)$rating;
    $halfStarCount = ceil($rating)-$fullStarCount;
    $emptyStarCount = $maxRating -$fullStarCount-$halfStarCount;

    $html = str_repeat($fullStar,$fullStarCount);
    $html .= str_repeat($halfStar,$halfStarCount);
    $html .= str_repeat($emptyStar,$emptyStarCount);
    $html = '<ul>'.$html.'</ul>';
    return $html;
}

Upvotes: 6

Shivaram Mahapatro
Shivaram Mahapatro

Reputation: 127

you should use a ternary operator in each label instead of radio buttons

<span class="rating">
                                <input type="radio" id="star5" name="rate<?php echo $cnt; ?>" value="5" <?php echo ($row_avg_prod_rvw['AVG_Rating'] == '5') ? 'checked' : '' ?>/>
                                <label class = "full" for="star5" title="5 stars"></label>
                                <input type="radio" id="star4half" name="rate<?php echo $cnt; ?>" value="4.5" <?php echo ($row_avg_prod_rvw['AVG_Rating'] == '4.5') ? 'checked' : '' ?>/>
                                <label class="half" for="star4half" title="4.5 stars"></label>
                                <input type="radio" id="star4" name="rate<?php echo $cnt; ?>" value="4" <?php echo ($row_avg_prod_rvw['AVG_Rating'] == '4') ? 'checked' : '' ?>/>
                                <label class = "full" for="star4" title="4 stars"></label>
                                <input type="radio" id="star3half" name="rate<?php echo $cnt; ?>" value="3.5" <?php echo ($row_avg_prod_rvw['AVG_Rating'] == '3.5') ? 'checked' : '' ?>/>
                                <label class="half" for="star3half" title="3.5 stars"></label>
                                <input type="radio" id="star3" name="rate<?php echo $cnt; ?>" value="3" <?php echo ($row_avg_prod_rvw['AVG_Rating'] == '3') ? 'checked' : '' ?>/>
                                <label class="full" for="star3" title="3 stars"></label>
                                <input type="radio" id="star2half" name="rate<?php echo $cnt; ?>" value="2.5" <?php echo ($row_avg_prod_rvw['AVG_Rating'] == '2.5') ? 'checked' : '' ?>/>
                                <label class="half" for="star2half" title="2.5 stars"></label>
                                <input type="radio" id="star2" name="rate<?php echo $cnt; ?>" value="2" <?php echo ($row_avg_prod_rvw['AVG_Rating'] == '2') ? 'checked' : '' ?>/>
                                <label class = "full" for="star2" title="2 stars"></label>
                                <input type="radio" id="star1half" name="rate<?php echo $cnt; ?>" value="1.5" <?php echo ($row_avg_prod_rvw['AVG_Rating'] == '1.5') ? 'checked' : '' ?>/>
                                <label class="half" for="star1half" title="1.5 stars"></label>
                                <input type="radio" id="star1" name="rate<?php echo $cnt; ?>" value="1" <?php echo ($row_avg_prod_rvw['AVG_Rating'] == '1') ? 'checked' : '' ?>/>
                                <label class = "full" for="star1" title="1 star"></label>
                                <input type="radio" id="starhalf" name="rate<?php echo $cnt; ?>" value="0.5" <?php echo ($row_avg_prod_rvw['AVG_Rating'] == '0.5') ? 'checked' : '' ?>/>
                                <label class="half" for="starhalf" title="0.5 stars"></label>
                            </span>

Upvotes: 0

Hameed
Hameed

Reputation: 2277

Have only 4 images, a full star, a half, a quarter and a 3-quarters.

Print the full star as many times as abs($av) and then print another one depending on rounded up/down value of $av - floor($av).

This is how I did for a website, but they did not want only to print the half and wanted 3-quarter and 1-quarter stars as well. But the logic is the same.

Just tested this code on writecode online:

 $av = 3.5;

for ($i = 1; $i <= floor($av); $i++) {
    echo "<img src=http://www.leedshospitalalert.org.uk/images/star_shape.gif>";
}

$av2 = $av - floor($av);

if ($av2 > 0.2 && $av2 < 0.8) {
    echo "<img src=http://icdn.pro/images/en/h/a/half-star-icone-6015-48.png>";
} 
elseif ($av2 > 0.7) 
{
    echo "<img src=http://www.leedshospitalalert.org.uk/images/star_shape.gif>";
}

Upvotes: 1

Just use like this:

$n = is_int($av) ? $av : floor($av) + 0.5;
echo '/images/'.$n.'-star.png';

enter image description here

Cut images per line and name it "1-star.png", "1.5-star.png", "2-star.png", "2.5-star.png", "3-start.png", "3.5-star.png" and so on...

Upvotes: 7

itachi
itachi

Reputation: 6393

using if

if ($av==1){
    echo '/images/1-star.png';
} elseif($av > 1 && < 2){
    echo '/images/1-half-star.png';
} elseif ($av==2){
    echo '.....';
}                    // so far so forth

Just tested. switch in this can give undesirable result.

Upvotes: -1

tawfekov
tawfekov

Reputation: 5122

I guess switch case would be clearest one and easy to customize

switch (true) {
  case  $av == 1 : 
  echo '/images/1-star.png';
  break;

  case  $av > 1 && $av < 2  : 
  echo '/images/1-half-star.png';
  break;

  case  $av == 2 : 
  echo '/images/2-star.png';
  break;

  case  $av > 2 && $av < 3  : 
  echo '/images/2-half-star.png';
  break;

  /***handle any other cases ****/ 
  default:
  echo '/images/0-star.png';
  break;
}

Thanks Dcoder , he pointed me to good idea

Upvotes: 2

Kerrek SB
Kerrek SB

Reputation: 476930

You could compute the following quantity:

$i = 2 * floor($av - 1) + (floor($av) == $av ? 0 : 1);

This will produce numbers 0, 1, 2, ... for your values, which you can then use to index into an array of filenames.

Upvotes: 0

Related Questions