Page 1 of 1

Rating system error?

Posted: Thu Apr 23, 2009 11:23 am
by tomsace
Hi,
I have setup a simple rating system on my website.
When I make up a number and add it into my database for example 23 total votes, total rating of 10 it shows the average rating as 2.3. But the problem is when I try to click a rating of 1-5 I receive the error:
Warning: Cannot modify header information - headers already sent by (output started at /home/tomsace/public_html/header.php:5) in /home/tomsace/public_html/game.php on line 39
Your vote has been cast
This is my script:

Code: Select all

<?php
//We only run this code if the user has just clicked a voting link
if ( $mode=="vote")
{
//If the user has already voted on the particular thing, we do not allow them to vote again $cookie = "Mysite$id";
if(isset($_COOKIE[$cookie]))
{
Echo "Sorry You have already ranked that site <p>";
}
//Otherwise, we set a cooking telling us they have now voted
else
{
$month = 2592000 + time();
setcookie(ZippyGame.$id, Voted, $month);
//Then we update the voting information by adding 1 to the total votes and adding their vote (1,2,3,etc) to the total rating
mysql_query ("UPDATE vote SET total = total+$voted, votes = votes+1 WHERE id = $id");
Echo "Your vote has been cast <p>";
}
} 
$id=mysql_real_escape_string($_GET['id']);
//Puts SQL Data into an array
$data = mysql_query("SELECT * FROM games WHERE id = '$id'") or die(mysql_error());
//Now we loop through all the data
while($ratings = mysql_fetch_array( $data ))
{
//This calculates the sites ranking and then outputs it - rounded to 1 decimal
$current = $ratings[total] / $ratings[votes];
Echo round($current, 1) . "&nbsp;";
Echo "(" . $ratings[votes] . " votes)<br>";
//This creates 5 links to vote a 1, 2, 3, 4, or 5 rating for each particular item
Echo "Vote: ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=1&id=".$ratings[id].">1 </a>";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=2&id=".$ratings[id].">2 </a>";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=3&id=".$ratings[id].">3 </a>";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=4&id=".$ratings[id].">4 </a>";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=5&id=".$ratings[id].">5 </a>";
}
?>
My database stores a unique ID for each item been rated (ID), total votes (TOTAL) and total rating (VOTES).

Re: Rating system error?

Posted: Fri Apr 24, 2009 3:29 am
by MasterBeta
What's on line 39 of game.php?

Re: Rating system error?

Posted: Fri Apr 24, 2009 1:16 pm
by tomsace
Oh yeah sorry, line 39 is:

Code: Select all

setcookie(ZippyGame.$id, Voted, $month);

Re: Rating system error?

Posted: Fri Apr 24, 2009 7:42 pm
by MasterBeta
The setcookie() function must be sent in the header, before the <html> tag.