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
Thu 20/01/05 at 15:45
Regular
"Picking a winner!"
Posts: 8,502
Check to see that it is returning the right value from your table.

ie where you have the code
if (mysql_num_rows($result) != 0) {
while ($row = mysql_fetch_assoc($result)) {
$rating = number_format(($row['rating']) / ($row['raters']),2);
print("Hope dis werkd cz am gettin angry!");
}


change it to something like

if (mysql_num_rows($result) != 0) {
print"A little test
Rating - ";
print $row['rating'];
print"
Number of raters - ";
print $row['raters'];
while ($row = mysql_fetch_assoc($result)) {
$rating = number_format(($row['rating']) / ($row['raters']),2);
print("Hope dis werkd cz am gettin angry!");
}
Thu 20/01/05 at 13:01
Regular
"www.funrunner.co.uk"
Posts: 289
Right well now all that is happening is on the show the rating page it says that:

Warning: Division by zero in /home/alexj17/public_html/andy/rateit/shizzle.php on line 19

Even though i've changed the table to this:

id rating raters
1 99 1
2 99 1

so the number of raters being divided is by 1!

here's the code:

$id = 1;


function average_rating($id) {

$conn = mysql_connect("localhost", "alexj17_andy", "andyandy");
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']),2);
print("Hope dis werkd cz am gettin angry!");
}
}
else {
$rating = "Not rated"; //customise this as you desire
}
return $rating;
}

?>



I inserted my own $id = 1 in there is that right?
Is it something wrong with my database or the code?

thanks
AJ
Wed 19/01/05 at 16:56
Regular
"NULL"
Posts: 1,384
Sorry, my mistake, the function is is_numeric not is_num, so the line should be:

if(!is_numeric($_POST['rating']))
Wed 19/01/05 at 16:42
Regular
"www.funrunner.co.uk"
Posts: 289
got a slight problem. What i am doing is using all the code you have given me to set everthing up so it works in a demo environment and when i get it to work i will integrate it into my site. But when i submit this form:



Rating:


i get this error:

Fatal error: Call to undefined function is_num() in /home/alexj17/public_html/andy/rateit/addrating.php on line 3

the code for addrating is this:


if(!is_num($_POST['rating'])) { <----------line 3

$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']) . "';");
}
}
?>

this has completely stumped me n me bro (who knows more stuff than me) can anyone explain it to me please?

thanks for all your help so far nimco!
Wed 19/01/05 at 16:05
Regular
"NULL"
Posts: 1,384
e.g. your form might be:



Rating:


where the PHP code to rate the picture is in a file called addrating.php
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
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 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 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
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

Freeola & GetDotted are rated 5 Stars

Check out some of our customer reviews below:

Easy and free service!
I think it's fab that you provide an easy-to-follow service, and even better that it's free...!
Cerrie
Brilliant service.
Love it, love it, love it!
Christopher

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.