PDA

View Full Version : letting users rate an item


undeadfilms
02-17-2005, 02:36 AM
I run a movie database website and I want to add the option that users can vote on each movie (1-5 ratings) and the page automatically updates itself.

I have no idea how to do this but I have an example.

Movie Page with Rating thing I want (http://www.brimstone.org/horrormovies.php?mode=show_movie_info&movie_id=1)

The part that says is this movie any good.

Any help would be greatly appreciated.

Taylor_1978
02-17-2005, 02:40 AM
Umm. This is a very vague message. Have you started on any coding for it? Or are you asking someone to write one for you?

Basically you need a HTML form, and then once posted you need it to add the result to your database.

undeadfilms
02-17-2005, 02:42 AM
Yeah I know, I apologize. I am just in the dark here. The form is no problem, its just the whole working it into the database. Will they automatically updated a single row in an single item or do I need a table for each item I want to be rated?

Thank you for your responce.

Taylor_1978
02-17-2005, 02:54 AM
Okay - I'm going to have to do a bit of assuming here.. but I'll take from what you have been saying in your other message to do so...

So lets say you have a table called "movies" and this has the following fields.

id
name
summary


Create another table called "ratings" with fields:

id
one
two
three
four
five

Now - once the person votes do something like this:


$result = mysql_query("SELECT * FROM ratings WHERE id='$_POST[id]'");
$row = mysql_fetch_array($result);

if ($_POST[rating] == 1) {
$row[one]++;
mysql_query("UPDATE ratings SET one='$row[one]' WHERE id='$_POST[id]'");
}

if ($_POST[rating] == 2) {
$row[two]++;
mysql_query("UPDATE ratings SET two='$row[two]' WHERE id='$_POST[id]'");
}
// Continue this for all 5


That should get ya started.

undeadfilms
02-17-2005, 02:55 AM
and do i just set up the id of the ratings table to match the id of the movies table?

Taylor_1978
02-17-2005, 02:57 AM
Yes. I assume your "movies" table field "id" is auto-increment... the ratings one should NOT be auto-increment and just make it match up as you said.

undeadfilms
02-17-2005, 03:02 AM
Ok I will try it out. Thank you so much for your time.

if you got some time heres my site...

Undead Films (http://undeadfilms.com)

Here is my code should I run it like this?


<form action="rate.php" method="POST">
<SELECT>
<OPTION SELECTED value="1">1
<OPTION value="2">2
<OPTION value="3">3
<OPTION value="4">4
<OPTION value="5">5
</SELECT>
<input type=submit value="Rate This Movie" name="submit"></center>
</form>


and here is rate.php

<?php
$Host = "localhost";
$User = "undeadf_aj";
$Password = "*****";
$DBName = "movies";
$TableName = "ratings";

$Link = mysql_connect ($Host, $User, $Password);

$result = mysql_query("SELECT * FROM ratings WHERE id='$_POST[id]'");
$row = mysql_fetch_array($result);

if ($_POST[rating] == 1) {
$row[one]++;
mysql_query("UPDATE ratings SET one='$row[one]' WHERE id='$_POST[id]'");
}

if ($_POST[rating] == 2) {
$row[two]++;
mysql_query("UPDATE ratings SET two='$row[two]' WHERE id='$_POST[id]'");
}

if ($_POST[rating] == 3) {
$row[one]++;
mysql_query("UPDATE ratings SET one='$row[one]' WHERE id='$_POST[id]'");
}

if ($_POST[rating] == 4) {
$row[one]++;
mysql_query("UPDATE ratings SET one='$row[one]' WHERE id='$_POST[id]'");
}

if ($_POST[rating] == 5) {
$row[one]++;
mysql_query("UPDATE ratings SET one='$row[one]' WHERE id='$_POST[id]'");
}
?>