Geekpedia Programming Tutorials






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)

On Thursday, April 24th 2008 at 08:31 PM
By Andrei Pociu (View Profile)
****-   (Rated 3.8 with 4 votes)
Contextual Ads
More PHP Resources
Advertisement
  1. // Example: ShowInStars(4.3, "left");
  2. function ShowInStars($Rate, $StarImgAlign = "left")
  3. {
  4.         // Remember to change the paths to the star graphics or place them in the Pictures folder
  5.         // You will need three star graphics: one that is off (gray), one that is on (gold) and one that is half on, half off
  6.         $StarOff = "<img src=\"Pictures/StarOff.gif\" align=\"left\" style=\"margin-right: 3px;\" alt=\"-\" />";
  7.         $StarOn = "<img src=\"Pictures/StarOn.gif\" align=\"left\" style=\"margin-right: 3px;\" alt=\"*\" />";
  8.         $StarHalf = "<img src=\"Pictures/StarHalf.gif\" align=\"left\" style=\"margin-right: 3px;\" alt=\"*\" />";
  9.         $TutRate = $Rate;
  10.         $TutRateInt = $TutRate * 10;
  11.         $ModRate = $TutRateInt % 10;
  12.         $TutRateIntFull = $TutRateInt - $ModRate;
  13.         $TutRateIntFull /= 10;
  14.         $TutRateIntFull = round($TutRateIntFull, 0);
  15.         $CountGrayStars = 0;
  16.         $StarsHtml = "";
  17.         if ($Rate > 0)
  18.         {
  19.                 if ($ModRate > 5)
  20.                 {
  21.                         $CountGrayStars--;
  22.                 }
  23.                 // If there modulus results in more than half a point paint half a star
  24.                 if ($StarImgAlign == "left")
  25.                 {
  26.                         $CountGrayStars += 5 - $TutRateIntFull;
  27.                         // Loop to show full stars
  28.                         for ($i = 0; $i < $TutRateIntFull; $i++)
  29.                         {
  30.                                 $StarsHtml .= $StarOn;
  31.                         }
  32.                         // Show the full star
  33.                         if ($ModRate > 5)
  34.                         {
  35.                                 $StarsHtml .= $StarOn;
  36.                         }
  37.                         else if ($ModRate > 0)
  38.                         {
  39.                                 $StarsHtml .= $StarHalf;
  40.                                 $CountGrayStars--;
  41.                         }
  42.                         // Loop to show gray stars
  43.                         for ($i = 0; $i < $CountGrayStars; $i++)
  44.                         {
  45.                                 $StarsHtml .= $StarOff;
  46.                         }
  47.                 }
  48.         }
  49.         else
  50.         {
  51.                 $StarsHtml .= $StarOff . $StarOff . $StarOff . $StarOff . $StarOff;
  52.         }
  53.         return $StarsHtml;
  54. }
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this code snippet
Comment Current Comments
by Baly on Monday, April 28th 2008 at 12:37 PM

Hi all

question: How can I make it to store and read the rated points in database?

by osQ on Wednesday, June 18th 2008 at 09:17 AM

Balu @ Create database where you insert all values.
Then you create php file which reads the database and your done.

I have system like this and it is working.
Sorry but i wont give you


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs PHP Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons