Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- Class Likes {
- private $user_obj;
- private $conn;
- public function __construct($conn, $user){
- $this->conn = $conn;
- $this->user_obj = NEW User($conn, $user);
- }
- public function addLikes($id_post, $id_user) {
- $id_post = $_POST['id_post'];
- $id_user = $_POST['id_user'];
- if(isset($_POST['likes'])) {
- $likeTotal = mysqli_query($conn, "SELECT * FROM likes where id_post='$id_post'");
- $likeTotalResult = mysqli_num_rows($likeTotal);
- $likeTotalPerUser = mysqli_query($conn, "SELECT * FROM likes WHERE id_user='$id_user' AND id_post='$id_post'");
- $likeTotalPerUserResult = mysqli_num_rows($likeTotalPerUser);
- if($likeTotalPerUserResult > 0) {
- exit();
- } else {
- mysqli_query($this->conn, "INSERT INTO likes (id_user, id_post, liked) VALUES ($id_user, $id_post, '1')");
- exit();
- }
- }
- }
- public function removeLikes($id_post, $id_user) {
- $id_post = $_POST['id_post'];
- $id_user = $_POST['id_user'];
- if(isset($_POST['unliked'])) {
- $result = mysqli_query($this->conn, "SELECT * FROM post where id_post=$id_post");
- $row = mysqli_fetch_array($result);
- $n = $row['likes'];
- $ifLikedCheck = mysqli_query($this->conn, "SELECT * FROM likes WHERE id_user=$id_user and id_post=$id_post");
- if($ifLikedCheck == 0) {
- exit();
- } else {
- mysqli_query($conn, "DELETE FROM likes WHERE id_user=$_SESSION[id_user] AND id_post=$_POST[id_post]");
- mysqli_query($conn, "UPDATE post SET likes=$n-1 WHERE id_post=$_POST[id_post]");
- }
- }
- }
- public function loadLikes($id_post, $id_user) {
- $id_post = $_POST['id_post'];
- $id_user = $_POST['id_user'];
- $likeEcho = "";
- $retrieveLikes = mysqli_query($this->conn, "SELECT * FROM likes WHERE id_post='$id_post'"); // Retrieves all likes for specific post.
- $totalLikes = mysqli_num_rows($retrieveLikes); // Query for displayable like count to be echo'd.
- $retrieveUser = mysqli_query($this->conn, "SELECT * FROM users WHERE id_user='$id_user'"); // Retrieves the specific user, liking post.
- $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.
- if(mysqli_num_rows($retrieveLikes) > 0){
- echo $likeEcho = '<div class="post-additional-info inline-items">
- <a style="cursor:pointer" class="post-add-icon-active inline-items" id="' .$id_post. '" data-id="' .$id_post. '">
- <svg class="olymp-heart-icon">
- <use xlink:href="svg-icons/sprites/icons.svg#olymp-heart-icon"></use>
- </svg>
- <span class="likes_count" id="' .$id_post. '">' .$totalLikes. '</span>
- </a>';
- } else {
- echo $likeEcho = '<div class="post-additional-info inline-items">
- <a style="cursor:pointer" class="post-add-icon inline-items" id="' .$id_post. '" data-id="' .$id_post. '">
- <svg class="olymp-heart-icon">
- <use xlink:href="svg-icons/sprites/icons.svg#olymp-heart-icon" id="addLike" data-id="' .$id_post. '"></use>
- <span class="likes_count" id="' .$id_post. '">' .$totalLikes. '</span>
- </a>';
- }
- }
- public function loadLikerPictureArray($id_post, $id_user) {
- //Displaying Liker's Profile Image and First Name (Picture Array)
- $id_post = $_POST['id_post'];
- $id_user = $_POST['id_user'];
- $arrayLiker = mysqli_query($this->conn, "SELECT * FROM users WHERE id_user IN (SELECT id_user FROM likes WHERE id_post='$id_post')");
- $arrayLikerResult = mysqli_num_rows($arrayLiker);
- $likerId = $likerFirstName = $likerPic = array();
- $l = 0;
- echo '<ul class="friends-harmonic">';
- while ($likerRow = $likerResult->fetch_assoc()) {
- $likerId[] = $likerRow['id_user'];
- $likerFirstName[] = $likerRow['firstName'];
- $likerPic[] = $likerRow['profilePic'];
- $likerTotal = $totalLikes - 2;
- if ($totalLikes == 0) {
- $likerName = "";
- } else if ($totalLikes == 1) {
- $likerName = '<a href="profile.php?id='.$likerId[0].'">'.$likerFirstName[0].'</a> liked this.';
- } else if ($totalLikes == 2) {
- $likerName = '<a href="profile.php?id='.$likerId[0].'">'.$likerFirstName[0].'</a> & <a href="profile.php?id='.$likerId[1].'">'.$likerFirstName[1].'</a> liked this.';
- } else {
- $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. ';
- }
- if($l >= 5){ continue; }
- echo '<li>
- <a href="profile.php?id='.$likerId[$l].'">
- <img src="'.$likerPic[$l].'" alt="'.$likerFirstName[$l].'">
- </a>
- </li>';
- $l++;
- }
- echo '</ul>';
- echo '<div class="names-people-likes">'.$likerName.'</div>';
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement