Advertisement
Guest User

Wut?

a guest
Aug 16th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.19 KB | None | 0 0
  1. Functions
  2.  
  3. /* * * * * * * * * * * * * * * *
  4. * Returns all posts under a category
  5. * * * * * * * * * * * * * * * * */
  6. function getPublishedPostsByCategory($category_id) {
  7.     global $conn;
  8.     $sql = "SELECT * FROM posts ps
  9.             WHERE ps.id IN
  10.             (SELECT pt.post_id FROM post_category pt
  11.                 WHERE pt.category_id=$category_id GROUP BY pt.post_id
  12.                 HAVING COUNT(1) = 1)";
  13.     $result = mysqli_query($conn, $sql);
  14.     // fetch all posts as an associative array called $posts
  15.     $posts = mysqli_fetch_all($result, MYSQLI_ASSOC);
  16.  
  17.     $final_posts = array();
  18.     foreach ($posts as $post) {
  19.         $post['category'] = getPostCategory($post['id']);
  20.         array_push($final_posts, $post);
  21.     }
  22.     return $final_posts;
  23. }
  24. /* * * * * * * * * * * * * * * *
  25. * Returns category name by category id
  26. * * * * * * * * * * * * * * * * */
  27. function getCategoryNameById($id)
  28. {
  29.     global $conn;
  30.     $sql = "SELECT name FROM categories WHERE id=$id";
  31.     $result = mysqli_query($conn, $sql);
  32.     $category = mysqli_fetch_assoc($result);
  33.     return $category['name'];
  34. }
  35. ?>
  36.  
  37. Page markup
  38.  
  39. <?php include('config.php'); ?>
  40. <?php include('includes/public_functions.php'); ?>
  41. <?php include('includes/head.php'); ?>
  42. <?php
  43.     // Get posts under a particular topic
  44.     if (isset($_GET['category'])) {
  45.         $category_id = $_GET['category'];
  46.         $posts = getPublishedPostsByCategory($category_id);
  47.     }
  48. ?>
  49.     <title>LifeBlog | Home </title>
  50. </head>
  51. <body>
  52. <div class="container">
  53. <!-- Navbar -->
  54.     <?php include( ROOT_PATH . '/includes/nav.php'); ?>
  55. <!-- // Navbar -->
  56. <!-- content -->
  57. <div class="content">
  58.     <h2 class="content-title">
  59.         Articles on <u><?php echo getCategoryNameById($category_id); ?></u>
  60.     </h2>
  61.     <hr>
  62.          
  63.      <!-- gets posts from db -->
  64.      <?php foreach ($posts as $post): ?>  
  65.         <div class="card md-8">
  66.        <div class="card-body">
  67.        
  68.   <!-- image from db -->    
  69.  
  70.    <img src="<?php echo BASE_URL . '/img/' . $post['image']; ?>" class="post_image" alt="post image" style="width:100%;margin-bottom:0">
  71.                     </div>
  72.                 </div>
  73.             </a>
  74.         </div>
  75.     <?php endforeach ?>
  76. </div>
  77. <!-- // content -->
  78. </div>
  79. <!-- // container -->
  80.  
  81. <!-- Footer -->
  82.     <?php include( ROOT_PATH . '/includes/footer.php'); ?>
  83. <!-- // Footer -->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement