php - How to display recent(latest post) as the first on a list of a blog -
hello need on php code below. trying display recent(latest post) first on list. don't know how use order timestamp please help. here code below:
<!doctype html> <?php include_once('includes/connection.php'); include_once('includes/article.php'); $article = new article; $articles = $article->fetch_all(); ?> <html> <head> <title>blogger</title> <link rel="stylesheet" href="assets/style.css"> </head> <body> <div class="container"> <a href="index.php" id="logo">b</a> <ol> <?php foreach ($articles $article){?> <li><a href="article.php?id=<?php echo $article['article_id'];>"> <?php echo $article['article_title'];?> </a> - <small> popsted <?php echo date('l js', $article['article_timestamp']);> </small> </li> <?php }?> </ol> <small><a href="admin">admin</a></small> </div> </body> </html>
below article class code, please me on how place/insert order timestamp query. thank you!
<?php class article{ public function fetch_all(){ global $pdo; $query = $pdo->prepare("select * articles"); $query->execute(); return $query->fetchall(); } public function fetch_data($article_id){ global $pdo; $query = $pdo->prepare("select * articles article_id = ?"); $query->bindvalue(1, $article_id); $query->execute(); return $query->fetch(); } } ?>
add order by
sql-query:
$query = $pdo->prepare("select * articles order article_timestamp desc");
the above tells mysql order results article_timestamp
column in descending order. if want oldest first, it's asc
in ascending.
you can read more in manual sorting columns.
Comments
Post a Comment