Visitor rating script in one to five scale

After reading an article visitors can submit rating or give feedback on the quality of the article by selecting a radio button.. This feature is quite common in various content management systems where user of visitor feedback is taken for assessment of the news or blog posting. Here we can ask the visitor to give feedback by writing comments or we can ask them to simply rate the article in 1 to 5 scale. Here we will discuss how to develop such a script to take care of ratings submitted by the visitors.

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

The script for content rating scripts has three main parts. One is displaying the form at the end of the article page. Second one is processing the submitted ratting of the visitor, third part is the displaying the average value of ratings given. We will try to keep these three parts in three different pages and we will include them from our content pages. This way maintaining the code became easy. For any modification we can change the script at a central location. We will include these three files in our content pages like this.
$cont_id="1";// unique id for the page
require "config.php";
require "ratingck.php";
require "disp.php";
require "rating.php";
You can see the first line requires a content 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

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 tags todo is used to trigger the form processing script kept inside ratingck.php file. In this page period ( radio ) buttons are used to collect the visitor rating. There are five radio buttons to collect 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.

Handling the form inputs: ratingck.php

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"){

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

if ($status=="OK")

$result=mysql_query("SELECT rating,nov FROM content_rating WHERE content_rating.cont_id= $cont_id");

// This is a new page so first add a new record
$tt=mysql_query("insert into content_rating (cont_id,nov,rating) values ( $cont_id,1,$rone)");

else {
$nov=$row->nov + 1;		


	$result=mysql_query("update content_rating set rating=$rating,nov=$nov where cont_id =$cont_id");

}else{echo $msg;}
}// end of todo checking

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here .

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