Scientists study the world as it is. Engineers create the world that has never been.
- Theodore von Kármán
Ok, first I'm gonna tell you something about this tutorial.
We are going to create rating system that is very easy to create, easy to install and the best part: "It is fully automated.
This PHP Rating System works by calling php file from Js on click of an mouse button.
Data can be saved id MySql database or txt files or anywhere you want. For this tutorial we will use txt files.
- You will need latest jquery library. I'am using jquery-1.6.4.min.js. And that's all. Nothing else.
We will only have 2 files:
- first one will be index.php
- and finally you will need PHP script that savethe data. Let's call this one also rate.php
This is the first file we are going to create. Let's take a look at this one.
< a href="javascript:rate('like')"> I Like it! < /a>
< a href="javascript:rate('dislike')"> Not so much.< /a>
As you can see this is only links that will call our JS function and this we will include on every page that you want to be rated.
As I said this file have a trigger function. That means that in this file we will have function that is going to call our php file on mouse click.
This function wee will call rate and it looks like this:
function rate(likeDislike){
if(likeDislike=='like'){
$.get('rate.php?action=like&u=< ? php echo $_SERVER['REQUEST_URI'];?>');
}else{
$.get('rate.php?action=dislike&u=< ? php echo $_SERVER['REQUEST_URI'];?>');
}
}
Note: Here php brackets are spaced "< ? PHP" . Delete spaces before use otherwise you code will not work due to the iregular sintax.
Let's explain what we have here. $.get calles rate.php file using GET method and sends two variables: " action" and " u".
Action variable sends string that tell us what action we want. Do we want to like or dislike.
Variable u is used so it can tell the PHP file which page we rated. < ? php echo $_SERVER['REQUEST_URI'];?> This makes our rater automated.
And we are finally here. Rate.php.
Because we said that we are going to use TXT files to save number of likes or dislikes in this file we will have something like this:
< ?php
$filename = basename($_GET['u']);
$filename=str_replace('.php', '', $filename);
$file=$filename.'_'.$_GET['action'].'.txt';
$fp = fopen($file, "r");
$count = fread($fp, 1024);
fclose($fp);
$count = $count + 1;
$fp = fopen($file, "w");
fwrite($fp, $count);
fclose($fp);
?>
First line of code is straight forward.
Second line is used to clear extension, in this case .php.
Third line, variable $file is our name of the txt file that depends on the curent url, page that we rate.You can put this file in separate folder or tell your function to save your taring files in some other folder by changing this line of code
$file=$filename.'_'.$_GET['action'].'.txt';
to
$file='./FOLDER NAME/'.$filename.'_'.$_GET['action'].'.txt';
As you can see $_GET['action'] will form unique name with $filename so you will hahe something like this for you index page.
index_like.txt
index_dislike.txt
In the rest of the lines we have reading, incrementing value from the file and writing new value in the file. I suppose you already know how to do this so I'm not going to explain those.
Very fast, very simple, easy to implement rating system.
Hope you like it. (-:7
Posted by: Admin
Updated: 2011-10-05