Taking tutorial submissions! Please email them to dave[AT]icemelon[DOT]com for review. Thanks!

Latest Tutorials
IceMelon IM: Add IM Functionality to phpBB [Misc] Want to add site-wide member IM to your phpBB community? Sure you do. Simple step-by-step instructions inside.

IceMelon IM: Add IM Functionality to vBulletin [Misc] Want to add site-wide member IM to your vBulletin community? Simple step-by-step instructions inside.

Crawl Your Site for PageRanks [PHP] So you know your site has a PageRank of 8. What about the PRs of all the inner pages? This tutorial will teach how you to automate the process of grabbing all these PRs.

Create CAPTCHA images [PHP] Worried about bots? Then create CAPTCHA images to prevent bots from taking advantage of your site.

Convert SHN and FLAC files to MP3 [Misc] Low on disk space and have an insensitive ear? Here are some steps to converting SHN and FLAC files to MP3 format.

[View All]

Become a sponsor for $15/month. Link is sitewide - PR5 homepage, 20+ PR4 pages, 90+ PR3 pages. Email dave[AT]icemelon[D0T]c0m.

Awesome Tutorials

A Rating System
By dave

Sit back, relax, and imagine this. You run a huge international movie database site. And, your database software is MySQL. You want visitors of your site to be able to rate movies, so that you can rank the movies. Here's how to get started...

First, let's work with some assumptions:
1) Your MySQL table that stores the movies is called 'table_movie.'
2) You uniquely identify each movie stored in 'table_movie' with an ID number, which you have labeled 'movieID.'
3) The rating system will be on a 10-point scale.

Modify the MySQL table
You will need to add to 2 more integer ('INT') columns to 'table_movie'; I would set the sizes to 255. Let's call them 'totalScore' and 'numberOfVotes.' Later on, you will calculate the rating by dividing totalScore/numberOfVotes. Get the logic?

You can have visitors rate movies by either submitting a form (of method='POST') or by clicking links. For convenience, let's say you opted for choice 2. Here's an example link:

Reminder: Variables that are retrieved from the URL are stored in the $_GET array. With that in mind, let's start writing rating.php:

$query = mysql_query("SELECT totalScore, numberOfVotes FROM table_movie WHERE movieID='$_GET[movieID]'");
list($totalScore, $numberOfVotes) = mysql_fetch_array($query);
$totalScore = $totalScore + $_GET['rating'];
$newRating = bcdiv($totalScore, ++$numberOfVotes, 1);
echo "The new rating is $newRating! ";
mysql_query("UPDATE table_movie SET totalScore='$totalScore', numberOfVotes='$numberOfVotes' WHERE movieID='$_GET[movieID]'");

The above code first extracted the current values of 'totScore' and 'numberOfVotes' for the movie specified by the movie ID number. Those values were then updated; and the new rating was calculated and printed/echoed out. Lastly, 'table_movie' was updated with these new values.

The function used to calculate the rating was bcdiv. This function has 3 arguments: the dividend, the divisor, and the number of decimal places to round to (respectively). In this case, we round to the tenth place. Note that the '++$numberOfVotes' will increment the 'numberOfVotes' variable before it's used.

That was simple enough. Now, let's say you want to create a "Top 10 Movies" list, as determined by your visitors' ratings. Here's how..

$query = mysql_query("SELECT movieName FROM table_movie WHERE numberOfVotes > 20 ORDER BY (totalScore/numberOfVotes) DESC LIMIT 0,10");
while( list($movieName) = mysql_fetch_array($query) )
  echo "<br> $movieName ";

Let's break the code down. First, look at the WHERE clause. I specified 'numberOfVotes > 20' so that a movie needs to have been rated more than 20 times before appearing on your top 10 list. I then ordered the results by '(totalScore/numberOfVotes) DESC.' As you recall, totalScore/numberOfVotes calculates the rating. The 'DESC' means to sort the ratings from highest to lowest. (The alternative would be 'ASC,' or ascending, which will transform your list to be a "Top 10 Worst Movies" list.) Finally, "LIMIT 0,10" limits the query to return only the first 10 results. To get a better idea of how 'LIMIT' works, here are a few examples and explanations:
» LIMIT 1,10 — returns results 2-11
» LIMIT 10,10 — returns results 10-20
» LIMIT 100,1 — returns result 100

Finally, the while loop just iterates through and prints out the movie names. Of course, you can modify the query to return more data, so that you can also print out the number of votes and rating associated with each movie. But, I'll leave that for you.

P.S. Check out my new site TheManWhoSoldtheWeb.com, where I publish guides and scripts on Internet Marketing and SEO. Here is a limited time freebie: the Rapid Google Indexer.

» Bookmark this Tutorial
» Bookmark IceMelon
Icemelon -- A Rating System -- PHP, CSS, Javascript Tutorials, & More!
  © 2005-2010 Icemelon.com   Email: dave[AT]icemelon[D0T]c0m