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!
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
}
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.
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 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