In this tutorial, I am using jquery bar raging plugin to display the star rating
http://github.com/antennaio/jquery-bar-rating
--
-- Table structure for table `post_rating`
--
CREATE TABLE `post_rating` (
`id` int(11) NOT NULL,
`userid` int(11) NOT NULL,
`postid` int(11) NOT NULL,
`rating` int(2) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `post_rating`
ADD PRIMARY KEY (`id`);
ALTER TABLE `post_rating`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
--
-- Table structure for table `posts`
--
CREATE TABLE `posts` (
`id` int(11) NOT NULL,
`title` varchar(100) NOT NULL,
`content` text NOT NULL,
`link` varchar(255) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `posts`
--
INSERT INTO `posts` (`id`, `title`, `content`, `link`, `timestamp`) VALUES
(1, 'Yes, except the Dave Matthews Band doesn\'t rock.', 'The alien mothership is in orbit here. If we can hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate. If rubbin\' frozen dirt in your crotch is wrong, hey I don\'t wanna be right.', 'link-1', '2021-03-21 05:41:54'),
(2, 'Saving the world with meals on wheels.', 'You know how I sometimes have really brilliant ideas? Heh-haa! Super squeaky bum time! I\'m the Doctor. Well, they call me the Doctor. I don\'t know why. I call me the Doctor too. I still don\'t know why.', 'link-2', '2021-03-21 05:42:02'),
(3, 'Tell him time is of the essence.', 'This man is a knight in shining armor. Watching ice melt. This is fun. Tell him time is of the essence. This man is a knight in shining armor. You look perfect. He taught me a code. To survive.', 'link-3', '2021-03-21 05:42:08'),
(4, 'What is AngularJS', 'AngularJS is a JavaScript MVC framework developed by Google that lets you build well structured, easily testable, declarative and maintainable front-end applications which provides solutions to standard infrastructure concerns.', 'link-5', '2021-03-20 16:00:00'),
(5, 'What is MongoDB', 'It is a quick tutorial on MongoDB and how you can install it on your Windows OS. We will also learn some basic commands in MongoDB for example, creating and dropping a Database, Creation of a collection and some more operations related to the collection.', 'link-6', '2021-03-21 16:00:00'),
(6, 'Python Flask Load content Dynamically in Bootstrap Modal with Jquery AJAX and Mysqldb', 'Python Flask Load content Dynamically in Bootstrap Modal with Jquery AJAX and Mysqldb', 'link-6', '2021-03-20 16:00:00'),
(7, 'AutoComplete Textbox with Image using jQuery Ajax PHP Mysql and JqueryUI', 'AutoComplete Textbox with Image using jQuery Ajax PHP Mysql and JqueryUI', 'link-7', '2021-03-14 16:00:00'),
(8, 'PHP Mysql Registration Form Validation using jqBootstrapValidation with Jquery Ajax', 'PHP Mysql Registration Form Validation using jqBootstrapValidation with Jquery Ajax', 'link-8', '2021-03-20 16:00:00'),
(9, 'Python Flask Registration Form Validation using jqBootstrapValidation with Jquery Ajax and Mysql', 'Python Flask Registration Form Validation using jqBootstrapValidation with Jquery Ajax and Mysql', 'link-9', '2021-03-19 16:00:00'),
(10, 'Displaying Popups data on mouse hover using Jquery Ajax and PHP Mysql database', 'Displaying Popups data on mouse hover using Jquery Ajax and PHP Mysql database', 'link-10', '2021-03-15 16:00:00'),
(11, 'Displaying Popups data on mouse hover using Jquery Ajax and Python Flask Mysql database', 'Displaying Popups data on mouse hover using Jquery Ajax and Python Flask Mysql database', 'link-11', '2021-03-14 16:00:00');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `posts`
--
ALTER TABLE `posts`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `posts`
--
ALTER TABLE `posts`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=12;
//index.php <html> <head> <title>5 Star Rating Using PHP Mysql and jQuery AJAX</title> <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css"> <link href='jquery-bar-rating-master/dist/themes/fontawesome-stars.css' rel='stylesheet' type='text/css'> <script src="jquery-3.0.0.js" type="text/javascript"></script> <script src="jquery-bar-rating-master/dist/jquery.barrating.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function() { $('.rating').barrating({ theme: 'fontawesome-stars', onSelect: function(value, text, event) { var el = this; var el_id = el.$elem.data('id'); if (typeof(event) !== 'undefined') { var split_id = el_id.split("_"); var postid = split_id[1]; $.ajax({ url: 'rating_ajax.php', type: 'post', data: {postid:postid,rating:value}, dataType: 'json', success: function(data){ var average = data['averageRating']; $('#avgrating_'+postid).text(average); } }); } } }); }); </script> </head> <body> <p><h1 align="center">5 Star Rating Using PHP Mysql and jQuery AJAX</h1></p> <div class="content"> <?php include "dbcon.php"; $userid = 4; $query = "SELECT * FROM posts"; $result = mysqli_query($conn,$query); while($row = mysqli_fetch_array($result)){ $postid = $row['id']; $title = $row['title']; $conntent = $row['content']; $link = $row['link']; $query = "SELECT * FROM post_rating WHERE postid=".$postid." and userid=".$userid; $userresult = mysqli_query($conn,$query) or die(mysqli_error()); $fetchRating = mysqli_fetch_array($userresult); $rating = $fetchRating['rating']; $query = "SELECT ROUND(AVG(rating),1) as averageRating FROM post_rating WHERE postid=".$postid; $avgresult = mysqli_query($conn,$query) or die(mysqli_error()); $fetchAverage = mysqli_fetch_array($avgresult); $averageRating = $fetchAverage['averageRating']; if($averageRating <= 0){ $averageRating = "No rating yet."; } ?> <div class="post"> <h1><a href='<?php echo $link; ?>' class='link' target='_blank'><?php echo $title; ?></a></h1> <div class="post-text"> <?php echo $conntent; ?> </div> <div class="post-action"> <select class='rating' id='rating_<?php echo $postid; ?>' data-id='rating_<?php echo $postid; ?>'> <option value="1" >1</option> <option value="2" >2</option> <option value="3" >3</option> <option value="4" >4</option> <option value="5" >5</option> </select> <div style='clear: both;'></div> Average Rating : <span id='avgrating_<?php echo $postid; ?>'><?php echo $averageRating; ?></span> <script type='text/javascript'> $(document).ready(function(){ $('#rating_<?php echo $postid; ?>').barrating('set',<?php echo $rating; ?>); }); </script> </div> </div> <?php } ?> </div> <style> .content{ border: 0px solid black; border-radius: 3px; padding: 5px; margin: 0 auto; width: 50%; } .post{ border-bottom: 1px solid black; padding: 10px; margin-top: 10px; margin-bottom: 10px; } .post:last-child{ border: 0; } .post h1{ font-weight: normal; font-size: 30px; } .post a.link{ text-decoration: none; color: black; } .post-text{ letter-spacing: 1px; font-size: 15px; font-family: serif; color: gray; text-align: justify; } .post-action{ margin-top: 15px; margin-bottom: 15px; } .like,.unlike{ border: 0; background: none; letter-spacing: 1px; color: lightseagreen; } .like,.unlike:hover{ cursor: pointer; } </style> </body> </html>rating_ajax.php
//rating_ajax.php <?php include "dbcon.php"; $userid = 4; $postid = $_POST['postid']; $rating = $_POST['rating']; $query = "SELECT COUNT(*) AS cntpost FROM post_rating WHERE postid=".$postid." and userid=".$userid; $result = mysqli_query($conn,$query); $fetchdata = mysqli_fetch_array($result); $count = $fetchdata['cntpost']; if($count == 0){ $insertquery = "INSERT INTO post_rating(userid,postid,rating) values(".$userid.",".$postid.",".$rating.")"; mysqli_query($conn,$insertquery); }else { $updatequery = "UPDATE post_rating SET rating=" . $rating . " where userid=" . $userid . " and postid=" . $postid; mysqli_query($conn,$updatequery); } $query = "SELECT ROUND(AVG(rating),1) as averageRating FROM post_rating WHERE postid=".$postid; $result = mysqli_query($conn,$query) or die(mysqli_error()); $fetchAverage = mysqli_fetch_array($result); $averageRating = $fetchAverage['averageRating']; $return_arr = array("averageRating"=>$averageRating); echo json_encode($return_arr);