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
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!");
}
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
if(!is_numeric($_POST['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!
where the PHP code to rate the picture is in a file called addrating.php
> 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
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.
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
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
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