Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Star Rating Script using PHP and MySQL with AJAX

#1
Star Rating Script using PHP and MySQL with AJAX

<div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2020/02/star-rating-script-using-php-and-mysql-with-ajax.png" width="550" height="314" title="" alt="" /></div><div><p>Last modified on February 4th, 2020 by Vincy.</p>
<p>How do you know your readers’ or customers’ thoughts on your website content? How does the star rating feature help to collect customers’ opinions?</p>
<p>Star rating is a feature that is used across different domains in a variety of ways. For example, it is one of the key building blocks in an eCommerce website.</p>
<p>Star rating helps you to know how people rank your content. It not only gives rank on your content. Also, it brings more readers to your page by the gained rating.</p>
<p>When you build a website, if you have got scope for implementing a star rating system and you should definitely experiment with it.</p>
<p>Websites use a variety of ways to allow users to rate content. For example, star rating, up-down rating, emoji rating and more.</p>
<p><img src="https://phppot.com/wp-content/uploads/2020/02/jQuery-Star-Rating-Script-550x314.png" alt="jQuery Star Rating Script" width="550" height="314"></p>
<p>We have seen so many examples for star rating, emoji rating and more. I grouped all those in this one single example.</p>
<p>I supported three UI alternatives for the rating section. Those are,</p>
<ul>
<li>Five-star rating</li>
<li>Favorite rating</li>
<li>Emoji rating</li>
</ul>
<p>I created a directive in PHP to configure the template for the rating appearance.</p>
<h2>What is inside?</h2>
<ol>
<li><a href="https://phppot.com/php/jquery-star-rating-script-using-php-and-mysql-with-ajax/#authentic-plugin-to-enable-dynamic-star-rating">Existing plugin to implement a dynamic star rating</a></li>
<li><a href="https://phppot.com/php/jquery-star-rating-script-using-php-and-mysql-with-ajax/#advantages-of-creating-a-custom-star-rating-script">Advantages of creating a custom star rating script</a></li>
<li><a href="https://phppot.com/php/jquery-star-rating-script-using-php-and-mysql-with-ajax/#various-types-of-rating-options">Various types of rating options</a></li>
<li><a href="https://phppot.com/php/jquery-star-rating-script-using-php-and-mysql-with-ajax/#about-this-jquery-star-rating-example">jQuery star rating example script</a></li>
<li><a href="https://phppot.com/php/jquery-star-rating-script-using-php-and-mysql-with-ajax/#the-rating-and-rated-content-database-script">The rating example database</a></li>
<li><a href="https://phppot.com/php/jquery-star-rating-script-using-php-and-mysql-with-ajax/#ui-design-to-render-star-rating-option">UI design to render star rating option</a></li>
<li><a href="https://phppot.com/php/jquery-star-rating-script-using-php-and-mysql-with-ajax/#jquery-functions-to-handle-user-rating-action">jQuery functions to handle user rating action</a></li>
<li><a href="https://phppot.com/php/jquery-star-rating-script-using-php-and-mysql-with-ajax/#storing-user-rating-to-mysql-database-from-php">Storing user rating to MySQL database from PHP</a></li>
<li><a href="https://phppot.com/php/jquery-star-rating-script-using-php-and-mysql-with-ajax/#output-screenshot-of-different-rating-options-with-star-favorite-emoji">Output screenshot of different rating options with star, favorite, emoji</a></li>
</ol>
<h2 id="authentic-plugin-to-enable-dynamic-star-rating">Existing plugins to implement a dynamic star rating</h2>
<p>There are various plugins available in the market to enable star rating. The ready-made plugins are with enormous features.</p>
<p>For example, <a href="https://github.com/gjunge/rateit.js" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">Rateit</a> is a jQuery based star rating plugin. It allows embedding a star rating with a list, select and more HTML markup. It supports Font Awesome, Material icons to display rating icons.</p>
<p>The <a href="https://github.com/janosgyerik/upvotejs" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">UpvoteJS</a> is a JavaScript package to render a StackExchange-like rating widget.</p>
<p>If you are having a WordPress website, there are built-in rating plugins available. <a href="https://wordpress.org/plugins/wp-postratings/" target="_blank" rel="noreferrer noopener" aria-label="Wp-PostRatings (opens in a new tab)">Wp-PostRatings</a> is a popular plugin to implement rating.</p>
<h2 id="advantages-of-creating-a-custom-star-rating-script">Advantages of creating a custom star rating script</h2>
<p>With a custom star rating, we can simplify the code logic instead of stuffing a lot.</p>
<p>And thereby, it will make it easy at the time of enhancement or maintenance phase.</p>
<p>It rectifies your overload with a feature-packed built-in. Also, it reduces your effort on managing a thousand lines of code for this simple UI feature.</p>
<h2 id="various-types-of-rating-options">Various types of rating options</h2>
<p>Websites use a different type of rating options to get user’s reviews. The following list shows some of the types.</p>
<ul>
<li>Single or multiple star rating</li>
<li>Facebook-like emoji rating</li>
<li>Simple up-down voting.</li>
<li>Like-unlike rating</li>
<li>Bar rating</li>
</ul>
<p>The types are veries in user’s mindset while rating. For example, the like-unlike and up-down rating expects binary options 0 or 1 from the user. But with the bar rating, it can have a range of points out of some limit.</p>
<h2 id="about-this-jquery-star-rating-example">Star rating example with AJAX</h2>
<p>I have created an AJAX-based star rating example code with PHP and jQuery.</p>
<p>This code will show a list of courses with an option to rate each course. The courses are from the database.</p>
<p>The rating element in the UI is configurable. I provided three UI alternatives for the rating section. Star, favorite and emoji ratings are there to collect users’ reviews.</p>
<p>While adding the rating, the code sends it to the PHP via AJAX. Then, it saves the ratings in the MySQL database.</p>
<p>Once rated, the user cannot rerate a course again to avoid duplicate ratings.</p>
<p>The following figure shows the file strucuture of the example code.</p>
<p><img src="https://phppot.com/wp-content/uploads/2020/02/star-rating-code-example-file-structure.jpg" alt="Star Rating Code Example File Structure"></p>
<h2 id="the-rating-and-rated-content-database-script">The rating example database</h2>
<p>This section shows the structure and the SQL of the database used in this example.</p>
<p>I have created two tables <em>tbl_courses</em> and <em>tbl_course_rating</em>. The <em>tbl_cources </em>database table contains courses on which the user will add ratings.</p>
<p>The <em>tbl_cource_rating</em> table has the mapping with the <em>tbl_cource</em> database. It has a unique rating without duplicates.</p>
<p><img src="https://phppot.com/wp-content/uploads/2020/01/star-rating-example-database-550x110.png" alt="Star Rating Example Database" width="550" height="110"></p>
<p>The below script shows the CREATE statement and the data dump for these two tables. By importing this script, you can setup this example in the local environment.</p>
<p class="code-heading">sql/db_rating.sql</p>
<pre class="prettyprint lang-php">--
-- Database: `db_rating`
-- -- -------------------------------------------------------- --
-- Table structure for table `tbl_course`
-- CREATE TABLE `tbl_course` ( `id` int(11) NOT NULL, `name` varchar(255) NOT NULL, `description` varchar(255) NOT NULL, `period` varchar(255) NOT NULL, `availabe_seats` int(11) NOT NULL, `last_date_to_register` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1; --
-- Dumping data for table `tbl_course`
-- INSERT INTO `tbl_course` (`id`, `name`, `description`, `period`, `availabe_seats`, `last_date_to_register`) VALUES
(1, 'Professional Training for Finantial Analyst\r\n', 'Professional Training for Finantial Analyst', '30 days', 2, '2020-01-31'),
(2, 'Enterprise Programming in Artificial Intelligence\r\n', 'Enterprise Programming in Artificial Intelligence', '30 days', 2, '2020-01-24'); -- -------------------------------------------------------- --
-- Table structure for table `tbl_course_rating`
-- CREATE TABLE `tbl_course_rating` ( `id` int(11) NOT NULL, `course_id` int(11) NOT NULL, `member_id` int(11) NOT NULL, `rating` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1; --
-- Indexes for dumped tables
-- --
-- Indexes for table `tbl_course`
--
ALTER TABLE `tbl_course` ADD PRIMARY KEY (`id`); --
-- Indexes for table `tbl_course_rating`
--
ALTER TABLE `tbl_course_rating` ADD PRIMARY KEY (`id`); --
-- AUTO_INCREMENT for dumped tables
-- --
-- AUTO_INCREMENT for table `tbl_course`
--
ALTER TABLE `tbl_course` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3; --
-- AUTO_INCREMENT for table `tbl_course_rating`
--
ALTER TABLE `tbl_course_rating` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=95;
COMMIT;</pre>
<h2 id="ui-design-to-render-star-rating-option">UI design to render star rating option</h2>
<p>In a landing page, it shows the list of courses from the database. The HTML code for this page is below.</p>
<p>It loads the rating element based on the PHP constant defined in a common configuration file.</p>
<p>The rating element will show clickable 5-stars or favorite-icons or emoji icons.</p>
<p>On clicking the rating element, it invokes a jQuery script to send AJAX requests to save ratings.</p>
<p class="code-heading">index.php</p>
<pre class="prettyprint lang-php">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
&lt;title&gt;Star Rating Script in PHP&lt;/title&gt;
&lt;link href="./assets/css/phppot-style.css" type="text/css" rel="stylesheet" /&gt;
&lt;link href="./assets/css/star-rating-style.css" type="text/css" rel="stylesheet" /&gt;
&lt;script src="./vendor/jquery/jquery-3.3.1.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;/head&gt; &lt;body&gt; &lt;div class="phppot-container"&gt; &lt;div class="container"&gt; &lt;h2&gt;Star Rating Script in PHP&lt;/h2&gt; &lt;div id="course_list"&gt; &lt;?php require_once "getRatingData.php"; ?&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;script src="./assets/js/rating.js"&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>I created separate template files to have different types of rating UI. Those are <em>star-rating-view.php</em>, <em>favorite-rating-view.php</em> and <em>emoji_rating_view.php</em>.</p>
<p>The template files show the onClick event handling specification in the markup.</p>
<p class="code-heading">star-rating-view.php</p>
<pre class="prettyprint lang-php">&lt;?php
for ($count = 1; $count &lt;= 5; $count ++) { $starRatingId = $row['id'] . '_' . $count; if ($count &lt;= $userRating) { ?&gt;
&lt;li value="&lt;?php echo $count; ?&gt;" id="&lt;?php echo $starRatingId; ?&gt;" class="star"&gt;&lt;img src="./img/&lt;?php echo $apperance; ?&gt;-filled.png"&gt;&lt;/li&gt;
&lt;?php } else { ?&gt;
&lt;li value="' . $count; ?&gt;" id="&lt;?php echo $starRatingId; ?&gt;" class="star" onclick="addRating(this,&lt;?php echo $row['id']; ?&gt;,&lt;?php echo $count; ?&gt;, 'star');" onMouseOver="mouseOverRating(&lt;?php echo $row['id']; ?&gt;,&lt;?php echo $count; ?&gt;,'&lt;?php echo $apperance; ?&gt;');" onMouseLeave="mouseOutRating(&lt;?php echo $row['id']; ?&gt;,&lt;?php echo $userRating; ?&gt;,'&lt;?php echo $apperance; ?&gt;');"&gt;&lt;img src="./img/&lt;?php echo $apperance; ?&gt;-open.png"&gt;&lt;/li&gt;
&lt;?php }
}
?&gt;</pre>
<p class="code-heading">favourite-rating-view.php</p>
<pre class="prettyprint lang-php">&lt;?php
for ($count = 1; $count &lt;= 5; $count ++) { $starRatingId = $row['id'] . '_' . $count; if ($count == $userRating) { ?&gt;
&lt;li value="&lt;?php echo $count; ?&gt;" id="&lt;?php echo $starRatingId; ?&gt;" class="star"&gt;&lt;img src="./img/&lt;?php echo $apperance; ?&gt;-filled.png"&gt;&lt;/li&gt;
&lt;?php } else { ?&gt;
&lt;li value="&lt;?php $count; ?&gt;" id="&lt;?php echo $starRatingId; ?&gt;" class="star" onclick="addRating(this,&lt;?php echo $row['id']; ?&gt;,&lt;?php echo $count; ?&gt;);" onMouseOver="mouseOverRating(&lt;?php echo $row['id']; ?&gt;,&lt;?php echo $count; ?&gt;,'&lt;?php echo $apperance; ?&gt;');" onMouseLeave="mouseOutRating(&lt;?php echo $row['id']; ?&gt;,&lt;?php echo $userRating; ?&gt;,'&lt;?php echo $apperance; ?&gt;');"&gt;&lt;img src="./img/&lt;?php echo $apperance; ?&gt;-open.png"&gt;&lt;/li&gt;
&lt;?php }
}
?&gt;</pre>
<p>In the emoji rating template, it shows a range of five emotion icons from <em>very-sad</em> to <em>very-happy</em>.</p>
<p>If you are providing support on the user’s queries, you can use the emoji rating. Embedding the emoji rating will give the customer’s emotional feel on your support.</p>
<p class="code-heading">emoji-rating-view.php</p>
<pre class="prettyprint lang-php">&lt;?php
for ($count = 1; $count &lt;= 5; $count ++) { $starRatingId = $row['id'] . '_' . $count; if ($count == $userRating) { ?&gt; &lt;li value="&lt;?php echo $count; ?&gt;" id="&lt;?php echo $starRatingId; ?&gt;" class="star"&gt;&lt;img src="./img/&lt;?php echo $apperance . $count; ?&gt;-filled.png"&gt;&lt;/li&gt;
&lt;?php } else {
?&gt; &lt;li value="&lt;?php $count; ?&gt;" id="&lt;?php echo $starRatingId; ?&gt;" class="star" onclick="addRating(this,&lt;?php echo $row['id']; ?&gt;,&lt;?php echo $count; ?&gt;);" onMouseOver="mouseOverRating(&lt;?php echo $row['id']; ?&gt;,&lt;?php echo $count; ?&gt;,'&lt;?php echo $apperance; ?&gt;');" onMouseLeave="mouseOutRating(&lt;?php echo $row['id']; ?&gt;,&lt;?php echo $userRating; ?&gt;,'&lt;?php echo $apperance; ?&gt;');"&gt;&lt;img src="./img/&lt;?php echo $apperance . $count; ?&gt;-open.png"&gt;&lt;/li&gt;
&lt;?php } }
?&gt;
</pre>
<h3>CSS Styles created for the star rating UI</h3>
<p class="code-heading">assets/css/star-rating-style.php</p>
<pre class="prettyprint lang-php">ul { margin: 0px; padding: 10px 0px 0px 0px; display: inline-flex;
} li.star { list-style: none; display: inline-block; margin-right: 5px; cursor: pointer; color: #9E9E9E;
} .row-title { font-size: 20px; color: #232323;
} .review-note { font-size: 12px; color: #999; font-style: italic;
} .row-item { margin-bottom: 20px; border-bottom: #F0F0F0 1px solid;
} p.text-address { font-size: 12px;
} img { height: 20px; width: 20px;
} .course-detail { font-size: 1em; margin-right: 20px;
} .loader-icon { display: none;
}
.response { display: inline-block; vertical-align: super; margin-left: 10px; color: #FF0000;
}
</pre>
<h2 id="jquery-functions-to-handle-user-rating-action">jQuery functions to handle user rating action</h2>
<p>This file contains jQuery functions to prepare AJAX requests to save user ratings.</p>
<p>It also handles the mouse hover events to highlight the rating element. The <em>mouseOverRating() </em>function highlights the star or other rating elements on hovering.</p>
<p>Similarly, <em>mouseOutRating() </em>resets the rating UI back to its original form on mouse out.</p>
<p>The AJAX code in the <em>addRating()</em> prepares the request with PHP endpoint URL and data params.</p>
<p>It receives the server response in the AJAX success callback at which it updates the UI accordingly.</p>
<p class="code-heading">assets/js/rating.js</p>
<pre class="prettyprint lang-javascript"> function mouseOverRating(courseId, rating, appearance) { if (appearance == "star") { for (var i = 1; i &lt;= rating; i++) { $('#' + courseId + "_" + i + ' img').attr('src', "./img/" + appearance + "-filled.png"); } } else { ratingIconPrefix = "./img/" + appearance; for (var i = 1; i &lt;= rating; i++) { if (appearance == "emoji") { ratingIconPrefix = "./img/" + appearance + "1"; } if (i == rating) { $('#' + courseId + "_" + i + ' img').attr('src', ratingIconPrefix + "-filled.png"); } } } } function mouseOutRating(courseId, userRating, appearance) { var ratingId; if (appearance == "star") { if (userRating != 0) { for (var i = 1; i &lt;= userRating; i++) { $('#' + courseId + "_" + i + ' img').attr('src', "./img/" + appearance + "-filled.png"); } } if (userRating &lt;= 5) { for (var i = (userRating + 1); i &lt;= 5; i++) { $('#' + courseId + "_" + i + ' img').attr('src', "./img/" + appearance + "-open.png"); } } $(".selected img").attr('src', "./img/" + appearance + "-filled.png"); } else { ratingIconPrefix = "./img/" + appearance; if (userRating &lt;= 5) { for (var i = 1; i &lt;= 5; i++) { if (appearance == "emoji") { ratingIconPrefix = "./img/" + appearance + i; } if (userRating == i) { $('#' + courseId + "_" + i + ' img').attr('src', ratingIconPrefix + "-filled.png"); } else { $('#' + courseId + "_" + i + ' img').attr('src', ratingIconPrefix + "-open.png"); } } } var selectedImageSource = $(".selected img").attr('src'); if (selectedImageSource) { selectedImageSource = selectedImageSource.replace('open', "filled"); $(".selected img").attr('src', selectedImageSource); } } } function addRating(currentElement, courseId, ratingValue, appearance) { var loaderIcon = $(currentElement).closest(".row-item"); $.ajax({ url : "ajax-end-point/insertRating.php", data : "index=" + ratingValue + "&amp;course_id=" + courseId, type : "POST", beforeSend : function() { $(loaderIcon).find("#loader-icon").show(); }, success : function(data) { loaderIcon = $(currentElement).closest(".row-item"); $(loaderIcon).find("#loader-icon").hide(); if (data != "") { $('#response-' + courseId).text(data); return false; } if (appearance == 'star') { $('#list-' + courseId + ' li').each( function(index) { $(this).addClass('selected'); if (index == $('#list-' + courseId + ' li').index( currentElement)) { return false; } }); } else { $(currentElement).addClass('selected'); } } }); }</pre>
<h2 id="storing-user-rating-to-mysql-database-from-php">Storing user rating to MySQL database from PHP</h2>
<p>When the AJAX script called, it prepares a request to the PHP. In PHP, it receives the chosen rating post data and stores it in the rating database.</p>
<p>Though the UI displays different elements, the rating value ranges from 1 to 5. In the database, it has the mapping between the rating value, course id and member id.</p>
<p>The below configuration is for setting the rating element appearance. The possible values are there with a comment statement.</p>
<p class="code-heading">Common/Config.php</p>
<pre class="prettyprint lang-php">&lt;?php
namespace Phppot; class Config
{ // Possible values: star | favourite | emoji const RATING_APPEARANCE = "favourite";
} </pre>
<p>This is a <a href="https://phppot.com/php/ajax-programming-with-php/">PHP endpoint called via AJAX</a>. It inserts the user rating to the database.</p>
<p>Before insert, it checks if the user added a rating already for that particular course. If so, then the code will not allow the user to rate again.</p>
<p class="code-heading">ajax-end-point/insertRating.php</p>
<pre class="prettyprint lang-php">&lt;?php
namespace Phppot; use Phppot\Rating;
require_once __DIR__ . "./../Model/Rating.php";
$rating = new Rating();
// Here the user id is harcoded.
// You can integrate your authentication code here to get the logged in user id
$userId = 5; if (isset($_POST["index"], $_POST["course_id"])) { $courseId = $_POST["course_id"]; $ratingIndex = $_POST["index"]; $rowCount = $rating-&gt;isUserRatingExist($userId, $courseId); if ($rowCount == 0) { $insertId = $rating-&gt;addRating($userId, $courseId, $ratingIndex); if (empty($insertId)) { echo "Problem in adding ratings."; } } else { echo "You have added rating already."; }
}
</pre>
<p>This code reads courses and corresponding rating from the database. It prepares <a href="https://phppot.com/jquery/load-dynamic-data-using-jquery/">HTML markup embedded with dynamic data</a>.</p>
<p>This file displays the courses list with the rating option in a landing page.</p>
<p class="code-heading">getRatingData.php</p>
<pre class="prettyprint lang-php">&lt;?php
namespace Phppot; use Phppot\Rating;
require_once "./Common/Config.php";
$config = new Config();
require_once "./Model/Rating.php";
$rating = new Rating();
// Here the user id is harcoded.
// You can integrate your authentication code here to get the logged in user id
$userId = 5; $apperance = $config::RATING_APPEARANCE; $courseResult = $rating-&gt;getCourse();
if (! empty($courseResult)) { foreach ($courseResult as $row) { $userRating = $rating-&gt;getUserRating($userId, $row['id']); $totalRating = $rating-&gt;getTotalRating($row['id']); $date = date_create($row["last_date_to_register"]); ?&gt;
&lt;div class="row-item"&gt; &lt;div class="row-title"&gt;&lt;?php echo $row['name']; ?&gt;&lt;/div&gt; &lt;ul class="list-inline" id="list-&lt;?php echo $row['id']; ?&gt;"&gt; &lt;?php require $apperance . "-rating-view.php"; ?&gt; &lt;img src="img/loader.gif" class="loader-icon" id="loader-icon"&gt; &lt;/ul&gt; &lt;div class="response" id="response-&lt;?php echo $row['id']; ?&gt;"&gt;&lt;/div&gt; &lt;p class="review-note"&gt;Total Reviews: &lt;?php echo $totalRating; ?&gt;&lt;/p&gt; &lt;p class="text-address"&gt; &lt;label class="course-detail"&gt;Period: &lt;?php echo $row["period"]; ?&gt;&lt;/label&gt;&lt;label class="course-detail"&gt;Available seats: &lt;?php echo $row["availabe_seats"]; ?&gt;&lt;/label&gt;&lt;label class="course-detail"&gt;Last Date to Register: &lt;?php echo date_format($date, "d M Y"); ?&gt;&lt;/label&gt; &lt;/p&gt;
&lt;/div&gt;
&lt;?php }
}
?&gt;</pre>
<p>In the above two PHP files, I have harcoded the user id with a PHP variable <em>$userId</em>. You can plugin <a href="https://phppot.com/php/php-login-script-with-session/">user authentication code</a> and get the logged-in user id.</p>
<p>The <em>Rating.php </em>is a PHP model class created for performing the rating actions.</p>
<p>It has functions to read courses and user ratings on them. The <em>getUserRating() </em>and <em>getTotalRating()</em> functions returns data to display the rating statistics.</p>
<p>The <em>isUserRatingExist()</em> checks the uniqueness of the user rating on a particular course.</p>
<p>I used <a href="https://phppot.com/php/crud-with-mysqli-prepared-statement-using-php/">prepared statements with MySQLi</a> for executing the database queries. The source contains a generic DAO class DataSource.php for executing database operations.</p>
<p class="code-heading">Model/Rating.php</p>
<pre class="prettyprint lang-php">&lt;?php
namespace Phppot; use Phppot\DataSource; class Rating
{ private $ds; function __construct() { require_once __DIR__ . './../lib/DataSource.php'; $this-&gt;ds = new DataSource(); } function getCourse() { $query = "SELECT * FROM tbl_course ORDER BY id DESC"; $result = $this-&gt;ds-&gt;select($query); return $result; } function getUserRating($userId, $courseId) { $average = 0; $avgQuery = "SELECT rating FROM tbl_course_rating WHERE member_id = ? and course_id = ?"; $paramType = 'ii'; $paramValue = array( $userId, $courseId ); $result = $this-&gt;ds-&gt;select($avgQuery, $paramType, $paramValue); if ($result &gt; 0) { foreach ($result as $row) { $average = round($row["rating"]); } // endForeach } // endIf return $average; } function getTotalRating($courseId) { $totalVotesQuery = "SELECT * FROM tbl_course_rating WHERE course_id = ?"; $paramType = 'i'; $paramValue = array( $courseId ); $result = $this-&gt;ds-&gt;getRecordCount($totalVotesQuery, $paramType, $paramValue); return $result; } function isUserRatingExist($userId, $courseId) { $checkIfExistQuery = "select * from tbl_course_rating where member_id = ? and course_id = ?"; $userId; $courseId; $paramType = 'ii'; $paramValue = array( $userId, $courseId ); $rowCount = $this-&gt;ds-&gt;getRecordCount($checkIfExistQuery, $paramType, $paramValue); return $rowCount; } function addRating($userId, $courseId, $rating) { $insertQuery = "INSERT INTO tbl_course_rating(member_id,course_id, rating) VALUES (?,?,?) "; $paramType = 'iii'; $paramValue = array( $userId, $courseId, $rating ); $insertId = $this-&gt;ds-&gt;insert($insertQuery, $paramType, $paramValue); return $insertId; }
}
</pre>
<h2 id="output-screenshot-of-different-rating-options-with-star-favorite-emoji">Output screenshot of different rating options with star, favorite, emoji</h2>
<p><img src="https://phppot.com/wp-content/uploads/2020/02/jQuery-Five-Star-Rating-Output-550x340.jpg" alt="jQuery Five Star Rating Output" width="550" height="340"></p>
<p><img src="https://phppot.com/wp-content/uploads/2020/02/Favorite-Rating-Template-Screenshot-550x172.jpg" alt="Favorite Rating Template Screenshot" width="550" height="172"></p>
<p>In the following screenshot, it displays a text in red. It is an error message to notify the user if he tries to add rating again.</p>
<p><img src="https://phppot.com/wp-content/uploads/2020/02/Emoji-Rating-Output-550x359.jpg" alt="Emoji Rating Output"></p>
<h2>Conclusion</h2>
<p>We have seen the importance of implementing a rating script in an application. Also, we have seen the types of ratings generally used by the applications.</p>
<p>By supporting 3 types of rating UI components in an example script, I sure it helps you to have options. You can choose and fix one among them on a need basis.</p>
<p>For example, if you manage more applications, then integrate this one in all. The rating configuration allows setting based on the nature of the application.</p>
<p>There is no limit. We can enhance this component by adding more rating options like-unlike, up-down voting, rating with a ranger and more.</p>
<p><a class="download" href="https://phppot.com/downloads/star-rating-script-in-php.zip">Download</a></p>
<p> <!-- #comments --> </p>
<div class="related-articles">
<h2>Popular Articles</h2>
</p></div>
<p> <a href="https://phppot.com/php/jquery-star-rating-script-using-php-and-mysql-with-ajax/#top" class="top">↑ Back to Top</a> </p>
</div>


https://www.sickgaming.net/blog/2020/02/...with-ajax/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016