How to display recent(latest post) as the first on a list of a blog

Home / Uncategorized / How to display recent(latest post) as the first on a list of a blog

Question:
Hello everyone I need your help on the php code below. I am trying to display recent(latest post) as the first on a list. I don’t know how to use ORDER BY timestamp Please help. Here is my code below:
fetch_all();
?>



Blogger

B

  1. ">



    popsted

Admin


Below is my Article class code, please help me on how to place/insert the ORDER BY timestamp query. Thank you!
prepare("SELECT * FROM articles");
$query->execute();

return $query->fetchAll();
}

public function fetch_data($article_id){
global $pdo;

$query = $pdo->prepare("SELECT * FROM articles WHERE article_id = ?");
$query->bindValue(1, $article_id);
$query->execute();

return $query->fetch();
}
}

?>


Answer:
Add an ORDER BY to your SQL-query:$query = $pdo->prepare("SELECT * FROM articles ORDER BY article_timestamp DESC");

The above tells MySQL to order the results by the article_timestamp column in a DESCending order. If you want the oldest first, it’s ASC as in ASCending.

You can read more in the manual about sorting columns.
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *