Forum Moderators: coopster

Message Too Old, No Replies

Rate the article

         

kkonline

8:05 am on Aug 28, 2007 (gmt 0)

10+ Year Member



I am making an article manager. I have counted the page views by updating table with views=views+1;

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?

Habtom

8:11 am on Aug 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

That should be it.

kkonline

8:23 am on Aug 28, 2007 (gmt 0)

10+ Year Member



And how do i prevent the same person from RATING THE SAME ARTICLE AGAIN AND AGAIN. Ip Logging and then checking and displaying you already voted this Article.

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?

Habtom

12:30 pm on Aug 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yea, but with the article rating, if you want to prevent duplicate votings and your visitors don't login to do that, you can protect it by collecting thier IPs, and reject the ones who already did vote.

whoisgregg

1:35 pm on Aug 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You really need to store the ratings in their own table. First, so you can redisplay to the user what they have previously rated an item. Second, so you can do good fraud protection.

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. :)

kkonline

4:45 pm on Aug 28, 2007 (gmt 0)

10+ Year Member



i created a table Rating and added the following data for testing. What changes do i make to pages.php to make it work. Currently no changes are made to db (to the data i have entered manually). What do i do to rate the articles.


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]