Show A 0-5 Rate As Stars

Show A 0-5 Rate As Stars
Show a 0 to 5 floating point rating in graphic stars using a PHP function. You can specify which direction to align the graphic stars (left or right)
// Example: ShowInStars(4.3, "left");
function ShowInStars($Rate, $StarImgAlign = "left")
{
        // Remember to change the paths to the star graphics or place them in the Pictures folder
        // You will need three star graphics: one that is off (gray), one that is on (gold) and one that is half on, half off
        $StarOff = "<img src=\"Pictures/StarOff.gif\" align=\"left\" style=\"margin-right: 3px;\" alt=\"-\" />";
        $StarOn = "<img src=\"Pictures/StarOn.gif\" align=\"left\" style=\"margin-right: 3px;\" alt=\"*\" />";
        $StarHalf = "<img src=\"Pictures/StarHalf.gif\" align=\"left\" style=\"margin-right: 3px;\" alt=\"*\" />";
        $TutRate = $Rate;
        $TutRateInt = $TutRate * 10;
        $ModRate = $TutRateInt % 10;
        $TutRateIntFull = $TutRateInt - $ModRate;
        $TutRateIntFull /= 10;
        $TutRateIntFull = round($TutRateIntFull, 0);
        $CountGrayStars = 0;
        $StarsHtml = "";
        if ($Rate > 0)
        {
                if ($ModRate > 5)
                {
                        $CountGrayStars--;
                }
                // If there modulus results in more than half a point paint half a star
                if ($StarImgAlign == "left")
                {
                        $CountGrayStars += 5 - $TutRateIntFull;
                        // Loop to show full stars
                        for ($i = 0; $i < $TutRateIntFull; $i++)
                        {
                                $StarsHtml .= $StarOn;
                        }
                        // Show the full star
                        if ($ModRate > 5)
                        {
                                $StarsHtml .= $StarOn;
                        }
                        else if ($ModRate > 0)
                        {
                                $StarsHtml .= $StarHalf;
                                $CountGrayStars--;
                        }
                        // Loop to show gray stars
                        for ($i = 0; $i < $CountGrayStars; $i++)
                        {
                                $StarsHtml .= $StarOff;
                        }
                }
        }
        else
        {
                $StarsHtml .= $StarOff . $StarOff . $StarOff . $StarOff . $StarOff;
        }
        return $StarsHtml;
}
Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top