The "Freeola Customer Forum" forum, which includes Retro Game Reviews, has been archived and is now read-only. You cannot post here or create a new thread or review on this forum.
If you dont know what i mean this site uses what i am talking about:
http://www.lemonzoo.com
thanks for any help!
AJ
If you dont know what i mean this site uses what i am talking about:
http://www.lemonzoo.com
thanks for any help!
AJ
Pic ID | averageRating | numberofRatings | TotalRating
PicID being the primary key.
All of them being ints.
Then have php code to insert someones rating to the database.
This would take the rating they enter and add it to the total rating and also increase numberofRatings by 1. Then use these to work out the new averageRating.
Shouldn't be too hard. Not really sure on tutorials though.
Then to display the average, you just echo $average, and to calculate the new average when someone else rates, you would do:
$average = (($average * $total) + $newrating) / ($total + 1);
$total = $total++;
Obviously you might want to neaten that up or whatever, but it'd work, and would only need 2 columns in a database, and to simply display the average rating would require no processing.
Or even have two columns $totalrating and $totalusers
$average = $totalrating / $totalusers;
And when someone rates, just do:
$totalrating += $newrating;
$totalusers++;
Again all help is very much appreciated!
AJ
And i have a general enough understanding to be able to integrate php code into my site as my site currently uses php.
So can anyone be kind enough to show me some code if they happen to have something that i could adapt to my situation?
Thanks for any help
AJ
Could you post the name of your table/database, and the exact field names of the table, then I can get it exact for you.
Your best bet is to have a table with 3 fields:
- one for a picture ID reference (unique, auto_increment, unsigned int)
- one for the average rating (unsigned int)
- one for the number of people who have rated (unsigned int)
e.g. id | rating | raters
Server: localhost Database: alexj17_picrating Table: pictures
id int(11) UNSIGNED auto_increment Primary
rating int(11) UNSIGNED
raters int(11) UNSIGNED
Thanks for this by the way
function average_rating($id) {
$conn = mysql_connect("localhost", "
mysql_select_db("alexj17_picrating");
$result = mysql_query("SELECT rating FROM pictures WHERE id = '".mysql_real_escape_string($id)."';");
if (mysql_num_rows($result) != 0) {
while ($row = mysql_fetch_assoc($result)) {
$rating = number_format(($row['rating']) / ($row['raters']),1);
}
}
else {
$rating = "Not rated"; //customise this as you desire
}
return $rating;
}
?>
Put the above PHP function code at the top of any page you want to display the average rating. Each time you want to display the average rating on that page, put the following code:
");?>
To add a new rating, I shall assume that you have a form to submit the picture ID and the rating, so put this code on the page you are submitting it to. Also, I shall assume the fields are called "id" and "rating" accordingly.
if(!is_num($_POST['rating'])) {
$conn = mysql_connect("localhost", "
mysql_select_db("alexj17_picrating");
$result = mysql_query("SELECT rating FROM pictures WHERE id = '".mysql_real_escape_string($id)."';");
if (mysql_num_rows($result) == 0) {
mysql_query("INSERT INTO pictures VALUES ('" . mysql_real_escape_string($_POST['id']) . "', '" . $_POST['rating'] . "', '1');");
}
else {
mysql_query("UPDATE pictures SET rating=rating+".$_POST['rating'].", raters=raters+1 WHERE id='" . mysql_real_escape_string($_POST['id']) . "';");
}
}
?>
I have just typed that into this box, so there may be some typos/mistakes in there - I haven't had a chance to check it either.
> To add a new rating, I shall assume that you have a form to submit
> the picture ID and the rating, so put this code on the page you are
> submitting it to. Also, I shall assume the fields are called
> "id" and "rating" accordingly.
So my form to do rating needs to assign figures for $rating and $id?
And what do you mean the page i am submitting it to? is that the same page the user clicks on to rate the picture?
The rest i understand and is looking good! thanks