I need to build a script that counts the number of review by star. for instance if a user entered a rating of 3 out five star to an item, then I want to put or placed his/her review into a field where all users that has rated the item 3 in other words to group users by rating. It is a five star rating and each user will placed according to his rating. The illustration above you can see the numbers inside the brackets which shows the number of times one, two, three star etc... has been choosen by users now I am trying to build a script that send each rating to it's category or according to the number of stars the user choose The URL link below has an illustration of how does the rating system will look. http://www.pmob.co.uk/temp/star-rating3.htm
Just have a table named ratings, with 7 fields id, articleid, 1s, 2s, 3s, 4s, 5s id is an auto_increment primary key, articleid is the id for the page the ratings is on and should link to your articles table, and obivously 1s, 2s etc.. stores the number of times it has been voted.
<?php $ratingData = Rating::OutputRating('demo'); if (Error::HasErrors()) { echo Error::ShowErrorMessages(); Error::ClearErrors(); } else { echo $ratingData; } ?> PHP: The code above is the output rating of some determined article. Now if you can see the word demo is the itemname 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? Database::ExecuteQuery("SELECT item_name AS 'item' FROM `rating` WHERE `item_name`='{$varItem}'", "item"); $results = Database::FetchResults("item"); Database::FreeResults("item"); Database::RemoveSavedResults("item); PHP: help please.
<?php $ratingData = Rating::OutputRating('demo'); if (Error::HasErrors()) { echo Error::ShowErrorMessages(); Error::ClearErrors(); } else { echo $ratingData; } ?> PHP: The code above will output a row of five stars. It will calculate the average of all the rating from different users. After a lot of analysis i have realized that the word 'demo' inside the parameters of the OutputRating method showed above is the name of the item being rated. Now that word is written by hand and i would like that instead of being written by hand it dynamically get pulled from a database by some kind of sql injection which will used some variable on the URL of that page being passed from another page. That's why that in previews posted I was mentioning about how could I possible put an sql injection inside that parameter. Well the code above which output a row of five stars and will automatically show 1,2,3,4 or 5 stars depending on the averaged calculated inside another method called "CalculateAverageRating" embed below. 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; } PHP: Now as I said the first script of the two listed above will output an row of stars. But in order to ouput that row of stars, calcualte its average, pull the information of each item from a database, it uses a lot of methods and functions behind it which is the hard part of it, and that's when it gets complicated. It uses a database scheme such as: If you need more information about building this script so soggesstion could be made please visit... http://www.search-this.com/2007/06/04/css-the-star-matrix-pre-loaded-part-2/ The final look and functionality will look like the picture below Now that I have the look we can build a rating system from scratch or we can modify the one By Cdradio at the link below http://www.search-this.com/2007/06/04/css-the-star-matrix-pre-loaded-part-2/ Please help or any suggestions on how to star about it.
You can use a name pulled from your table in there if you like, but I don't see any advantages of doing this? The way they have set it up seems fine how it is, just make sure you use a unique name (the name of the item the ratings is on) for each item, which as i said can be pulled from the db if you like.
exactly That I want to know how can I put the name of the item in the parameter () I need a sql injection to insert the value coming from the url. <?php $shoename = $_GET['shoename'] $id = (int)$_GET['id']; if( $id === 0 ) { exit('ID can only be an integer'); } ?> PHP: the shoename has a value coming from the url and I want to place inside the OutputRating method parameter. <?php $ratingData = Rating::OutputRating('demo'); if (Error::HasErrors()) { echo Error::ShowErrorMessages(); Error::ClearErrors(); } else { echo $ratingData; } ?> PHP: I other words instead of "demo" put an sql injection that extract the value of the $shoename variable coming from the url and inserted in the parameter.
It does work.. Thank you... Facing another problem though now... 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 .= "</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; } PHP: The ul below just show the number with bullets without the format of the class rating. 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"; } PHP: The css style sheet is below .rating{ width:80px; height:16px; margin:0 0 20px 0; padding:0; list-style:none; clear:both; position:relative; background: url(../images/star-matrix.gif) no-repeat 0 0; } .rating li.total { background:none; top:0; right:-40px; /*left:90px; /*if you want brackets aligned left*/ position:absolute; fext-indent:0; font-size::93%; } ul.rating li { cursor: pointer; /*ie5 mac doesn't like it if the list is floated\*/ float:left; /* end hide*/ text-indent:-999em; } ul.rating li a { position:absolute; left:0; top:0; width:16px; height:16px; text-decoration:none; z-index: 200; } ul.rating li.one a {left:0} ul.rating li.two a {left:16px;} ul.rating li.three a {left:32px;} ul.rating li.four a {left:48px;} ul.rating li.five a {left:64px;} ul.rating li a:hover { z-index:2; width:80px; height:16px; overflow:hidden; left:0; background: url(../images/star-matrix.gif) no-repeat 0 0 } ul.rating li.one a:hover {background-position:0 -96px;} ul.rating li.two a:hover {background-position:0 -112px;} ul.rating li.three a:hover {background-position:0 -128px} ul.rating li.four a:hover {background-position:0 -144px} ul.rating li.five a:hover {background-position:0 -160px} /* this is used to remove the hover affect */ /* use the background position according to the table above to display the required images*/ .rated{ width:80px; height:16px; margin:0 0 3px 0; padding:0; list-style:none; clear:both; position:relative; background: url(../images/star-matrix.gif) no-repeat 0 0; } ul.rated li { cursor: pointer; /*ie5 mac doesn't like it if the list is floated\*/ float:left; /* end hide*/ text-indent:-999em; } ul.rated li.one a {left:0} ul.rated li.two a {left:16px;} ul.rated li.three a {left:32px;} ul.rated li.four a {left:48px;} ul.rated li.five a {left:64px;} /* add these classes to the ul to effect the change to the correct number of stars */ .nostar {background-position:0 0} .onestar {background-position:0 -16px} .twostar {background-position:0 -32px} .threestar {background-position:0 -48px} .fourstar {background-position:0 -64px} .fivestar {background-position:0 -80px} /* end rating code */ h3{margin:0 0 2px 0;font-size:110%} after the if statement in the OutputRating method there is an else statement which will execute if Rating::CheckRatingsByIp is not equal to 0 else it excute the "rated" class in which works fine, it format the ul tags in the else statement but in the if statement it doesn't format which is the default appearance before the user rate the item. The default appearance is 'nostar' waiting to be rated. There is another method call ShowStars which both statements in the OutputRating method uses the if and else statemtn. ShowStars method: private static function ShowStars($varStars) { $classes = ''; // Select the Number of Stars Class switch ($varStars) { case 1: $classes .= "onestar"; break; case 2: $classes .= "twostar"; break; case 3: $classes .= "threestar"; break; case 4: $classes .= "fourstar"; break; case 5: $classes .= "fivestar"; break; default: $classes .= "nostar"; break; } return $classes; } PHP: Help please.