GetDotted Domains

Viewing Thread:
"Picture rating system."

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.

Mon 17/01/05 at 22:28
Regular
"www.funrunner.co.uk"
Posts: 289
Just wondering how i would go about creating a picture rating system as found on many other sites? Any tutorials you people would recomend? How many MySQL tables will it need? Any information regarding this would be very helpful

If you dont know what i mean this site uses what i am talking about:

http://www.lemonzoo.com

thanks for any help!
AJ
Mon 17/01/05 at 22:28
Regular
"www.funrunner.co.uk"
Posts: 289
Just wondering how i would go about creating a picture rating system as found on many other sites? Any tutorials you people would recomend? How many MySQL tables will it need? Any information regarding this would be very helpful

If you dont know what i mean this site uses what i am talking about:

http://www.lemonzoo.com

thanks for any help!
AJ
Mon 17/01/05 at 22:36
Regular
"Picking a winner!"
Posts: 8,502
You'd probably get away with just the one table with something like

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.
Mon 17/01/05 at 23:39
Regular
"NULL"
Posts: 1,384
You could even do it just knowing the number of people who have rated, and the average, for example, $average and $total

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++;
Tue 18/01/05 at 20:18
Regular
"www.funrunner.co.uk"
Posts: 289
ah perhaps i should have mentioned my beginner status in php and mysql! i have used both before but this has generally been with other peoples code. Does anyone know any tutorials or help files which have been made to help in this particular task because i dont want to learn all of php or mysql just this task!!

Again all help is very much appreciated!
AJ
Tue 18/01/05 at 21:06
Regular
"NULL"
Posts: 1,384
Although this isn't a complex problem, it would definitely be of use to you to have a look at the basics of PHP and MySQL first. Even if someone here was to give you all the code you need to do it, it would still need to be integrated into your site, and you would need to set up the MySQL database and things yourself. This would require some knowledge at least of PHP/MySQL.
Tue 18/01/05 at 22:00
Regular
"www.funrunner.co.uk"
Posts: 289
I know the general basics of both and i have set up a mySQL table with pic ID| total rating| number of ratings| average rating as the fields.

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
Wed 19/01/05 at 00:32
Regular
"NULL"
Posts: 1,384
OK, I'll try and mock some code up for you tomorrow (well today actually, but I'm going to bed in a few mins).

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
Wed 19/01/05 at 10:49
Regular
"www.funrunner.co.uk"
Posts: 289
ok so here's all my database stuff

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
Wed 19/01/05 at 13:01
Regular
"NULL"
Posts: 1,384
OK, I'll give you two sets of code, one to add a new rating, and one to print the average rating. I'll start with the code to print the average:


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.
Wed 19/01/05 at 13:57
Regular
"www.funrunner.co.uk"
Posts: 289
Nimco wrote:
> 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

Freeola & GetDotted are rated 5 Stars

Check out some of our customer reviews below:

Many thanks!!
Registered my website with Freeola Sites on Tuesday. Now have full and comprehensive Google coverage for my site. Great stuff!!
John Shepherd
Simple, yet effective...
This is perfect, so simple yet effective, couldnt believe that I could build a web site, have alrealdy recommended you to friends. Brilliant.
Con

View More Reviews

Need some help? Give us a call on 01376 55 60 60

Go to Support Centre
Feedback Close Feedback

It appears you are using an old browser, as such, some parts of the Freeola and Getdotted site will not work as intended. Using the latest version of your browser, or another browser such as Google Chrome, Mozilla Firefox, or Opera will provide a better, safer browsing experience for you.