Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Show posts descending from newest to oldest

Hi guys, i am currently making a website for a customer. And he want's to show all posts he's making from newest to oldest. And i thought peace of cake! Well.. I ran into some problems. I doesn't show anything at all. I tried doing the same thing as i did in my comment section where again, it's showing the newest and the older ones down.

This is my code: <?php $query = "SELECT * FROM posts"; $query = "ORDER BY post_author DESC "; $all_posts_query = mysqli_query($connection, $query);

            while($row = mysqli_fetch_assoc($all_posts_query)) {
            $post_id = $row['post_id'];
            $post_title = $row['post_title'];
            $post_author = $row['post_author'];
            $post_date = $row['post_date'];
            $post_image = $row['post_image'];

?> Hope you can help me out :)

Peter Hatzer
Peter Hatzer
20,837 Points

Hi Mike,

Your query needs a space between it this is probably reason why its not working. Plus am sure its pulling the $query variable as u have not concatenated it. Try this:

<?php 
$query = "SELECT * FROM posts"; 
$query . = "ORDER BY post_author DESC "; 
?>

or

<?php 
$query = "SELECT * FROM posts ORDER BY post_author DESC "; 
?>

2 Answers

Neither of them works Peter? The first one make's the posts totally dissapear, the second one is still showing newest posts in the bottom

Peter Hatzer
Peter Hatzer
20,837 Points

Apologize the first one should have a space on it like so :

<?php 
$query = "SELECT * FROM posts"; 
$query . = " ORDER BY post_author DESC "; 
?>

I just notice you ordering by author, would you not order by post_date or post_id? Or is there more then one author?

Well Peter i added your code like

$query = "SELECT * FROM posts"; $query . = " ORDER BY post_date DESC ";

And then it's giving me an HTTP ERROR 500 why?