Ajax-PHP Rating Stars Script

from coursesweb.net

This is a script to add a Rating System (with stars) to rate images, articles, videos, games, or any element in pages of your web site.
This Rating Stars Script uses Ajax technology, with JavaScript and PHP (with OOP), it can be included in any page (".html", or ".php"), but the server must run PHP.
The rating data for each element can be saved in TXT files on the server, SQLite database, or in a MySQL database. By default, the script saves data in TXT files.
The rated item, the number of the day, and the user (name or IP) are saved on the server, and in cookies, so, the user can rate again the next day.
You can use the script to add rating system to one or more elements in a web page.
If you have a register /login script in your web site, you can set this Rating Script to allow only the logged users to rate. By default, all the visitors can rate an element once to 24 hours.

Installation and Settings

1. Copy on your server the directories: "ratingfiles", and "ratingtxt" (with all their files), in the "Root" folder ('www', 'public_html', 'htdocs').
2. In the <head> </head> zone of the page in which you want to use this script, add this line of code (for CSS styles):
<link href="/ratingfiles/ratings.css" rel="stylesheet" type="text/css" />
3. In the place /places of the web page where you want to display the stars and the rating system, add the following code (a DIV tag):
<div class="srtgs" id="rt_??"></div>
- Replace the two "??" characters with the name /ID of the element you want to be rated, the rating stars added by JavaScript in this DIV will be associated to this element. This name (or ID) must be unique, and always after the characters: "rt_" in the "id" attribute.

For example, if you want to apply the rating system to an image, and to an article, both in the same page, add under each one a DIV with the code presented above. The "id" attribute in each DIV will be: id="rt_img_name", and id="rt_article1". Similarly you can proceed with multiple elements.

4. Add this code to the end of the HTML document, before the ending </body>, so the JavaScript instructions to be accessed after all the html code for ratings data are loaded.
<script src="/ratingfiles/ratings.js"></script>
• You can see how it is added in the file "test.html" (which can be used for test, if it is accessed from the server).

If the script is used in multiple pages of your website, you should be aware to not have two identical names, even if they are in different pages, because they are saved in the same ".txt" file (or mysql /sqlite table).

5. The "ratingtxt" directory, with TXT files: "rtgitems.txt", and "rtgusers.txt" are necesary if you want to save the rating data on TXT files, or SQLite database.
In this case you must set read-write-execute permissions to these files, CHMOD 0777 (or 0755) so the PHP can write data in that directory.

For SQLite

To save data in SQLite database, sets the 'sqlite' value to the SVRATING constant, in ratings.php file.
define('SVRATING', 'sqlite');
- Then, create the tables in SQLite database. Open in your browser the file "ratingfiles/create_tables.php".
(In your browser you must access an address like: "http://localhost/ratingfiles/create_tables.php"), "localhost" is the domain name.
- It will create a sqlite.db file in the ratingtxt directory.

To change the path and name of the SQLite database used by this script, edit the following line in the class.rating.php file (in the setConn() function):
self::$conn = new PDO('sqlite:'.$this->dir.'sqlite.db');
For example:
self::$conn = new PDO('sqlite:path/to/sqlite.db');

For MySQL

- If you want to save data in MySQL database, you can ignore /delete the "ratingtxt/" directory.
In this case, open the file "ratings.php" (in the "ratingfiles/" folder), and edit the following data:
    1) To set the script to use MySQL, change the value of the constant SVRATING from 'txt' to 'mysql' :
define('SVRATING', 'mysql');
    2) For connecting to your MySQL database:
define('DBHOST', 'localhost');            - replace localhost with your MySQL server address
define('DBUSER', 'root');                 - replace root with your database user name
define('DBPASS', 'passdb');               - replace passdb with your password for MySQL database
define('DBNAME', 'dbname');               - replace dbname with the name of your MySQL database
3) Create the tables in MySQL database. To create the tables, open in your browser the file "ratingfiles/create_tables.php".
(In your browser you must access an address like: "http://localhost/ratingfiles/create_tables.php"), "localhost" is the domain name.

Other settings

1) This application can be used to display four types of rating stars system. Just set the value of the rt_style variable in the ratings.js file:
2) If you want the user to can rate only a single item in one day, change the value of the constant NRRTG from 0 to 1.
define('NRRTG', 0);

If your web site has a register /login script, and you want than only the logged users to can rate the elements, set the value of USRRATE to 0 . Then set $_SESSION['username'] with the session your script uses to keep logged users; to the following code, in "ratings.php"
define('USRRATE', 1);
if(USRRATE !== 1) {
  if(!isset($_SESSION)) session_start();
  if(isset($_SESSION['username'])) define('RATER', $_SESSION['username']);
}



In this Rating Script, the user can vote again an item next day.
If you want to set another time when the user can vote again, for example over 6 hours, or 2 days, next week, or you want the user to can vote an item a single time only, use the PHP files from the 'rating_stars_script_set_time.zip' archive.

- Replace the "class.rating.php", "create_tables.php", and "ratings.php" with the files from that archive.
- See the comments in "ratings.php". Then access the "create_tables.php" to create the tables in MySQL or SQLite database.

With these php files, data can be stored only in MySQL or SQLite database.


3) To show just the voting results for an element (without the possibility to vote), add data-rt='novote' attribute to the DIV that shows the rating stars.
<div class='srtgs' id='rt_??' data-rt='novote'></div>


4) If the rating data are saved in MySQL or SQLite database:
To create a list which shows the rating scores of multiple voted items, based on highest rating, perform a SQL Select in the "rtgitems" table. This table stores the voted items (in "item" column), points (in "totalrate" column), and number of votes (in "nrrates" column).
Example:
SELECT `item`, (`totalrate` / `nrrates`) AS `rank` FROM `rtgitems` ORDER BY (`totalrate` / `nrrates`) DESC LIMIT 10

5) If the "ratingfiles/" folder is not in Root, or if you want to use this Rating Script in pages that are in different folders, open the "ratings.js" file (in "ratingfiles" directory), and replace this code:
cerere_http.open("post", '/ratingfiles/ratings.php', true);
With this line of code (to use the absolute path to the "ratings.php" file):
cerere_http.open("post", 'http://'+ document.domain +'/ratingfiles/ratings.php', true);

6) The Time Zone is set to "Europe/London" in "ratings.php" file. To change it, modify the value of the date_default_timezone_set() function:
date_default_timezone_set('Europe/London');


You can test it online on the page: Ajax-PHP Rating Stars Script.

- The JavaScript instructions, the names and sizes in CSS, and stars are related, if you change something without knowing exactly what it does, it may not display / work properly.


Have a Good Life with Everyone and with YourSelf.

With respect, CoursesWeb.net