Jquery star rating tutorial using php and mysql
This is a very simple and quick tutorial on how to create star ratings very easily using jquery And store visitors vote in database to display product popularity.This is a sample script, Here i did not use very good UI, I just focus on creating dynamic star rating feature using PHP and Mysql.
I created a sample database where visitors vote will be store and Using those votes i’ll display average rating of product, Script created in Core PHP and Mysql so that you can easily integrate in your web based project.

Sample rating table structure.
CREATE TABLE IF NOT EXISTS `rating` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` int(11) NOT NULL,
`vote` float NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
In this tutorial i used a jquery star rating plugin to display star rating as UI part you can explore more UI rating feature by this official rating plugin. It also support bootstrap responsive feature.
http://www.jqueryrain.com/?d8VUtmAN
Create sample UI file with some demo products and it’s rating.
Product-1 |
Product-2 |
After that include required rating libraries(css & js).
Apply jquery code when ever user give rating to product then one ajax request will be go to server with productId and given vote and you need to store these values in your database.
Create server file rating.php, Where you’ll write rating save and fetch function.
0) {
$data['avg'] = $resultSet['vote']/$resultSet['count'];
$data['totalvote'] = $resultSet['count'];
} else {
$data['avg'] = 0;
$data['totalvote'] = $resultSet['count'];
}
return $data;
}
if(isset($_REQUEST['type'])) {
if($_REQUEST['type'] == 'save') {
$vote = $_REQUEST['vote'];
$productId = $_REQUEST['productId'];
$query = "INSERT INTO rating (product_id, vote) VALUES ('$productId', '$vote')";
// get connection
$con = connect();
$result = mysqli_query($con, $query);
echo 1; exit;
}
}
?>
See live demo and download source code.
DEMO |
DOWNLOAD |
Hope this tutorial will be helpful to make rating system for your web based projects.
If you like this post please don’t forget to subscribe my public notebook for more useful stuff
[jetpack_subscription_form title=”” subscribe_text=”Enter your email address to subscribe my public notebook..!!” subscribe_button=”SUBSCRIBE” show_subscribers_total=”1″]