Order of posts when I consult the database of my CMS

2

I'm developing a Site with CMS. It works perfectly just that you would like the last posts to be at the top of the page. What is happening now is that the last posts go to the bottom of the page just wanted the order to be opposite.

PHP Class "Article"

<?php
class Article {
  public function fetch_all() {
    global $pdo;

    $query = $pdo->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();
  }
}
?>

index.php

<?php
include_once('includes/connection.php');
include_once('includes/article.php');

$article = new Article;
$articles = $article->fetch_all();
?>
<html>
<head>
    <title>CMS Tutorial</title>
    <link rel ="stylesheet" href="assets/style.css"/>
</head>
<body>
  <div class="container">
    <a href="index.php" id ="logo">CMS</a>
    <ol>
      <?php foreach ($articles as $article) { ?>
      <li>
      <?php echo $article['article_title']; ?>
        - <small>
            posted <?php echo date('l jS', $article['article_timestamp']); ?>
          </small>
      </li>
      <p><?php echo $article['article_content']; ?></p>
      <?php }?>         
    </ol>
    <br>
    <small><a href="admin">admin</a></small>
  </div>
</body>
</html>
    
asked by anonymous 06.03.2014 / 16:48

3 answers

3

As you can see from your question and based on the comments you've already left in the existing answers, your articles table contains a field with the name article_timestamp .

Assuming that the field does indeed contain Unix timestamp data, a datetime or date , and that the saved value refers to the date of your registration, you can query the following form:

$query = $pdo->prepare("SELECT * FROM articles ORDER BY article_timestamp DESC");
/*                      └──┬───┘ └─────┬─────┘ └──────────────┬──────────────┘     */
/*                         ↓           ↓                      ↓                    */
/*                    Seleccionar   da tabela         ordenado pelo campo          */
/*                       tudo       articles      article_timestamp de forma       */
/*                                                       descendente               */

Where you are saying that you want to collect everything from the articles table but sorted by the article_timestamp field in descending order.

Not related

Not related to the problem in question, but I noticed that in markup of HTML in your question, you have a <p></p> out of context, ie after closing li but before close the ol you are applying a paragraph.

This situation is not allowed, you should put the paragraph inside li , before closing it:

<ol>
  <?php
  foreach ($articles as $article) {
    echo '
    <li>
      '.$article["article_title"].'
      - <small> posted '.date("l jS", $article["article_timestamp"]).'</small>
      <p>'.$article["article_content"].'</p>
    </li>';
  }
  ?>         
</ol>

The way you have it. although it works well in some browsers because they ignore that lapse, does not work well in all, particularly the older ones.

    

06.03.2014 / 17:43
3

You are using which CMS. Of how you're showing. Your query should look like this if you are using PHP PDO:

public function fetch_all() {
  $sql  = "SELECT * FROM mytable";
  $stmt = Connection::prepare( $sql );
  $stmt->execute();

  return $stmt->fetchAll();
}

Add in your SQL the next part to get them in descending order.

public function fetch_all() {
  $sql  = "SELECT * FROM articles ORDER BY article_id DESC";
  $stmt = Connection::prepare( $sql );
  $stmt->execute();

  return $stmt->fetchAll();
}
    
06.03.2014 / 17:09
0

Add ORDER BY "id" DESC eg to your SQL query that takes POSTS from the database.

    
06.03.2014 / 16:52