Product Rating Script Using PHP
Creating a function to display a product ratings is trivial using PHP.
All you need is to decide on the values for you ratings and a the image to display for each rating.
For this Example we will use a system that stores ratings as descrete values and not continous values. Lets say we have a rating system that is out of 5. The descrete values taken by a rating are 0, 1 , 1.5, 2, 2.5, 3, 3.5 , 4 , 4.5, 5
The script will require 10 images to represent each rating.
All that we have to do is create a function that takes the rating and decides on the corresponding image for the rating.
A typical function would be as follows:
<?php
function getRatingImage($rating)
{
if ($rating <= 0 ){$rating_image = “./zero.jpg”;}
if ($rating >= 0.5){$rating_image= “./half.jpg”;}
if ($rating >= 1 ){$rating_image= “./one.jpg”;}
if ($rating >= 1.5){$rating_image= “./one_half.jpg”;}
if ($rating >= 2 ){$rating_image= “./two.jpg”;}
if ($rating >= 2.5){$rating_image= “./two_half.jpg”;}
if ($rating >= 3 ){$rating_image= “./three.jpg”;}
if ($rating >= 3.5){$rating_image= “./three_half.jpg”;}
if ($rating >= 4 ){$rating_image= “./four.jpg”;}
if ($rating >= 4.5){$rating_image= “./four_half.jpg”;}
if ($rating >= 5 ){$rating_image= “./five.jpg”;}
return $rater_stars;
}
?>
To use the code simply pass the rating to the above function as retrieved from your database.
- Copying Records From One Table to Another Using SQL Queries
- Google: Ethics vs Freedom of Information
- Abstract Art(Sort of!!)
- Editing Permalink Text
- PPC(Pay Per Click) vs PPA(Pay Per Action)
- Huge Alexa Rank Gain
- Most Popular Websites in South Africa
- WTF is it with Conime.exe ??
- Using Paypal in South Africa
- How Much is Your Website Worth ?