Znalazłem najprostszy skrypt Star rating na stronie
http://blog.hackerkernel.com/2015/11/01/star-rating-system-in-php-mysql-jquery/
Mam pytanie odnośnie tego w jaki sposób wyświetlić ocenę z bazy danych tzn:
Mam ocenę 5 gwiazdek i jedyna możliwość jaka przychodzi mi do głowy aby to wyświetlić to:
echo " <div id='star-container'> <i class='fa fa-star fa-2x star' id='star-1' style='color: gold;'></i> <i class='fa fa-star fa-2x star' id='star-2' style='color: gold;'></i> <i class='fa fa-star fa-2x star' id='star-3' style='color: gold;'></i> <i class='fa fa-star fa-2x star' id='star-4' style='color: gold;'></i> <i class='fa fa-star fa-2x star' id='star-5' style='color: gold;'></i> <i class='fa fa-star fa-2x star' id='star-6' ></i> </div> ";
I tutaj pojawia się pytanie odnośnie tego jak to poprawnie zrobić ponieważ chciałbym po najechaniu myszką zaznaczały się gwiazdki a jeśli zrobię to na sztywno jak wyżej nie mam takiej możliwości.
Index
<html> <head> <title>5 Star Rating System In PHP, MySql & jQuery</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css"> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <input type="hidden" value="HERE COME THE PRODUCT ID" id="product_id"> <div class="container"> <h1>Star Rating System</h1> <div id="star-container"> <i class="fa fa-star fa-3x star" id="star-1"></i> <i class="fa fa-star fa-3x star" id="star-2"></i> <i class="fa fa-star fa-3x star" id="star-3"></i> <i class="fa fa-star fa-3x star" id="star-4"></i> <i class="fa fa-star fa-3x star" id="star-5"></i> </div> <div id="result"></div> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script type="text/javascript" src="main.js"></script> </body> </html>
main.js
$(document).ready(function(){ /*STAR RATING*/ $('.star').on("mouseover",function(){ //get the id of star var star_id = $(this).attr('id'); switch (star_id){ case "star-1": $("#star-1").addClass('star-checked'); break; case "star-2": $("#star-1").addClass('star-checked'); $("#star-2").addClass('star-checked'); break; case "star-3": $("#star-1").addClass('star-checked'); $("#star-2").addClass('star-checked'); $("#star-3").addClass('star-checked'); break; case "star-4": $("#star-1").addClass('star-checked'); $("#star-2").addClass('star-checked'); $("#star-3").addClass('star-checked'); $("#star-4").addClass('star-checked'); break; case "star-5": $("#star-1").addClass('star-checked'); $("#star-2").addClass('star-checked'); $("#star-3").addClass('star-checked'); $("#star-4").addClass('star-checked'); $("#star-5").addClass('star-checked'); break; } }).mouseout(function(){ //remove the star checked class when mouseout $('.star').removeClass('star-checked'); }); $('.star').click(function(){ //get the stars index from it id product_id = $("#product_id").val(), //store the product id in variable star_container = $(this).parent(), //get the parent container of the stars result_div = $("#result"); //result div $.ajax({ url: "store_rating.php", type: "POST", data: {star:star_index,product_id:product_id}, beforeSend: function(){ star_container.hide(); //hide the star container result_div.show().html("Loading..."); //show the result div and display a loadin message }, success: function(data){ result_div.html(data); } }); }); });