0

this is probably going to require something very basic but I can't find an answer to do this in the way I want it to work. What I have is a mysql table of articles and a table of favourites where if you like an article the information is logged. So I need to select all of the articles that I like from one table and then all of the articles from another and display them all as a feed, which I have done. The part I need help with is saying, if I like one of the posts, do not display the like button (Alternatively if I haven't yet liked it, show the button). Can anyone please point me in the right direction with this? Here's the code I have but it only shows the last one I Liked:

<?php
$check_like_sql = "SELECT * FROM posts WHERE type = 'Like' && poster = '$yourid'";
$check_like_res = mysqli_query($con, $check_like_sql) or die (mysqli_error());
if(mysqli_affected_rows($con)>0){
    while($likes = mysqli_fetch_array($check_like_res)){
        $yourlike = $likes['media'];
    }
}
?>
<?php
$get_posts_sql = "SELECT posts.*, members.username, members.avatar, fans.connection FROM posts LEFT JOIN members ON members.id = posts.poster LEFT JOIN fans ON fans.fan = '$yourid' WHERE posts.poster = fans.connection ORDER BY date DESC";
$get_posts_res = mysqli_query($con, $get_posts_sql) or die (mysqli_error());
if(mysqli_affected_rows($con)>0){
    while($posts = mysqli_fetch_assoc($get_posts_res)){
        $postid = $posts['id'];
        etc
        etc


        if($yourlike == $postid){
            $likethis = "<a href=\"php/unlike.php?poster=$yourid&post=$postid\">Unlike</a> . ";
        }
        else if($posttype == "Like"){
            $likethis = "";
        }
        else{
            $likethis = "<a href=\"php/like.php?poster=$yourid&lat=$yourlat&lon=$yourlon&like=$postid&user=$postusername\">Like</a> . ";
        }

        $post .= "";
    }
}
?>
Cœur
  • 37,241
  • 25
  • 195
  • 267
user2516546
  • 111
  • 1
  • 2
  • 9

1 Answers1

1

You can left outer join from your articles table to your liked_articles table.

select a.*, 
       case when la.liked_article_id is null then 'Not Liked Yet' else 'Already Liked' as LikedStatus 
from   articles a
left outer join liked_articles la
on     a.id = la.article_id
and    @userId = la.user_id;

This may not be syntactically correct for MYSQL but you get the gist.

Bill Gregg
  • 7,067
  • 2
  • 22
  • 39