Trier et Afficher les résultats d'une requête

Fermé
tridam23 Messages postés 99 Date d'inscription mercredi 5 novembre 2014 Statut Membre Dernière intervention 11 juin 2021 - 18 sept. 2015 à 13:37
tridam23 Messages postés 99 Date d'inscription mercredi 5 novembre 2014 Statut Membre Dernière intervention 11 juin 2021 - 21 sept. 2015 à 14:26
J'ai besoin de votre aide sur le sujet suivant pour mon appli de gestion d'inscription d'élèves pour une école
ma base de donnée contient les noms, sexe, classe (1ere A, 1ere B, 3eme C,etc), nom des parents...
Je veux donner la possibilité d'afficher l'ensemble des élèves, triés par classe.
Par contre, je veux qu'aparaissent au dessus de chaque groupe la classe des élèves d'un même niveau


Exemple :

1ère A
Eleve 55
Eleve 10
Eleve 07

2ème Maternelle
Eleve 01
Eleve 02
Eleve 03

3ème B
Eleve 06
Eleve 13
Eleve 22
etc...

Et là, pour faire cette boucle d'affichage, je cale.
Help ! :euh:

<?php
session_start();
if (!(isset($_SESSION['NIV']))){
    header("location:index.html");
}

require_once("connection.php");
$order = isset($_GET['order'])?$_GET['order']:'ASC';
$req = "select * from ELEVES
           ORDER BY CLASSE $order ";
$rs = mysql_query($req) or die(mysql_error());

$thOrder = $order == 'ASC'? 'DESC' : 'ASC';

?>


<html>
<head>
    <meta charset="utf-8">
<title>Affichage des élèves</title>
<link href="stle.css" rel="stylesheet" type="text/css">
<script type="text/javascript">
function imprimer(url){
  options = "Width=700,Height=700" ;
  window.open( url, "Impression", options ) ;
}
</script>
</head>
<body>
<table width="500" border="0">
    <tr>
        <td><a href="saisieEleve.html"><br>Inscrire un élève</a></td>
        <td><a href="ChercherEleves.php"><br>Rechercher un élève</a></td>
        <td><a href="index.html"><br>Accueil</a></td>
    </tr>
</table>
<br>
<table border="1" width="50%">
    <tr>
        <th></th>
        <th>PHOTO</th>
        <th><a href="?order=<?php echo $thOrder; ?>" >CLASSE </a></th>
        <th>SECTION</th>
       <th>GROUPEMENT</th>
        <th>INSCRIPTION</th>
        <th>NOM</th>
        <th>PRENOM</th>
        <th>POSTNOM</th>
        <th>NAISSANCE</th>
        <th>SEXE</th>
        <th>NATIONALITE</th>
        <th>ADRESSE</th>
        <th>PERE</th>
        <th>TEL PERE</th>
        <th>ADRESSE DOMICILE</th>
    </tr>
    <?php while ($ET = mysql_fetch_assoc($rs)) { ?>
        <tr>
            <td><a href="detailEleve.php?code=<?php echo $ET['CODE'] ;?>" class="bouton" 
onclick="imprimer('detailEleve.php?code=<?php echo $ET['CODE'];?>') ;return false;">Imprimer</a></td>
            <td><img src="images/<?php echo($ET['PHOTO']) ?>" width="90" height="113"</td>
            <td><?php echo($ET['CLASSE']) ?></td>
            <td><?php echo($ET['SECTION']) ?></td>
            <td><?php echo($ET['GROUPEMENT']) ?></td>
            <td><?php echo($ET['INSCRIPTION']) ?></td>
            <td><?php echo($ET['NOM']) ?></td>
            <td><?php echo($ET['PRENOM']) ?></td>
            <td><?php echo($ET['POSTNOM']) ?></td>
            <td><?php echo($ET['NAISSANCE']) ?></td>
            <td><?php echo($ET['SEXE']) ?></td>
            <td><?php echo($ET['NATIONALITE']) ?></td>
            <td><?php echo($ET['ADRESSE']) ?></td>
            <td><?php echo($ET['PERE']) ?></td>
            <td><?php echo($ET['TELPERE']) ?></td>
            <td><?php echo($ET['ADRESSE1']) ?></td>
            <?php if ($_SESSION['NIV'] == 0) { ?>
                <td><a href="supprimerEleve.php?code=<?php echo($ET['CODE']) ?>" class="bouton">Supprimer</a></td>
                <td><a href="editEleve.php?code=<?php echo($ET['CODE']) ?>" class="bouton">Editer</a></td>
            <?php } ?>
        </tr>
    <?php } ?>
</table>
</body>
</html>

1 réponse

Pitet Messages postés 2826 Date d'inscription lundi 11 février 2013 Statut Membre Dernière intervention 21 juillet 2022 524
18 sept. 2015 à 17:05
Salut,

Puisque ton résultat est trié par classe, tu peux regrouper les élèves par classes comme ceci :
<?php 
$classeCourante = '';

while ($ET = mysql_fetch_assoc($rs)) {
	if ($classeCourante !== $ET['CLASSE']) {
		$classeCourante = $ET['CLASSE'];
		
		echo $ET['CLASSE'];
	}
	
	echo $ET['NOM'];
}


Bonne journée
0
Je dois ajouter ca dans mon code ou dans un nouveau fichier?
0
Je dois lajouter dans mon code ci haut ou dans un nouveau fichier svp?
0
tridam23 Messages postés 99 Date d'inscription mercredi 5 novembre 2014 Statut Membre Dernière intervention 11 juin 2021
19 sept. 2015 à 01:37
quand j'ajoute votre code dans le mien, il efface toute les données enregistrées...
0
Pitet Messages postés 2826 Date d'inscription lundi 11 février 2013 Statut Membre Dernière intervention 21 juillet 2022 524
21 sept. 2015 à 13:56
Ce code n'est qu'un exemple. Tu dois adapter ta boucle while en fonction de cet exemple.
0
tridam23 Messages postés 99 Date d'inscription mercredi 5 novembre 2014 Statut Membre Dernière intervention 11 juin 2021 > Pitet Messages postés 2826 Date d'inscription lundi 11 février 2013 Statut Membre Dernière intervention 21 juillet 2022
21 sept. 2015 à 14:26
je en suis pas très pro en php. je suis apprenti. c'est un peu du chinois ce que vous me dite lol.
0