Aide pour un code PHP/MySQL
Bonsoir,
Après plusieurs heures à caser ma tête la dessus je me retourne vers vous espérant trouver une solution optimale à mon problème.
Voici mon code :
Code:
1 2 3 4 5 6 7 8 9 10
| $tableau=mysql_query("SELECT id,categorie,section,format FROM ma_table where 1=1 ORDER BY categorie desc, section, format, id desc");
$catPrecedent = "" ;
while($datamenu=mysql_fetch_assoc($tableau))
{
if ( $datamenu["categorie"] != $catPrecedent ) {
echo 'Categorie : '.$datamenu["categorie"].'<br />';
}
echo 'Section : '.$datamenu["section"].' - Format : '.$datamenu["format"].' - ID : '.$datamenu["id"].'<br />';
$catPrecedent = $datamenu["section"] ;
} |
Ce code permet de trier les enregistrements de la table de cette façon :
Catgeorie : A
Section : S1 – Format : F1 – ID : 15
Section : S1 – Format : F1 – ID : 8
Section : S1 – Format : F1 – ID : 7
Section : S1 – Format : F1 – ID : 6
Section : S1 – Format : F1 – ID : 1
Section : S1 – Format : F2 – ID : 2
Section : S1 – Format : F3 – ID : 10
Section : S2 – Format : F1 – ID : 4
Section : S2 – Format : F1 – ID : 11
Section : S2 – Format : F1 – ID : 3
Section : S3 – Format : F2 – ID : 14
Section : S3 – Format : F2 – ID : 13
Section : S3 – Format : F2 – ID : 5
Section : S3 – Format : F3 – ID : 12
Section : S3 – Format : F3 – ID : 9
Catgeorie : B
....................................
Cependant je veux limiter le nombre de lignes qui ont la même section et le même format pour une catégorie donnée à 2 (prendre les lignes dont ID est le plus bas)
Résultat obtenu :
Catgeorie : A
Section : S1 – Format : F1 – ID : 6
Section : S1 – Format : F1 – ID : 1
Section : S1 – Format : F2 – ID : 2
Section : S1 – Format : F3 – ID : 10
Section : S2 – Format : F1 – ID : 11
Section : S2 – Format : F1 – ID : 3
Section : S3 – Format : F2 – ID : 13
Section : S3 – Format : F2 – ID : 5
Section : S3 – Format : F3 – ID : 12
Section : S3 – Format : F3 – ID : 9
Catgeorie : B
................
Merci à vous d’avance pour votre aide.