kangjaz

proses_new_rating

Oct 1st, 2020
1,141
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. //include connect
  3. require_once('./connect.php');
  4. //dapatkan ip user dan encrypt menggunakan MD5
  5. $ipuser = md5($_SERVER['REMOTE_ADDR']);
  6.  
  7. if (isset($_POST['rate']) && is_numeric($_POST['rate']) && isset($_POST['id']) && is_numeric($_POST['id'])) {
  8.     //tampung value yang dikirim melalui ajax
  9.     $rate = $con->real_escape_string($_POST['rate']);
  10.     $id = $con->real_escape_string($_POST['id']);
  11.  
  12.     //cek apakah user telah memberi penilaian terhadap content tersebut
  13.     $sql = $con->query("SELECT * FROM `tbl_rating` WHERE ipuser = '" . $ipuser . "' AND id_content = '" . $id . "'");
  14.  
  15.     //hitung row
  16.     if ($sql->num_rows > 0) {
  17.         //lakukan update jika user sudah pernah menilai sesuai id content
  18.         $con->query("UPDATE `tbl_rating` SET `rating` = '" . $rate . "' WHERE ipuser = '" . $ipuser . "' AND id_content = '" . $id . "'");
  19.     } else {
  20.         //simpan jika user belum pernah menilai
  21.         $con->query("INSERT INTO `tbl_rating` VALUES ('" . $ipuser . "', '" . $rate . "', '" . $id . "')");
  22.     }
  23.  
  24.     //hitung rata-rata rating sesuai kontent
  25.     $q = $con->query("SELECT AVG(rating) AS jml FROM tbl_rating WHERE id_content = '" . $id . "'")->fetch_assoc();
  26.  
  27.     echo $rate . '|' . ceil($q['jml']) . '|';
  28.     //looping untuk menentukan warna bintang
  29.     for ($i = 1; $i <= 5; $i++) {
  30.         if ($i <= ceil($q['jml'])) {
  31.             echo '<span class="on"><i class="fa fa-star"></i></span>';
  32.         } else {
  33.             echo '<span class="off"><i class="fa fa-star"></i></span>';
  34.         }
  35.     }
  36. }
  37.  
RAW Paste Data