Classement 1er,2ème

Résolu
keunene Messages postés 72 Date d'inscription samedi 5 avril 2014 Statut Membre Dernière intervention 10 mars 2023 - 16 déc. 2014 à 13:13
keunene Messages postés 72 Date d'inscription samedi 5 avril 2014 Statut Membre Dernière intervention 10 mars 2023 - 17 déc. 2014 à 09:45
Bonjour,

je voudrais faire un classement par point mais ce qui me bloque c'est donner le même rang à deux personnes qui ont le même point.
voici mon code:

<?php
mysql_connect("localhost", "root", "");
mysql_select_db("forumu");
function classement(){
//if(isset($_GET['id'])) $id = urldecode($_GET['id']);

$results = array();
$sql = "SELECT nomcl,pointcl FROM classement ORDER BY pointcl desc";
$query = mysql_query($sql) or die("error");
while($row = mysql_fetch_assoc($query)){
$results[] = $row;
}
return $results;
}

?>
<?php

$rang=0;
$classements = classement();
foreach($classements as $classement){
$rang++;
?>
<table width="914" border="1" >
<tr style="font-family:Georgia, 'Times New Roman', Times, serif; font-size:14px; background-color:#CDCDCD; color:#000">
<td width="73" align="center"></td>
<td width="48" align="center"><?php echo $classement["nomcl"]; ?></td>
<td width="114" align="center"><?php echo $classement["pointcl"]; ?></td>

<td width="46" align="center"><?php echo $rang; ?> </td>

</tr>
</table>

<?php
}
?>

merci d'vavance

2 réponses

jordane45 Messages postés 38144 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 21 avril 2024 344
Modifié par jordane45 le 17/12/2014 à 00:10
Bonjour,

Quelque chose de ce style devrait faire l'affaire :

<?php
mysql_connect("localhost", "root", "");
mysql_select_db("forumu");
function classement(){
//if(isset($_GET['id'])) $id = urldecode($_GET['id']); 
 $results = array();
 $sql = "SELECT  nomcl
                ,pointcl 
            FROM classement 
            ORDER BY pointcl DESC";
 $query = mysql_query($sql) or die("error <br>".$sql);
 while($row = mysql_fetch_assoc($query)){
  $results[] = $row;
   }
   return $results;
}  

$Rang=0;
$PointsPrec = ''
$Points='';
$classements = classement();

 foreach($classements  as $classement){
  // Récupération des valeurs
  $Points = isset($classement["pointcl"])?$classement["pointcl"]:'';
  $nomcl = isset($classement["nomcl"])?$classement["nomcl"]:'';
  
  // Ajout d'un test pour comparer les "nouveaux" points
  // au points précédent.. pour savoir si on incrémente ou pas
 if($Points != $PointsPrec)
     $Rang++;
  }
?>
 <table width="914" border="1" >
  <tbody>
     <tr style="font-family:Georgia, 'Times New Roman', Times, serif; font-size:14px; background-color:#CDCDCD; color:#000">
      <td width="73" align="center"></td>
      <td width="48" align="center">
        <?php echo $nomcl;  ?>
      </td>
      <td width="114" align="center">
        <?php echo $points; ?>
      </td>
      <td width="46" align="center">
        <?php echo $rang; ?>
      </td>      
     </tr>
   </tbody>
 </table>
 
 <?php
 //On stocke la nouvelle valeur de $Points dans la variable $PointsPrec
 $PointsPrec = $Points;
 } // Fin de la boucle

?>


Avant de poser une question, merci de lire la charte du site.
Cordialement, Jordane
0
keunene Messages postés 72 Date d'inscription samedi 5 avril 2014 Statut Membre Dernière intervention 10 mars 2023
17 déc. 2014 à 09:45
merci infiniment
0
Rejoignez-nous