Return to Snippet

Revision: 39081
at January 13, 2011 01:30 by bwangila


Initial Code
function display_skill_level ( $number ) {
    // Convert any entered number into a float
    // Because the rating can be a decimal e.g. 4.5
    $number = number_format ( $number, 1 );

    // Get the integer part of the number
    $intpart = floor ( $number );

    // Get the fraction part
    $fraction = $number - $intpart;

    // Rating is out of 5
    // Get how many stars should be left blank
    $unrated = 5 - ceil ( $number );

    // Populate the full-rated stars
    if ( $intpart <= 5 ) {
        for ( $i=0; $i<$intpart; $i++ )
	    echo '<img src="/images/star-rating-full.png" />';
    }
    
    // Populate the half-rated star, if any
    if ( $fraction == 0.5 ) {
        echo '<img src="/images/star-rating-half.png" />';
    }
    
    // Populate the unrated stars, if any
    if ( $unrated > 0 ) {
        for ( $j=0; $j<$unrated; $j++ )
	    echo '<img src="/images/star-rating-none.png" />';
    }
}

Initial URL

                                

Initial Description
I recently wanted to use stars to show my skill levels in various aspects of web design in my portfolio e.g, PHP, MySQL etc; similar to a rating system. It would be repetitive to have to copy the HTML image code over and over or to design various ratings in photoshop.

So I came up with this function, all you need are three images: a full rating star, a half rating star and an grayed out unrated star

Initial Title
PHP Star Rating function

Initial Tags

                                

Initial Language
PHP