Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [HTML][PHP][MySQL] System Oceniania
xGlenor
post 13.08.2019, 15:11:13
Post #1





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 16.07.2019

Ostrzeżenie: (0%)
-----


Hej posiadam taki system oceniania

  1. <?php
  2. $conn = new mysqli('localhost', 'root', '', 'ratingSystem');
  3.  
  4. if (isset($_POST['save'])) {
  5. $uID = $conn->real_escape_string($_POST['uID']);
  6. $ratedIndex = $conn->real_escape_string($_POST['ratedIndex']);
  7. $ratedIndex++;
  8.  
  9. if (!$uID) {
  10. $conn->query("INSERT INTO stars (rateIndex) VALUES ('$ratedIndex')");
  11. $sql = $conn->query("SELECT id FROM stars ORDER BY id DESC LIMIT 1");
  12. $uData = $sql->fetch_assoc();
  13. $uID = $uData['id'];
  14. } else
  15. $conn->query("UPDATE stars SET rateIndex='$ratedIndex' WHERE id='$uID'");
  16.  
  17. exit(json_encode(array('id' => $uID)));
  18. }
  19.  
  20. $sql = $conn->query("SELECT id FROM stars");
  21. $numR = $sql->num_rows;
  22.  
  23. $sql = $conn->query("SELECT SUM(rateIndex) AS total FROM stars");
  24. $rData = $sql->fetch_array();
  25. $total = $rData['total'];
  26.  
  27. $avg = $total / $numR;
  28. ?>
  29. <!doctype html>
  30. <html lang="en">
  31. <head>
  32. <meta charset="UTF-8">
  33. <meta name="viewport"
  34. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  35. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  36. <title>Rating System</title>
  37. <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
  38. </head>
  39. <body>
  40. <div align="center" style="background: #000; padding: 50px;color:white;">
  41. <i class="fa fa-star fa-2x" data-index="0"></i>
  42. <i class="fa fa-star fa-2x" data-index="1"></i>
  43. <i class="fa fa-star fa-2x" data-index="2"></i>
  44. <i class="fa fa-star fa-2x" data-index="3"></i>
  45. <i class="fa fa-star fa-2x" data-index="4"></i>
  46. <br><br>
  47. <?php echo round($avg,2) ?>
  48. </div>
  49.  
  50. <script src="http://code.jquery.com/jquery-3.4.0.min.js" integrity="sha256-BJeo0qm959uMBGb65z40ejJYGSgR7REI4+CW1fNKwOg=" crossorigin="anonymous"></script>
  51. <script>
  52. var ratedIndex = -1, uID = 0;
  53.  
  54. $(document).ready(function () {
  55. resetStarColors();
  56.  
  57. if (localStorage.getItem('ratedIndex') != null) {
  58. setStars(parseInt(localStorage.getItem('ratedIndex')));
  59. uID = localStorage.getItem('uID');
  60. }
  61.  
  62. $('.fa-star').on('click', function () {
  63. ratedIndex = parseInt($(this).data('index'));
  64. localStorage.setItem('ratedIndex', ratedIndex);
  65. saveToTheDB();
  66. });
  67.  
  68. $('.fa-star').mouseover(function () {
  69. resetStarColors();
  70. var currentIndex = parseInt($(this).data('index'));
  71. setStars(currentIndex);
  72. });
  73.  
  74. $('.fa-star').mouseleave(function () {
  75. resetStarColors();
  76.  
  77. if (ratedIndex != -1)
  78. setStars(ratedIndex);
  79. });
  80. });
  81.  
  82. function saveToTheDB() {
  83. $.ajax({
  84. url: "index.php",
  85. method: "POST",
  86. dataType: 'json',
  87. data: {
  88. save: 1,
  89. uID: uID,
  90. ratedIndex: ratedIndex
  91. }, success: function (r) {
  92. uID = r.id;
  93. localStorage.setItem('uID', uID);
  94. }
  95. });
  96. }
  97.  
  98. function setStars(max) {
  99. for (var i=0; i <= max; i++)
  100. $('.fa-star:eq('+i+')').css('color', 'green');
  101. }
  102.  
  103. function resetStarColors() {
  104. $('.fa-star').css('color', 'white');
  105. }
  106. </script>
  107. </body>
  108. </html>


Wie ktoś może jak do tego zrobić to aby każdy mógł głosować tylko raz?
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 20.08.2019 - 02:46