The full class definitino goes as below:
PHP Code:
<?php
class Rating
{
## PRIVATE VARIABLES
## END PRIVATE VARIABLES
## PUBLIC METHODS
// Output the Rating information
// Returns a string of HTML
public static function OutputRating($varItem)
{
// Verify $varItem was provided
if ($varItem != null && strlen(trim($varItem)) != 0)
{
// Check if Magic QUotes is ON
if (!get_magic_quotes_gpc())
{
$varItem = addslashes($varItem);
}
// Information for the Output
$averageStars = Rating::CalculateAverageRating($varItem);
// Check to see that the user has not already rated this item
if (Rating::CheckRatingsByIp($varItem) == 0)
{
$classes = "rating" . Rating::ShowStars($averageStars);
// Write Output HTML for the Rating Data
$output = "\r\n";
$output .= "<ul class=\"{$classes}\" id=\"{$varItem}\">\r\n";
$output .= " <li class=\"one\"><a href=\"javascript:RateItem('{$varItem}', 1);\" title=\"1 Star\">1</a></li>\r\n";
$output .= " <li class=\"two\"><a href=\"javascript:RateItem('{$varItem}', 2);\" title=\"2 Stars\">2</a></li>\r\n";
$output .= " <li class=\"three\"><a href=\"javascript:RateItem('{$varItem}', 3);\" title=\"3 Stars\">3</a></li>\r\n";
$output .= " <li class=\"four\"><a href=\"javascript:RateItem('{$varItem}', 4);\" title=\"4 Stars\">4</a></li>\r\n";
$output .= " <li class=\"five\"><a href=\"javascript:RateItem('{$varItem}', 5);\" title=\"5 Stars\">5</a></li>\r\n";
$output .= "</ul>\r\n";
}
else
{
$classes = "rated " . Rating::ShowStars($averageStars);
// Write Output HTML for the Rating Data
$output = "\r\n";
$output .= "<ul class=\"{$classes}\" id=\"{$varItem}\">\r\n";
$output .= " <li class=\"one\">1</li>\r\n";
$output .= " <li class=\"two\">2</li>\r\n";
$output .= " <li class=\"three\">3</li>\r\n";
$output .= " <li class=\"four\">4</li>\r\n";
$output .= " <li class=\"five\">5</li>\r\n";
$output .= " <li class=\"total\">[78]</li>";
$output .= "</ul>\r\n";
}
}
else
{
$output = "";
// This is a major issue. NO information can be retrieve if an item name is not passed.
Error::LogError("Variable Missing", "You must provide the item name for this function to find the average.");
}
return $output;
}
// Rate an Item
// Returns the name/value pair of new class names and the item name
public static function RateItem($varItem, $varRating, $varClasses)
{
$newClassNames = $varClasses;
// Verify $varName was provided
if ($varItem != null && strlen(trim($varItem)) != 0
&& $varRating != null && strlen(trim($varRating)) != 0 && is_numeric($varRating)
&& $varClasses != null && strlen(trim($varClasses)) != 0)
{
// Check if Magic Quotes is ON
if (!get_magic_quotes_gpc())
{
$varItem = addslashes($varItem);
}
// Check to see that the user has not already rated this item
if (Rating::CheckRatingsByIp($varItem) == 0)
{
$ipAddress = $_SERVER['REMOTE_ADDR'];
Database::ExecuteQuery("INSERT INTO `rating` (`item_name`, `rating`, `ip_address`, `date_rated`) VALUES ('{$varItem}', {$varRating}, '{$ipAddress}', NOW())", "InsertRating");
Database::FetchResults("InsertRating");
Database::FreeResults("InsertRating");
Database::RemoveSavedResults("InsertRating");
// Information for the Output
$averageStars = Rating::CalculateAverageRating($varItem);
$newClassNames = "rated " . Rating::ShowStars($averageStars);
}
}
else
{
// This is a major issue. NOT enough information was sent to log the item
Error::LogError("Variable(s) Missing", "You must provide all of the information to log the rating of this item.");
}
// Build Name/Value Pair to return
$nameValue = "classes={$newClassNames}&item={$varItem}";
return $nameValue;
}
## END PUBLIC METHODS
## PRIVATE METHODS
// Calculate Average Rating
// Returns the number of stars to show
private static function CalculateAverageRating($varItem)
{
$averageStars = 0;
// Query Average Rating for a specific Item
Database::ExecuteQuery("SELECT AVG(`rating`) AS `averageRating` FROM `rating` WHERE `item_name`='{$varItem}'", "AverageRating");
$results = Database::FetchResults("AverageRating");
Database::FreeResults("AverageRating");
Database::RemoveSavedResults("AverageRating");
// Round the Average into a Whole Number
if (sizeof($results) == 1)
{
if ($results[0]['averageRating'] != null)
{
$averageStars = round($results[0]["averageRating"], 0);
}
}
else
{
// This is simply a warning, as it isn't vital if no results were found, as the item may be new.
Error::LogWarning("Rating Data Missing", "No entries were found for '{$varName}', this might be the first entry.");
}
return $averageStars;
}
// Show Stars
// Returns the class information for the number of stars to show
private static function ShowSars($varStars)
{
$aStars = array(
1 => 'onestar',
2 => 'twostar',
3 => 'threestar',
4 => 'fourstar',
5 => 'fivestar'
);
return (true === array_key_exists((integer)$varStars, $aStars)) ? $aStars[(integer)$varStars] : 'nostar' ;
}
// Check Ratings By IP Address
// Returns the number of ratings for an item by an ip address
private static function CheckRatingsByIp($varItem)
{
$ipAddress = $_SERVER['REMOTE_ADDR'];
Database::ExecuteQuery("SELECT COUNT(*) AS `totalRatings` FROM `rating` WHERE `item_name`='{$varItem}' AND `ip_address`='{$ipAddress}'", "AlreadyRated");
$results = Database::FetchResults("AlreadyRated");
Database::FreeResults("AlreadyRated");
Database::RemoveSavedResults("AlreadyRated");
// Check to see that the user has not already rated this item
if ($results != null && $results[0]['totalRatings'] != null)
{
return $results[0]['totalRatings'];
}
return 0;
}
## END PRIVATE METHODS
}
?> \
PHP Code:
<?php
$ratingData = Rating::OutputRating('demo');
if (Error::HasErrors())
{
echo Error::ShowErrorMessages();
Error::ClearErrors();
}
else
{
echo $ratingData;
}
?>
The second bit of code above enclosed in php tags is the output rating of some determined article. Now if you can see the word demo is the item name now I am planning to place a sql query inside where it chooses the item name coming from the url string. is that possible to place a sql query as a parameter?
PHP Code:
Database::ExecuteQuery("SELECT item_name AS 'item' FROM `rating` WHERE `item_name`='{$varItem}'", "item");
$results = Database::FetchResults("item");
Database::FreeResults("item");
Database::RemoveSavedResults("item);
Bookmarks