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;
}