Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Functions
- /* * * * * * * * * * * * * * * *
- * Returns all posts under a category
- * * * * * * * * * * * * * * * * */
- function getPublishedPostsByCategory($category_id) {
- global $conn;
- $sql = "SELECT * FROM posts ps
- WHERE ps.id IN
- (SELECT pt.post_id FROM post_category pt
- WHERE pt.category_id=$category_id GROUP BY pt.post_id
- HAVING COUNT(1) = 1)";
- $result = mysqli_query($conn, $sql);
- // fetch all posts as an associative array called $posts
- $posts = mysqli_fetch_all($result, MYSQLI_ASSOC);
- $final_posts = array();
- foreach ($posts as $post) {
- $post['category'] = getPostCategory($post['id']);
- array_push($final_posts, $post);
- }
- return $final_posts;
- }
- /* * * * * * * * * * * * * * * *
- * Returns category name by category id
- * * * * * * * * * * * * * * * * */
- function getCategoryNameById($id)
- {
- global $conn;
- $sql = "SELECT name FROM categories WHERE id=$id";
- $result = mysqli_query($conn, $sql);
- $category = mysqli_fetch_assoc($result);
- return $category['name'];
- }
- ?>
- Page markup
- <?php include('config.php'); ?>
- <?php include('includes/public_functions.php'); ?>
- <?php include('includes/head.php'); ?>
- <?php
- // Get posts under a particular topic
- if (isset($_GET['category'])) {
- $category_id = $_GET['category'];
- $posts = getPublishedPostsByCategory($category_id);
- }
- ?>
- <title>LifeBlog | Home </title>
- </head>
- <body>
- <div class="container">
- <!-- Navbar -->
- <?php include( ROOT_PATH . '/includes/nav.php'); ?>
- <!-- // Navbar -->
- <!-- content -->
- <div class="content">
- <h2 class="content-title">
- Articles on <u><?php echo getCategoryNameById($category_id); ?></u>
- </h2>
- <hr>
- <!-- gets posts from db -->
- <?php foreach ($posts as $post): ?>
- <div class="card md-8">
- <div class="card-body">
- <!-- image from db -->
- <img src="<?php echo BASE_URL . '/img/' . $post['image']; ?>" class="post_image" alt="post image" style="width:100%;margin-bottom:0">
- </div>
- </div>
- </a>
- </div>
- <?php endforeach ?>
- </div>
- <!-- // content -->
- </div>
- <!-- // container -->
- <!-- Footer -->
- <?php include( ROOT_PATH . '/includes/footer.php'); ?>
- <!-- // Footer -->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement