Afficher 5 articles maximum

Par Yubo, il y a 11 ans


Bonsoir,
cela fait plusieurs jours que je cherche comment afficher maximum 5 articles

Voici mon code:

<?php $query = $db->query('SELECT TOP 10 * FROM articles ORDER BY id DESC'); date("H:i:s"); while ($post = $query->fetch()) { date_default_timezone_set('Europe/Paris'); setlocale(LC_TIME, 'fr_FR.UTF8', 'fr.UTF8', 'fr_FR.UTF-8', 'fr.UTF-8'); $time = strtotime($post['date']); $myFormatForView = date("l j F Y, H:i", $time); echo '<div class="list-group"> <div class="list-group-item active"> <h4 class="list-group-item-heading">' . $post["titre"] . '</h4> <p class="list-group-item-text"><i style="color:white;">Par ' . $post["auteur"] . ' | Publié le ' . $myFormatForView . '</i></p> </div> <div class="list-group-item"> <p class="list-group-item-text">' . $post['contenu'] . '</p> </div> </div> '; } ?>

Merci beaucoup!

11 réponses

tom49, il y a 11 ans

Salut

Rajoute un "LIMIT 0,5" à ta requete pour récuperer les cinq dernieres entrées

Yubo, il y a 11 ans

ça ne fonctionne pas :(

Carouge10, il y a 11 ans

Bonsoir,

Suffit de remplacer TOP 10 par TOP 5 et le tour est joué.

Au passage, revoyer votre boucle while, il n'y a pas besoin de faire un date_default_timezone_set à chaque fois.

Yubo, il y a 11 ans

var dump de qu'elle variable? $query?

Yubo, il y a 11 ans

Le top ne fonctionne pas non plus, ... Et voici le var dump de query *object(PDOStatement)#2 (1) { ["queryString"]=> string(49) "SELECT FROM articles ORDER BY id DESC LIMIT 0,5" }**

Carouge10, il y a 11 ans

@Brandon-xprodeur : Je viens de découvrir sont utiliter, ça à l'air sympa.

Carouge10, il y a 11 ans

Ceci devrait être bon:

<?php date_default_timezone_set('Europe/Paris'); setlocale(LC_TIME, 'fr_FR.UTF8', 'fr.UTF8', 'fr_FR.UTF-8', 'fr.UTF-8'); $query = $db->query('SELECT TOP 5 * FROM articles ORDER BY id DESC'); while ($post = $query->fetch()) { $time = strtotime($post['date']); $myFormatForView = date("l j F Y, H:i", $time); ?> <div class="list-group"> <div class="list-group-item active"> <h4 class="list-group-item-heading"><?= $post["titre"]; ?></h4> <p class="list-group-item-text"><i style="color:white;">Par <?= $post["auteur"]; ?> | Publié le <?= $myFormatForView; ?></i></p> </div> <div class="list-group-item"> <p class="list-group-item-text"><?= $post['contenu']; ?></p> </div> </div> <?php }
Yubo, il y a 11 ans

@Carouge10: Il me retourne une erreur

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '5 FROM articles ORDER BY id DESC' at line 1' in C:\xampp\htdocs\views\index.view.php:21 Stack trace: #0 C:\xampp\htdocs\views\index.view.php(21): PDO->query('SELECT TOP 5 ...') #1 C:\xampp\htdocs\index.php(5): require('C:\xampp\htdocs...') #2 {main} thrown in C:\xampp\htdocs\views\index.view.php on line 21

Yubo, il y a 11 ans

16 environs

Carouge10, il y a 11 ans

Votre 1ère requête fonctionne, mais pas la mienne ?

Yubo, il y a 11 ans

Avec le LIMIT 5 ça fonctionne maintenant! Merci d'avoir refait mon code tout sale x)