Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.58 KB | None | 0 0
  1. [code]function getPublishedPostsByCategory($category_id) {
  2.     global $conn;
  3.     $sql = "SELECT * FROM posts ps
  4.             WHERE ps.id IN
  5.             (SELECT pt.post_id FROM post_category pt
  6.                 WHERE pt.category_id=$category_id GROUP BY pt.post_id
  7.                 HAVING COUNT(1) = 1)";
  8.     $result = mysqli_query($conn, $sql);
  9.     // fetch all posts as an associative array called $posts
  10.     $posts = mysqli_fetch_all($result, MYSQLI_ASSOC);
  11.  
  12.     $final_posts = array();
  13.     foreach ($posts as $post) {
  14.         $post['category'] = getPostCategory($post['id']);
  15.         array_push($final_posts, $post);
  16.     }
  17.     return $final_posts;
  18. }[/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement