php - A better way to check If a user bookmarked a post -


I am facilitating a small bookmark for a site, where a user can bookmark a post when a user is someone Bookmarks the post, it is added to the table postmark .

The way I am currently presenting the main post from the table is Post and then every postId = id and Select count for userId = currentUser from the table postmarks . Is there a better and more efficient way to do this instead of running two separate statements? The way I am doing it is quite inefficient.

My code is all below because it has not been implemented yet, so I can make changes.

Post

  "id" "PostTitle" "1" "Heading 1" "2" "Heading 2" "3" "Title 3 "" 4 "" Title 4 "" 5 "" Title 5 "" 6 "" Title 6 "" 7 "" Title 7 "" 8 "" Title 8 "" 9 "" Title 9 "" 10 "" Title 10 "  

Postmasters

 " id "" postId "" postTitle "userId" "addDate" "1" "1 "" Title 1 "" 1 "" 2014-04-22 09:57:35 "" 2 "" 2 "" Heading 2 "" 1 "" 2014-04 -22 09:58:10 "" 3 "" 3 "" Title 3 "" 1 "" 2014-04-22 09:58:12 "" 4 "" 4 "" Title 4 "" 1 "" 2014-04 -22 09:58:25 "" 5 "" 5 "" Title 5 "" 1 "" 2014-04-22 09:58:37 "" 6 "" 1 "" title 1 "" 2 "" 2014-04 -22 09:59:10 "" 7 "" 2 "" Heading 2 "" 2 "" 2014-04-22 09:59:20 "" 8 "" 1 "" Title 1 "" 3 "" 2014-04-22 09:59:33 "" 9 "" 2 "" Heading 2 "" 3 "" 2014-04-22 09:59:45 "" 10 "" 3 "" Title 3 "" 3 "" 2014-04-22 09:59:53 "  

My code

  $ userId = 1; $ PageData = 'Choose ID, Post Title LIMIT 10'; $ Stmt = $ conn- & gt; Ready ($ page data); $ Stmt- & gt; Executed (); $ Stmt- & gt; BindColumn (1, $ one); $ Stmt- & gt; BindColumn (2, $ b); $ Post = $ stmt-> Punctiquity (); If ($ posts> = 1) {while ($ rows = $ stm-> get (pdo: FETCH_BOUND)) {ebb $ a. '' $ H .; // bookmarks $ bookmarks = "Select Postmark from Postmark (post id) where postId = '". $ A. "'And userId ='" $ userId. "'"; $ Stmtx = $ conn- & gt; Ready ($ bookmark); $ Stmtx- & gt; Executed (); $ Stmtx- & gt; BindColumn (1, $ one); $ Stmtx- & gt; Bring (); If ($ a == 1) {echo 'bookmarks & lt; Br> & # 39; & # 39; } Else {echo 'Bookmarked Not & lt; Br> & # 39; & # 39; } // find the markup ending ends}} other {echo 'nothing to perform'; }   

Result

  1 title 1 bookmarked 2 title 2 bookmarked 3 title 3 bookmark 4 title 4 bookmark 5 Title 5 Bookmarks 6 Title 6 Bookmark No 7 Title 7 Bookmark No 8 Title 8 Bookmark No 9 Title 9 Bookmark No 10 Title 10 Not Bookmarked    

I updated your SQL query with an addition. If the current user has bookmarked the page, now you should be able to get a bull PostDate = 'SELECT id, postTitle, IF (counting (separate postMarks.userId)> 0, true, false) postmark mail posts by mail as posts by post . Id = postMarks.postId and userId = ". $ UserId." Posted by Group. IDLIT 10 ';

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -