Displaying posts by category

Hi, i’m trying to display posts by category for a blog I am building.

I’m new to php and have been following a guide

This is in my functions.php to display posts by category.

function getPublishedPostsByCategory($category_id) {

global $conn;
$sql = "SELECT * FROM posts pc
        WHERE pc.id IN
        (SELECT post_id FROM post_category  
            WHERE category_id=$category_id GROUP BY 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;

And in the page where I am displaying the posts I have

<?php // Get posts under a particular topic if (isset($_GET['category'])) { $categoy_id = $_GET['category']; $posts = getPublishedPostsByCategory($category_id); } ?>

I’m getting the error

Notice : Undefined variable: posts in /home/ttestdmor/public_html/filtered_posts.php on line 31

Warning : Invalid argument supplied for foreach() in /home/ttestdmor/public_html/filtered_posts.php on line 31

Can anyone help me fix this? Don’t understand why it is happening.

EDIT: mangaed to get it down to just this error - Invalid argument supplied for foreach()

Thanks

Simon

Hi Sieb,

To me it seems a very logic notice. Only if the if statement returns true then $posts will be initialized. Here is a better approach:

<?php

// introduce new variables that will exist for sure and give them a default value
$categoy_id = 0;
$posts = array();

if (isset($_GET['category'])) { 
    // if TRUE then overwrite the default values
    $categoy_id = $_GET['category']; 
    $posts = getPublishedPostsByCategory($category_id);
}

// now $category_id and $posts will exist for sure :slight_smile:
.... 
?>

Hi frankbeen, thanks for getting back to me and for your help.

I tried the code you posted but it prevents the page from loading.

There can be a lot of reasons for that. If the page keeps loading until you get a timeout that indicates that your application stays in a loop. If you just get a blank page you should maybe turn on php display errors. Or show some relevant code that you have so far.

I have managed to get the page to load now using your code but it isn’t displaying the posts.

Basically i’m following along to this guide: https://codewithawa.com/posts/how-to-create-a-blog-in-php-and-mysql-database---db-design

But using my own page design which works for the previous part of the guide for displaying published posts from the db.

All I have done is replace topics/post_topic in the guide with categories/post_category.

According to the guide, when I click on a category it should take me to a page that displays all posts under for that category id.

Thanks for your help by the way, i’ve tried asking elsewhere but you are the only one to reply :slight_smile:

Solved my problem.

In my code, on my blog homepage that I used to take you to the filterd posts page which displays all posts by a certain category was

<?php echo BASE_URL . filtered_posts.php?category=**' . $post['category']['id'] ?>

Which was causing the url to be org.ukfiltered_posts.php instead of org.uk/filtered_posts.php

So adding the backslash fixed it

<?php echo BASE_URL . /filtered_posts.php?category=**' . $post['category']['id'] ?>

Sponsor our Newsletter | Privacy Policy | Terms of Service