/ Published in: PHP
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
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
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function display_skill_level ( $number ) { // Convert any entered number into a float // Because the rating can be a decimal e.g. 4.5 // Get the integer part of the number // Get the fraction part $fraction = $number - $intpart; // Rating is out of 5 // Get how many stars should be left blank // 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" />'; } }