Today, We want to share with you Simple PHP Rating System with database using JavaScript.
In this post we will show you Star-Rating System, hear for PHP Star Rating System with JavaScript we will give you demo and example for implement.
In this post, we will learn about Building a 5 Star Rating System With jQuery, AJAX and PHP with an example.
- Simple PHP Rating System with database using JavaScript
- Database Script for Star
Rating
- Add Star Rating using PHP AJAX
- Create Hover Effect on Five-Star
- PHP JavaScript Star Rating System Output
- Read
- Summary
- Related posts
There are the Following The simple About Simple PHP Rating System with database using JavaScript Full Information With Example and source code.what makes a
restaurant 5 star?
Database Script for Star Rating
Make statements as well as the data facke for the tbl_hotel_review and tbl_hotels tables.
--
-- Table structure for table `tbl_hotel_review`
--
CREATE TABLE `tbl_hotel_review` (
`id` int(11) NOT NULL,
`member_id` int(11) NOT NULL DEFAULT '1',
`hotel_id` int(11) NOT NULL,
`review` int(2) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `tbl_hotels`
--
CREATE TABLE `tbl_hotels` (
`id` int(11) NOT NULL,
`name` varchar(100) NOT NULL,
`address` varchar(150) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `tbl_hotels`
--
INSERT INTO `tbl_hotels` (`id`, `name`, `address`) VALUES
(1, '9 old sungan classic ploat new', '12, pakainfo Enclave'),
(2, 'newar infinityknow web dola', '78, PAKA Park');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `tbl_hotel_review`
--
ALTER TABLE `tbl_hotel_review`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `tbl_hotels`
--
ALTER TABLE `tbl_hotels`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `tbl_hotel_review`
--
ALTER TABLE `tbl_hotel_review`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=13;
--
-- AUTO_INCREMENT for table `tbl_hotels`
--
ALTER TABLE `tbl_hotels`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
COMMIT;
List Restaurants with Star Rating Option
<body onload="displayHotelData('fetchReviewData.php')">
<div class="container">
<h2>5 Star Rating System using simple PHP MySQLi and Javascript</h2>
<span id="hotel_list"></span>
</div>
</body>
<script type="text/javascript">
function displayHotelData(url) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("hotel_list").innerHTML = this.results;
}
};
xhttp.open("GET", url, true);
xhttp.send();
}//endFunction
</script>
fetchReviewData.php
This PHP servser side source code is called with jQuery an AJAX request to response the restaurant list with user review data. It simple dynamic forms HTML
results that will be fetched at the jQuery AJAX success simple callback.
<?php
require_once "db.php";
require_once "functions.php";
$member_id = 1;
$query = "SELECT * FROM tbl_hotels ORDER BY id DESC";
$result = mysqli_query($conn, $query);
$outputString = '';
foreach ($result as $row) {
$memberReview = memberReview($member_id, $row['id'], $conn);
$totalRating = totalRating($row['id'], $conn);
$outputString .= '
<div class="row-item">
<div class="row-title">' . $row['name'] . '</div> <div class="response" id="response-' . $row['id'] . '"></div>
<ul class="list-inline" onMouseLeave="starmemberRatig(' . $row['id'] . ',' . $memberReview . ');"> ';
for ($count = 1; $count <= 5; $count ++) {
$starRatingId = $row['id'] . '_' . $count;
if ($count <= $memberReview) {
$outputString .= '<li value="' . $count . '" id="' . $starRatingId . '" class="star selected">★</li>';
} else {
$outputString .= '<li value="' . $count . '" id="' . $starRatingId . '" class="star" title="addRating(' . $row['id'] . ',' . $count . ');" onMouseOver="mouseOverRating(' . $row['id'] . ',' . $count . ');">★</li>';
}
} // endFor
$outputString .= '
</ul>
<p class="review-note">Total Reviews: ' . $totalRating . '</p>
<p class="text-address">' . $row["address"] . '</p>
</div>
';
}
echo $outputString;
?>
Add Star Rating using PHP AJAX
an AJAX methods addRating() to call the server side PHP file insertHotelReview.php
<script>
function addRating(newhotelId, reviewValue) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
displayHotelData('fetchReviewData.php');
if (this.results != "success") {
alert(this.results);
}
}
};
xhttp.open("POST", "insertHotelReview.php", true);
xhttp.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
var parameters = "index=" + reviewValue + "&hotel_id="
+ newhotelId;
xhttp.send(parameters);
}
</script>
insertHotelReview.php
<?php
require_once ('db.php');
$member_id = 1;
if (isset($_POST["index"], $_POST["hotel_id"])) {
$newhotelId = $_POST["hotel_id"];
$review = $_POST["index"];
$checkIfExistQuery = "select * from tbl_hotel_review where member_id = '" . $member_id . "' and hotel_id = '" . $newhotelId . "'";
if ($result = mysqli_query($conn, $checkIfExistQuery)) {
$totalCount = mysqli_num_rows($result);
}
if ($totalCount == 0) {
$insertHotelQuery = "INSERT INTO tbl_hotel_review(member_id,hotel_id, review) VALUES ('" . $member_id . "','" . $newhotelId . "','" . $review . "') ";
$result = mysqli_query($conn, $insertHotelQuery);
echo "success";
} else {
echo "Thanks, Already Voted!";
}
}
?>
Create Hover Effect on Five-Star
Other pure JavaScript Functions to Make Hover Effect
on Five-Star
<script>
function mouseOverRating(newhotelId, review) {
startsRatingNew(newhotelId)
for (var i = 1; i <= review; i++)
{
var reviewId = newhotelId + "_" + i;
document.getElementById(reviewId).style.color = "#ff6e00";
}
}
function startsRatingNew(newhotelId)
{
for (var i = 1; i <= 5; i++)
{
var reviewId = newhotelId + "_" + i;
document.getElementById(reviewId).style.color = "#9E9E9E";
}
}
function starmemberRatig(newhotelId, memberReview) {
var reviewId;
if(memberReview !=0) {
for (var i = 1; i <= memberReview; i++) {
reviewId = newhotelId + "_" + i;
document.getElementById(reviewId).style.color = "#ff6e00";
}
}
if(memberReview <= 5) {
for (var i = (memberReview+1); i <= 5; i++) {
reviewId = newhotelId + "_" + i;
document.getElementById(reviewId).style.color = "#9E9E9E";
}
}
}
</script>
PHP JavaScript Star Rating System Output
PHP-JavaScript-Star-Rating-System-Output-Angular 6 CRUD Operations Application TutorialsRead :
- Technology
- Google Adsense
- Programming
Summary
You can also read about
AngularJS, ASP.NET, VueJs, PHP.
I hope you get an idea about PHP Star Rating System with JavaScript.
I would like to have feedback
on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.
I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live
in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.