Trier une boucle dans un tableau PHP

Fermé
lehman - 25 mai 2010 à 13:26
 lehman - 25 mai 2010 à 14:19
Bonjour,
J'aimerai trier par date des données récupérées depuis la base de donnée dans un tableau .

while($r = $q->fetch()) {
echo '<tr bgcolor="#FED1E9"><td><input title="Selectionner" type="checkbox" name="delete[]" value="'. $r['cle'] .'" /></td><td><a href="'. $r['lien'] .'">'. substr($r['lien'], 0, 30) .'...</a></td><td><input title="Normal" size="40" readonly="readonly" value="'. URL_SITE . $r['cle'] .'" name="html"><br /><input title="HTML" size="40" readonly="readonly" value="<a href=\''. URL_SITE . $r['cle'] .'\'>'. URL_SITE . $r['cle'] .'</a>" name="html"><br /><input title="BBcode" size="40" readonly="readonly" value="[url='. URL_SITE . $r['cle'] .'>'. URL_SITE . $r['cle'] .'/url" name="html"></td><td>'. $r['gain'] .'$</td><td>'. $r['vues'] .'</td><td>'. $r['day'] .'/'. $r['month'] .'/'. $r['year'] .'</td><td><a href="./links-del-'. $r['cle'] .'" onclick="return confirm(\''. $LOCALE['links']['sure'] .'\');"><img
alt="'.$LOCALE['links']['delete'].'" src="images/delete.png"></a></td></tr>';
}

A voir également:

3 réponses

Neliel Messages postés 6146 Date d'inscription jeudi 9 juillet 2009 Statut Contributeur Dernière intervention 20 mars 2017 1 692
25 mai 2010 à 13:31
Bonjour,

Ouah! Ta boucle est compliquée... Comme tu t'y retrouves ?

Pour récupérer les données d'une base trié par date, dans la mesure où tu as un champ date dans ta table, il suffit de rajouter ORDER BY <champ_date> à la fin de la requête SQL...

Par exemple, tu as la table :

Table1(Clé, Nom, Date_Nom);

Si tu veux récupéré tous les champs de ta table trié par date, tu fais:

"select * from Table1 order by Date_nom"

0
voici la table


$q = $sql->query('SELECT cle, lien, vues, gain, DAYOFMONTH(date) AS day, MONTH(date) AS month, YEAR(date) AS year FROM liens WHERE user = '. $USER['id']);
if($q->rowCount()) {
    echo '<form id="myform" action="" method="post" onsubmit="return confirm(\''. $LOCALE['links']['sure'] .'\');"><table style="width:100%; text-align:center;">
	<tr bgcolor="#e9f0fa"><th style="width:5px"></th><th>'. $LOCALE['links']['link'] .'</th><th>'. $LOCALE['links']['short link'] .'</th><th>'. $LOCALE['links']['gain'] .'</th><th>'. $LOCALE['links']['vues'] .'</th><th>'. $LOCALE['links']['date'] .'</th><th>'. $LOCALE['links']['actions'] .'</th></tr>';
    while($r = $q->fetch()) {
	echo '<tr bgcolor="#FED1E9"><td><input title="Selectionner" type="checkbox" name="delete[]" value="'. $r['cle'] .'" /></td><td><a href="'. $r['lien'] .'">'. substr($r['lien'], 0, 30) .'...</a></td><td><input title="Normal" size="40" readonly="readonly" value="'. URL_SITE . $r['cle'] .'" name="html"><br /><input title="HTML" size="40" readonly="readonly" value="<a href=\''. URL_SITE . $r['cle'] .'\'>'. URL_SITE . $r['cle'] .'</a>" name="html"><br /><input title="BBcode" size="40" readonly="readonly" value="[url='. URL_SITE . $r['cle'] .'>'. URL_SITE . $r['cle'] .'/url" name="html"></td><td>'. $r['gain'] .'$</td><td>'. $r['vues'] .'</td><td>'. $r['day'] .'/'. $r['month'] .'/'. $r['year'] .'</td><td><a href="./links-del-'. $r['cle'] .'" onclick="return confirm(\''. $LOCALE['links']['sure'] .'\');"><img
alt="'.$LOCALE['links']['delete'].'" src="images/delete.png"></a></td></tr>';

}


Je fé koi
0
Bonjour,
Voici le code

$q = $sql->query('SELECT cle, lien, vues, gain, DAYOFMONTH(date) AS day, MONTH(date) AS month, YEAR(date) AS year FROM liens WHERE user = '. $USER['id']);
if($q->rowCount()) {
echo '<form id="myform" action="" method="post" onsubmit="return confirm(\''. $LOCALE['links']['sure'] .'\');"><table style="width:100%; text-align:center;">
<tr bgcolor="#e9f0fa"><th style="width:5px"></th><th>'. $LOCALE['links']['link'] .'</th><th>'. $LOCALE['links']['short link'] .'</th><th>'. $LOCALE['links']['gain'] .'</th><th>'. $LOCALE['links']['vues'] .'</th><th>'. $LOCALE['links']['date'] .'</th><th>'. $LOCALE['links']['actions'] .'</th></tr>';
while($r = $q->fetch()) {
echo '<tr bgcolor="#FED1E9"><td><input title="Selectionner" type="checkbox" name="delete[]" value="'. $r['cle'] .'" /></td><td><a href="'. $r['lien'] .'">'. substr($r['lien'], 0, 30) .'...</a></td><td><input title="Normal" size="40" readonly="readonly" value="'. URL_SITE . $r['cle'] .'" name="html"><br /><input title="HTML" size="40" readonly="readonly" value="<a href=\''. URL_SITE . $r['cle'] .'\'>'. URL_SITE . $r['cle'] .'</a>" name="html"><br /><input title="BBcode" size="40" readonly="readonly" value="[url='. URL_SITE . $r['cle'] .'>'. URL_SITE . $r['cle'] .'[/url]" name="html"></td><td>'. $r['gain'] .'$</td><td>'. $r['vues'] .'</td><td>'. $r['day'] .'/'. $r['month'] .'/'. $r['year'] .'</td><td><a href="./links-del-'. $r['cle'] .'" onclick="return confirm(\''. $LOCALE['links']['sure'] .'\');"><img
alt="'.$LOCALE['links']['delete'].'" src="images/delete.png"></a></td></tr>';
}

Je dois faire quoi STP ?
Configuration: Windows XP / Firefox 3.5.3
0