Visitor rating script in one to five scale

Radio buttons to select rating
After reading an article visitors can submit rating or give feedback on the quality of the article by selecting a radio buttons in 1 to 5 scale.

This can also be uses a part of a picture rating script where visitors can submit ratings for the photos.

This is a basic script using PHP MySQL, you can read advance rating script using PHP MySQL & Ajax here

There are Three parts in this scirpt.
  1. Displaying the form at the end of the article page
  2. Processing the submitted rating of the visitor and storing in MySQL database table
  3. Displaying the average value of ratings given by the visitors on the page.
We will include these three files in our content pages like this.
$cont_id="1";// unique id for the page
require "config.php"; // Database connection 
require "rating.php";// Show the rating buttons ( Part I )
require "ratingck.php";// handle the rating submitted  ( Part II
require "disp.php";// Display the rating already submitted ( part III) 

Unique ID of the page $cont_id

You can see the first line requires a content ID $cont_id to identify the page in which it is connected. As we will be storing the rating in a database table so we need a system to identify each page with its rating, so the content ID will take care of this, here the content id is kept as a number to establish a link between our content management script and this rating script. In your case you can keep this as varchar field to store characters also. You have to take care that this filed must be a unique field as we can't store duplicate content id here.

Displaying the rating form: rating.php ( Part I )

This is taken care by rating.php file. This file is included by all pages where form for submitting rating is required. This form uses one hidden tag todo which is used to trigger the form processing script which is kept inside ratingck.php file. In this page five period ( radio ) buttons are used to collect the visitor rating in the range of 1 to 5. For better clarity star images are used by the side of the radio buttons. Here each radio button is provided with onclick event to submit the form. Once the form is submitted the rating is handled by ratingck.php page.
echo "<TABLE width=95% border=0 align=center cellpadding=0 cellspacing=0 >
<form name=f1 action='' method=post>";
<input type=hidden name=cont_id value='$cont_id'>
<input type=hidden name=todo value='submit-rating'>
<tr><td ><INPUT TYPE=RADIO NAME=rone Value=1 onClick='f1.submit()';><img src=images/star.gif>1</td>

<td><INPUT TYPE=RADIO NAME=rone Value=2 onClick='f1.submit()';>
<img src=images/star.gif><img src=images/star.gif>2</td>

<td ><INPUT TYPE=RADIO NAME=rone Value=3 onClick='f1.submit()';>
<img src=images/star.gif><img src=images/star.gif><img src=images/star.gif>3</td>

<td ><INPUT TYPE=RADIO NAME=rone Value=4 onClick='f1.submit()';>
<img src=images/star.gif><img src=images/star.gif><img src=images/star.gif>
<img src=images/star.gif>4</td>

<td ><INPUT TYPE=RADIO NAME=rone Value=5 onClick='f1.submit()';>
<img src=images/star.gif><img src=images/star.gif>
<img src=images/star.gif><img src=images/star.gif><img src=images/star.gif>5</td></tr>

<tr><td align=center colspan=5 bgcolor='#f1f1f1'> <INPUT TYPE=SUBMIT value=Vote NAME=Vote>
 </td>	</tr></form></table></center> ";

Handling the form inputs: ratingck.php ( Part II )

Once the form is submitted the rating of the visitor is collected and then it is checked if the record for this page ( identified through $cont_id) is already exist. If the record is not there then a new record with current rating is submitted. If the record is already there then the data ( record ) is updated. While updating the record we have to store total number of ratings given in the field nov and then add the present rating to old rating stored in the record. Each time we add a rating we will increase the number of vote filed ( nov ) by one.
if(isset($todo) and $todo=="submit-rating"){
$rone=$_POST['rone']; // Collect the rating value

$status="OK"; // Flag to store status
if(!isset($rone)){$msg=$msg."Pleae give your score and then click the button";
$status="NOT OK";

// If required additional validations can be added here // 			
if ($status=="OK"){
if($stmt = $connection->prepare("SELECT rating,nov FROM content_rating WHERE content_rating.cont_id=?")){
  $result = $stmt->get_result();
  $row=$result->fetch_object(); // get data from table for the page
  echo $connection->error;
// This is a new page so first add a new record
$query="INSERT INTO content_rating (cont_id,nov,rating) values ( ?,1,?)";
$stmt->bind_param("ii", $cont_id, $rone);
echo "No of records inserted : ".$connection->affected_rows; // Confirming the update
echo $connection->error;
} // end if , if query execution is successful 
} // end if , no record for the rating is found for the page. 

else { // Update the rating for the record
$rating=$row->rating;// read previous rating available in table 
$nov=$row->nov + 1;	 // Increase number of votes by 1 	
$status="OK";  // set the flat 

$rating=$rating+$rone; // Add current rating to stored rating 
$score_avg=number_format(($row->rating/$row->nov),2,".",""); // Find previous rating to display
$stmt = $connection->prepare("UPDATE content_rating SET rating=$rating,nov=$nov WHERE cont_id =?");
if ($stmt) {
$stmt->bind_param('i', $cont_id);
//echo "Record Updated:";
echo "Averate rating : $score_avg"; // Display the rating 
//echo $stmt->affected_rows;
echo $connection->error;
}else{echo $msg;}
}// end of todo checking
Read the part II for display of average rating

Using SQLite Database

Instead of MySQL database we can use SQLite. The basic script part remain same and here Php PDO is used for connection.
By updating few lines inside config.php file we can use same script connecting to MySQL database as PDO connection is common for both.

Changes required inside config.php

You must download the rating script using file. Open config.php file. There are two sections in this file, use one of them.
### Using SQLite database ##### 
// This will create the database if not exists in the same location where the script is running.
// For a different location give the path details. // use any one line below. 

//$my_conn = new PDO('sqlite:D:\\sqlite-data\\rating.db');// different path
//$my_conn = new PDO('sqlite:'.dirname(__FILE__).'/rating.db'); // same location

#### End of SQLite database ####

###### PHP PDO ##### For php PDO use below lines ##
$host_name = "localhost"; // or different host 
$database = "my_db";   // Change your database name
$username = "root";    // Your database user id 
$password = "password";// Your password

//////// Do not Edit below /////////
try {
$my_conn = new PDO('mysql:host='.$host_name.';dbname='.$database, $username, $password);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
####### END of PHP PDO ######

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer