three star rating system

rating system

by rajutikale

HTML

<div class="star-box">
   <li>
       <span class="stars"><?php echo $position;?></span>
  </li>
</div>

<?php 
if( !isset($_SESSION['ranking']) || $_SESSION['ranking']['points'] <= 1000 ) {
    if( !isset($_SESSION['ranking']['points']) || $_SESSION['ranking']['points'] == '' ) {
        $position = '0px';
        $position = 0.00;
    } else {
        $position = ($_SESSION['ranking']['points'] * 0.14).'px';
        $position = 0.75;
    }

    $str1 = 'class="active" ';
    $str1 .= ' style="background-position:'.$position.' 9px;"';

}

if( $_SESSION['ranking']['points'] > 1000 &&  $_SESSION['ranking']['points'] <= 5000 ) {
    if( !isset($_SESSION['ranking']['points']) || $_SESSION['ranking']['points'] == '' ) {
        $position = '0px';
        $position = 0.00;
    } else {
        $position = ($_SESSION['ranking']['points']*0.035).'px';
        $position = 1.40;
    }
}

?>

CSS

span.stars, span.stars span {
    display: block;
    background: url(../../images/stars.png) 0 -16px repeat-x;
    width: 50px;
    height: 20px;
    text-indent: -9999px;
}

span.stars span {
    background-position: 0 0;
}

JavaScript

$(function() {
    $('span.stars').stars();
});

$.fn.stars = function() {
    return $(this).each(function() {
        // get the value
        var val = parseFloat($(this).html());

        // make sure that the value is in (0 - 5) range, multiply to get width
        val = Math.round(val * 4) / 4;
        var size = Math.max(0, (Math.min(5, val))) * 16;

        // create stars holder
        var $span = $('<span />').width(size);

        // replace the numerical value with stars
        $(this).html($span);
    });
}