How to develop "favorite" feature (put liked post into favorites list etc.)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ilya Kraft
    New Member
    • Jan 2011
    • 134

    How to develop "favorite" feature (put liked post into favorites list etc.)

    Hello,

    I need to develop "favorite" feature for my website. I'd like to use something similar to twitter. For example I have a page with lot's of different posts and then a link to "Favorite" post > If person clicks that link, post gets added to favorite list. How to develop something like this?

    Additional - how can I change color of Favorited post say from white to yellow, so it indicates favorite posts for user.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Set up a table named something along the lines of 'user_favorites '. This table will contain two items:
    1. user_id: so we can grab the favorited data for some user;
    2. article_id: so we can later pull the relevant articles from your database.


    Then you would build a simple database query that looks up data from your user_favorites table based on the user_id of the current user. From this data, you use the article_id data to query your articles table.

    Now have a go and post back if you need help.

    Comment

    • ilya Kraft
      New Member
      • Jan 2011
      • 134

      #3
      Hi,

      Thnx, Alright I tried it here is what I've got so far.

      Code:
      //I've gathered users I'd before and it is in $id variable
      //Now I select information from user_favorites
      
      sql = ("SELECT * FROM user_favorites WHERE user_id=$id");
      while($row = mysql_fetch_array($sql)){ 
      $article_id=$row_["article_id"];
      }
      
      //Then I select articles that have id that is equal
      //to $article_id
      
      sql2 = ("SELECT * FROM articles WHERE id=$article_id");
      
      //And then I just use while loop to gather info
      Is this right approach to this? I haven't tried it yet, because I need to complete different areas before I can do this one ))) I asked question to be prepared )))

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        The second SQL query would have to be part of your first while loop, otherwise, you'll only have to last article id.

        Code:
        loop over each article id where user_id = $id
           select article from articles table where article id matches
        end loop
        That is the pseudo-code.

        Comment

        • ilya Kraft
          New Member
          • Jan 2011
          • 134

          #5
          Wooh, Ok so it will have to look like this?

          Code:
          sql = ("SELECT * FROM user_favorites WHERE user_id=$id");
          while($row = mysql_fetch_array($sql)){ 
          $article_id=$row_["article_id"];
          sql2 = ("SELECT * FROM articles WHERE id=$article_id");
          
          //And I will initialize variables like $article_body, $title right after second query, is it right?
          
          }

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            You would use the data from the second query within that loop, yes.

            Comment

            Working...