Advertisement
Guest User

Untitled

a guest
May 21st, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. <?php
  2. Class Likes {
  3. private $user_obj;
  4. private $conn;
  5. public function __construct($conn, $user){
  6. $this->conn = $conn;
  7. $this->user_obj = NEW User($conn, $user);
  8. }
  9.  
  10.  
  11. public function addLikes($id_post, $id_user) {
  12. $id_post = $_POST['id_post'];
  13. $id_user = $_POST['id_user'];
  14.  
  15. if(isset($_POST['likes'])) {
  16. $likeTotal = mysqli_query($conn, "SELECT * FROM likes where id_post='$id_post'");
  17. $likeTotalResult = mysqli_num_rows($likeTotal);
  18.  
  19. $likeTotalPerUser = mysqli_query($conn, "SELECT * FROM likes WHERE id_user='$id_user' AND id_post='$id_post'");
  20. $likeTotalPerUserResult = mysqli_num_rows($likeTotalPerUser);
  21. if($likeTotalPerUserResult > 0) {
  22. exit();
  23. } else {
  24. mysqli_query($this->conn, "INSERT INTO likes (id_user, id_post, liked) VALUES ($id_user, $id_post, '1')");
  25. exit();
  26. }
  27. }
  28. }
  29. public function removeLikes($id_post, $id_user) {
  30. $id_post = $_POST['id_post'];
  31. $id_user = $_POST['id_user'];
  32.  
  33. if(isset($_POST['unliked'])) {
  34. $result = mysqli_query($this->conn, "SELECT * FROM post where id_post=$id_post");
  35. $row = mysqli_fetch_array($result);
  36. $n = $row['likes'];
  37.  
  38. $ifLikedCheck = mysqli_query($this->conn, "SELECT * FROM likes WHERE id_user=$id_user and id_post=$id_post");
  39. if($ifLikedCheck == 0) {
  40. exit();
  41. } else {
  42. mysqli_query($conn, "DELETE FROM likes WHERE id_user=$_SESSION[id_user] AND id_post=$_POST[id_post]");
  43. mysqli_query($conn, "UPDATE post SET likes=$n-1 WHERE id_post=$_POST[id_post]");
  44. }
  45. }
  46.  
  47. }
  48.  
  49. public function loadLikes($id_post, $id_user) {
  50. $id_post = $_POST['id_post'];
  51. $id_user = $_POST['id_user'];
  52. $likeEcho = "";
  53.  
  54. $retrieveLikes = mysqli_query($this->conn, "SELECT * FROM likes WHERE id_post='$id_post'"); // Retrieves all likes for specific post.
  55. $totalLikes = mysqli_num_rows($retrieveLikes); // Query for displayable like count to be echo'd.
  56. $retrieveUser = mysqli_query($this->conn, "SELECT * FROM users WHERE id_user='$id_user'"); // Retrieves the specific user, liking post.
  57. $hasUserLiked = mysqli_query($this->conn, "SELECT * FROM likes WHERE id_user='$_SESSION[id_user]' AND id_post='$id_post'"); //Querying if the User has liked the post.
  58.  
  59.  
  60.  
  61. if(mysqli_num_rows($retrieveLikes) > 0){
  62.  
  63. echo $likeEcho = '<div class="post-additional-info inline-items">
  64. <a style="cursor:pointer" class="post-add-icon-active inline-items" id="' .$id_post. '" data-id="' .$id_post. '">
  65. <svg class="olymp-heart-icon">
  66. <use xlink:href="svg-icons/sprites/icons.svg#olymp-heart-icon"></use>
  67. </svg>
  68. <span class="likes_count" id="' .$id_post. '">' .$totalLikes. '</span>
  69. </a>';
  70.  
  71. } else {
  72.  
  73. echo $likeEcho = '<div class="post-additional-info inline-items">
  74. <a style="cursor:pointer" class="post-add-icon inline-items" id="' .$id_post. '" data-id="' .$id_post. '">
  75. <svg class="olymp-heart-icon">
  76. <use xlink:href="svg-icons/sprites/icons.svg#olymp-heart-icon" id="addLike" data-id="' .$id_post. '"></use>
  77. <span class="likes_count" id="' .$id_post. '">' .$totalLikes. '</span>
  78. </a>';
  79.  
  80. }
  81. }
  82.  
  83. public function loadLikerPictureArray($id_post, $id_user) {
  84. //Displaying Liker's Profile Image and First Name (Picture Array)
  85. $id_post = $_POST['id_post'];
  86. $id_user = $_POST['id_user'];
  87. $arrayLiker = mysqli_query($this->conn, "SELECT * FROM users WHERE id_user IN (SELECT id_user FROM likes WHERE id_post='$id_post')");
  88. $arrayLikerResult = mysqli_num_rows($arrayLiker);
  89. $likerId = $likerFirstName = $likerPic = array();
  90. $l = 0;
  91. echo '<ul class="friends-harmonic">';
  92. while ($likerRow = $likerResult->fetch_assoc()) {
  93. $likerId[] = $likerRow['id_user'];
  94. $likerFirstName[] = $likerRow['firstName'];
  95. $likerPic[] = $likerRow['profilePic'];
  96. $likerTotal = $totalLikes - 2;
  97. if ($totalLikes == 0) {
  98. $likerName = "";
  99. } else if ($totalLikes == 1) {
  100. $likerName = '<a href="profile.php?id='.$likerId[0].'">'.$likerFirstName[0].'</a> liked this.';
  101. } else if ($totalLikes == 2) {
  102. $likerName = '<a href="profile.php?id='.$likerId[0].'">'.$likerFirstName[0].'</a> & <a href="profile.php?id='.$likerId[1].'">'.$likerFirstName[1].'</a> liked this.';
  103. } else {
  104. $likerName = '<a href="profile.php?id='.$likerId[0].'">'.$likerFirstName[0].'</a>, <a href="profile.php?id='.$likerId[1].'">'.$likerFirstName[1].'</a> and <br>'.$likerTotal.' more liked this. ';
  105. }
  106. if($l >= 5){ continue; }
  107. echo '<li>
  108. <a href="profile.php?id='.$likerId[$l].'">
  109. <img src="'.$likerPic[$l].'" alt="'.$likerFirstName[$l].'">
  110. </a>
  111. </li>';
  112. $l++;
  113. }
  114. echo '</ul>';
  115.  
  116. echo '<div class="names-people-likes">'.$likerName.'</div>';
  117. }
  118.  
  119. }
  120.  
  121.  
  122. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement