*sigh* I'm about to just give up.. I'm trying to do this:
[ARTICLE START]
Code: CREATE DATABASE `5starratings`;
Now that you have your database created you want to create a table inside the database which will hold the ratings. The reason why I wanted to implement a 5 Star Rating system was because I wanted users to be able to rate songs on my website. Hence the name of my table is songratings. You can name the table whatever you want as long as you make appropriate changes to the script. Ok here is the code to create the table.
Code: CREATE TABLE `songratings` (
`id` INT NOT NULL AUTO_INCREMENT ,
`song_id` INT NOT NULL ,
`song_rating` FLOAT NOT NULL ,
`song_num_votes` INT NOT NULL ,
PRIMARY KEY ( `id` )
);
Ok let me explain the code above. You just created a table with 4 coloumns (Fields). The first Field "id" is auto incermented and has nothing to do with the rating script. Its just there to keep the table organized. The second field "song_id" is an integer. Since I am rating songs each song I want to rate must be assigned a song_id number. The next field "Song_rating" is important. Please not that I made my song_rating field a FLOAT instead of an INT. The reason is that you want the rating to be in a decimal format. If you use INT instead of Float the MYSQL will round of the rating and that will cause alot of problems. The last but not the least field is the "song_num_votes" field. This records the number of votes given to a particular song.
Ok now that you have created your database and table, its time to get down to the script. My applogies to PHP Freaks as I changed the code to fit my style and liking. The major change I made to the script was that I am not using functions. I dont know why but there is something about functions I dont like.
Now you need to create two PHP pages. A page called form.php which will contain the rating form and the second page called rateprocess.php which will contain the script to grab the rating from the form, calculate new rating, update the database with the new rating and display the new rating and stars.
HERE IS THE CODE FOR THE FIRST PAGE :::..FORM.PHP..:::
<html>
<head>
<title>Rate this song</title>
</head>
<body>
<p>Rate this song!</p>
<?
//form is in php because of the variable $song_id which is recieved through urlencode!
echo ("
<form name="Rate_Form" method="post" action="rateprocess.php">
<select size="1" name="rating">
<option selected value="5">5- Excellent</option>
<option value="4">4 - Good</option>
<option value="3">3- Fair</option>
<option value="2">2 - So So</option>
<option value="1">1 - Poor</option>
<option value="0">0- Awful</option>
</select>
<input type="hidden" name="song_id" value="$song_id">
<input type="submit" value="Go!">
</form>
");
?>
</body>
</html>
Let me explain the code above. Remember that I am rating songs. Now a vistor came to my website where he browsed to a page which listed songs. Now he liked a song and wanted to rate it. So he clicked the rate link next to the song which then brought him to the form.php page. Now the page form.php recieves the Variable $song_id from the page where the user clicked the rate link. Hence the rate link should look like this
<p><a href="
http://www.somesite.com/rate/form.php?song_id=1">Rate this Song</a></p>
I know and some of might be thinking, What the Heck is guy talking about. My appologies agian. Please try to concentrate and remember that I am rating songs. Now inorder for me to rate songs each song should be assigned a song_id. So before any thing can be rated I need to assign all my songs song_ids. To do this I must input manually the song_ids in my database.
But for the sake of this tutorial I am going to assume that I just have one song that I want users to rate. I am going to assign the song the song_id '1'. This will make things a lot simpler and easy to understand. So I am going to go back to my database and enter the song_id 1 for the one song that I want to rate. Here is the code to do just that.
CODE: INSERT INTO `songratings` ( `id` , `song_id` , `song_rating` , `song_num_votes` )
VALUES (
'', '1', '0', '0'
);
Ok Let me expalin. Now I just updated my database and added a song whose song_id=1 to be rated. Please note that I assigned 0 to song_rating and song_num_votes. This just means that the song has not been rated yet and the NOT RATED YET Gif will be dispalyed.
Now We want to update the form.php page because we are not recieving the variable $song_id from any page. Hence all we are going to do is to assign the varialbe $song_id a value of 1.
<html>
<head>
<title>Rate this song</title>
</head>
<body>
<p>Rate this song!</p>
<?
$song_id=1;
//form is in php because of the variable $song_id which is recieved through urlencode!
echo ("
<form name="Rate_Form" method="post" action="rateprocess.php">
<select size="1" name="rating">
<option selected value="5">5- Excellent</option>
<option value="4">4 - Good</option>
<option value="3">3- Fair</option>
<option value="2">2 - So So</option>
<option value="1">1 - Poor</option>
<option value="0">0- Awful</option>
</select>
<input type="hidden" name="song_id" value="$song_id">
<input type="submit" value="Go!">
</form>
");
?>
</body>
</html>
Now let me explain what the page form.php does. This page takes 2 variable and posts or passes them to the rateprocess.php page. The first variable is $song_id and the second variable is the rating the user assigned the song ($rating). Quite simple.
Now lets move on to the next page :::..RATEPROCESSPHP..:::
Let me give you the code first and then I will explain.
<html>
<head>
<title>Rate Process</title>
</head>
<body>
<?
// --------------------------------------------- PART 1 -------------------------------------
//PLEASE CHANGE THESE VALUES !!
$dbhost = "HOST"; // DB HOST
$dbusername = "USERNAME"; // USERNAME
$dbpass = "PASSWORD"; // USER PASSWORD
$dbname = "5starrating"; // DB NAME
//PLEASE CHANGE THESE VALUES !!
$connection = mysql_connect($dbhost, $dbusername, $dbpass);
$SelectedDB = mysql_select_db($dbname);
$query = mysql_query ("SELECT song_rating, song_num_votes FROM songratings WHERE song_id=$song_id") or die (mysql_error());
while(list($song_rating, $song_num_votes)= mysql_fetch_array($query))
{
$new_count = ($song_num_votes + 1);
$song_rating2 = ($song_rating * $song_num_votes);
$new_rating = (($rating + $song_rating2) / ($new_count));
$new_rating2 = number_format($new_rating, 2, '.', '');
$update_rating = mysql_query("UPDATE songratings SET
song_rating='$new_rating2',song_num_votes='$new_count' WHERE song_id=$song_id");
echo "<div align="center"><b>
<p>Thanks for your vote!</p>
<p>The new rating for this song is:
$new_rating2 out of 5</p>";
}
// --------------------------------------------- PART 2 ------------------------------------------
if((($new_rating2 >= 0)or($new_rating2 == 0)) && ($new_rating2 <= 0.50)){
echo "<img src="/rate/images/stars/0o5.gif" width="70" height="18">";
}
if((($new_rating2 >= 0.50)or($new_rating2 == 0.50)) && ($new_rating2 <= .99)){
echo "<img src="/rate/images/stars/05o5.gif" width="70" height="18">";
}
if((($new_rating2 >= 1.00)or($new_rating2 == 1.50)) && ($new_rating2 <= 1.49)){
echo "<img src="/rate/images/stars/1o5.gif" width="70" height="18">";
}
if((($new_rating2 >= 1.50)or($new_rating2 == 1.50)) && ($new_rating2 <= 1.99)){
echo "<img src="/rate/images/stars/15o5.gif" width="70" height="18">";
}
if((($new_rating2 >= 2.00)or($new_rating2 == 2.00)) && ($new_rating2 <= 2.49)){
echo "<img src="/rate/images/stars/2o5.gif" width="70" height="18">";
}
if((($new_rating2 >= 2.50)or($new_rating2 == 2.50)) && ($new_rating2 <= 2.99)){
echo "<img src="/rate/images/stars/25o5.gif" width="70" height="18">";
}
if((($new_rating2 >= 3.00)or($new_rating2 == 3.00)) && ($new_rating2 <= 3.49)){
echo "<img src="/rate/images/stars/3o5.gif" width="70" height="18">";
}
if((($new_rating2 >= 3.50)or($new_rating2 == 3.50)) && ($new_rating2 <= 3.99)){
echo "<img src="/rate/images/stars/35o5.gif" width="70" height="18">";
}
if((($new_rating2 >= 4.00)or($new_rating2 == 4.00)) && ($new_rating2 <= 4.49)){
echo "<img src="/rate/images/stars/4o5.gif" width="70" height="18">";
}
if((($new_rating2 >= 4.50)or($new_rating2 == 4.50)) && ($new_rating2 <= 4.99)){
echo "<img src="/rate/images/stars/45o5.gif" width="70" height="18">";
}
if($new_rating2 == 5.0){
echo "<img src="/rate/images/stars/5o5.gif" width="70" height="18">";
}
?>
</body>
</html>
[ARTICLE END]
I'm understanding most of it...
but there's so many errors in the coding and stuff..
I'll just give up