Llamasery.com Home  
 
Server-side Tutorial/Article: Rate This - Part 2 of 4

How it works

Since seeing is believing, if you want to see a real demonstration of this script in action - albeit a very simple demonstration - visit this site where you'll find three short and very boring articles you can rate.

Since we claimed it was 'simple', let's begin by showing you the code you need to add to the page containing your second article where you want the rating system to appear:

<?php
$article = 2;
include("rate-this.inc");
?>

That's all the code you need. Plus - of course - a simple database table set up to record the voting, and your choice of images to represent the voting graphically But before we get to the exact how to do it all, let's just consider how relatively trivial the operations are.

  1. Read the database to get the number of previous voters and the total score-to-date
  2. Display a representative number of 'good' and 'bad' images to show the average score graphically
  3. Display the average vote and number of voters so far
  4. If this visitor has not already voted then display the 'vote now' form, otherwise do not display the form
  5. If the visitor 'votes', then increment the number of voters, increase the 'score', update the database, re-display the page, and stop the same visitor from voting again.

If you can read the number of previous voters and total score from your database, calculating the average score is trivial code; a couple of simple loops can display the right numbers of 'good' and 'bad' images; and it's trivial to display the average vote and number of voters. If your visitor votes, it's trivial to increase the number of voters and the total score-to-date. All the 'tricks' to this script involve how to stop the 'vote now' form from appearing when that particular visitor has already voted.

So, tell me about the trick!

«  previous  |  next  »