Forum Moderators: coopster
Next I want to make a rating system (like the standard ones you have on news/articles site). Something like when users mouse over the 5 blank stars they get colored and on mouse press the rating is entered. How do i display the stars according to the rating if rating is 3 then 3 stars and so on...
So for that, should i have a separate count of number of votes and then use simple average or what?
votes=3
sum of all the ratings (for example 4+3+4=11)
so the current rating would be 11/3; then make it perfect integer by type cating
Or is there any better solution?
secondly, it would be similar to views
update vote=vote+1;
and then rating=rating+$rating (existing in db + the user rating)
Is that correct?
You can still maintain a running score in the main table to avoid a costly database call to calculate scores on each pageload, but you'll still need that data to prevent fraud. Track IP, user agent, session ID, timestamp, and rating for each vote and you'll be good. :)
CREATE TABLE `ratings` (
`RateID` int(11) NOT NULL auto_increment,
`ElementID` varchar(20) NOT NULL default '',
`Rating` int(11) NOT NULL default '0',
`IP` varchar(80) NOT NULL default '',
PRIMARY KEY (`RateID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;--
-- Dumping data for table `ratings`
--
INSERT INTO `ratings` VALUES (1, 'wow3', 3, '');
The code i posted above so when i write http://localhost/pages.php?page=3 no change is there in the db?
<?php
//$lastPage = ceil($numRows/$perPage);
// Database Connection
include("$_SERVER[DOCUMENT_ROOT]/includes/connect.inc.php");
include("$_SERVER[DOCUMENT_ROOT]/includes/stripgpcslash.inc.php");// If current page number, use it
// if not, set one!
if(!isset($_GET['page']))
{
$page = 1;
}
else
{
if(ctype_digit($_GET['page']))
{
$page=trim(mysql_real_escape_string($_GET['page']));
}
else
{
echo "invalid query";
exit;
}
}
// Define the number of results per page
$max_results = 1;
// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);
// Perform MySQL query on only the current page number's results
$countviews = mysql_query("UPDATE wow SET views=views+1 WHERE id=$page");
$sql = mysql_query("SELECT * FROM wow WHERE `trusted` = 0 ORDER BY `id` ASC LIMIT $from, $max_results");
while($row = mysql_fetch_array($sql)){
echo $row['content']."</p><br />";
}
function getRatings(&$averageRating, &$totalRatings, &$rateArray, $type, $articleID){
$ratingquery = mysql_query("SELECT `Rating` FROM `Ratings` WHERE `ElementID` = '".$type.$articleid."'");
while($row = mysql_num_rows($ratingquery)){
$rateArray[] = $row['Rating'];
}
$totalRatings = count($rateArray);
if(!$totalRatings){
$averageRating = array_sum($rateArray)/$totalRatings;
}else{
$averageRating = 0;
}
return;
}
$totalRatings = 0;
$averageRating = 0;
$elementType = "wow";
$articleId = $page;
$rateArray = array();
getRatings($averageRating, $totalRatings, $rateArray, $elementType, $articleId);
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM wow"),0);
$total_pages = ceil($total_results / $max_results);
echo "<center>Select a Page<br />";
if($page > 1){
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\">< Prev </a> ";
}
echo $page;
echo " of ";
echo $total_pages;
// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\"> Next ></a>";
}
echo "</center>";
mysql_close($con);
?>
[edited by: eelixduppy at 5:59 pm (utc) on Aug. 28, 2007]
[edit reason] delinked [/edit]