IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

PHP & Base de données Discussion :

Problème pour trier un tableau [MySQL]


Sujet :

PHP & Base de données

  1. #1
    Membre du Club
    Inscrit en
    Avril 2006
    Messages
    261
    Détails du profil
    Informations forums :
    Inscription : Avril 2006
    Messages : 261
    Points : 64
    Points
    64
    Par défaut Problème pour trier un tableau
    Bonjour jai crée un tableau avec les résultats de ma requete, le tableau affiche tout simplement le contenue de ma table Offre, sur les entetes des colonnes jai des liens, quand je clique sur un des liens, le tri croissant fonctionne, cependant le tri decroissant ne fonctionne pas, je suis bloqué depuis, hier soir, jai beau faire plei nde test ais je comprend plus la.

    J'utilise une base sous mysql.

    Pouvez vous m'aider svp

    Ps: je débute

    voila une partie de mon code

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    <?php
    include('testeurDeSession.php');
    include('connexion.php');
    if (isset($_GET['t']))
    {
    $t=$_GET['t'];
     
    if ($t=='a')
    {
    	$txt="Le client a été ajouté avec succès.";
    }
    elseif ($t=='m')
    {
    	$txt="Le client a été modifié avec succès.";
    }
    elseif ($t=='s')
    {
    	$txt="Le client a été supprimé avec succès.";
    }
    }
     
    $SQL = "SELECT * FROM offre ";
     
    $tri="";
    $d="";
    $ti="";
    $a="";
    //tri des colonnes
    if (isset($_GET['tri']))
    {$tri=$_GET['tri'];}
    elseif (isset($_GET['d']) and !empty($_GET['d']) and !$_GET['d']=="")
    {
    echo "test2 $d";
    $d=$_GET['d'];
    echo "test3 $d";
    }
    elseif (isset($_GET['ti']))
    {$t=$_GET['ti'];}
    elseif (isset($_GET['a']))
    {$a=$_GET['a'];}
     
     
    echo "tri : $tri<br\>d : $d<br\>t : $ti<br\>";
     
    if (($tri=="d" and $d=="") or $tri=="") 
    {
    	$SQL=$SQL." ORDER BY date_publication".$d;
    	$d="DESC";
    	echo "1er if";
    }
    elseif ($tri=="d" and $d=="DESC" )
    {
    	$SQL=$SQL." ORDER BY date_publication ".$d;
    	$d="";
    	echo "2eme if";
    }
    elseif ($tri=="ti" and $ti=="" )
    {
    	$SQL=$SQL." ORDER BY titre".$ti;
    	$ti="DESC";
    	echo "3eme if";
    }	
    elseif ($tri=="ti" and $ti=="DESC" )
    {
    	$SQL=$SQL." ORDER BY titre ".$ti;
    	$ti="";
    }
    elseif ($tri=="a" and $a=="" )
    {
    	$SQL=$SQL." ORDER BY attribuee".$a;
    	$a="DESC";
    }
    elseif ($tri=="a" and $a=="DESC" )
    {
    	$SQL=$SQL." ORDER BY attribuee ".$a;
    	$a="";
    }
    $res = mysql_query($SQL);
    echo $SQL;
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Document sans titre</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    <!--
    .Style2 {color: #33CC99}
    -->
    </style>
    </head>
    <body bgcolor="#99FF99">
    <p align="center" style="font-weight:bold " >Gestion des clients</p>
    <br />
    <a href="Ajout/ajout.php">Ajouter un Client</a>
    <br /><br />
    <?php
    if (!empty($t))
    {
    	?><span class="Style2"><?php echo $txt?> 
    	<br \><br \></span>
    	<?php
    }
    ?>
    	<table border="1">
          <tr align="center">
            <td><a href="clients.php?tri=d&d=<?php echo $d?>">Date</a></td>
            <td><a href="clients.php?tri=ti&ti=<?php echo $ti?>">Titre</a></td>
            <td><a href="clients.php?tri=a&a=<?php echo $a?>">Attribu&eacute;e</a></td>
            <td>Formations</td>
            <td colspan="4">Edition</td>
          </tr>
          <?php
     
    while($rows=mysql_fetch_assoc($res)) 
    { 
    ?>
          <?php $datePub = $rows['date_publication'];
    list($aPub, $mPub, $jPub) = split("-", $datePub);
    ?>
          <tr align="center">
            <td><?php echo "$jPub/$mPub/$aPub";?> </td>
            <td><?php echo $rows['titre'];?> </td>
            <td><input name="attribue<?php echo $rows['no_offre'];?>" type="radio" value="" disabled="disabled"
    	<?php
    	if ((bool)$rows['attribuee'])
    	{echo "checked";}
    	?>
    	>
            </td>

  2. #2
    Membre habitué Avatar de Shinji62
    Profil pro
    Inscrit en
    Novembre 2006
    Messages
    135
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 135
    Points : 150
    Points
    150
    Par défaut
    Pour commencer le contraire de DESC c'est ASC essaye ca deja .

    De plus sans vouloir te vexer ton code, me donne mal à la tête essaye de reecrire ca proprement, utilise un switch au lieu de ton if.


    Puis si jamais tu ne trouves pas, reposte

  3. #3
    Membre du Club
    Inscrit en
    Avril 2006
    Messages
    261
    Détails du profil
    Informations forums :
    Inscription : Avril 2006
    Messages : 261
    Points : 64
    Points
    64
    Par défaut
    Bah ASC ca e dire croissant, jai deja essayé ca ne marche pas moi ce que je ve c'est décroissant.

    Mon code est peut etre assez bourrin (débutant inside) mais le problème n'est pas la, je voudrais juste savoir pourquoi il rentre pas dans le If contenant le DESC

  4. #4
    Membre du Club
    Inscrit en
    Avril 2006
    Messages
    261
    Détails du profil
    Informations forums :
    Inscription : Avril 2006
    Messages : 261
    Points : 64
    Points
    64
    Par défaut
    C'est bon c résolu, un pote ma aidé, on a réussi à réecrire toute la page

    Voila le code pour ceuw que ca interesse

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    <?php
    include('testeurDeSession.php');
    include('connexion.php');
     
    if (isset($_GET['t']))
    {
    	$t=$_GET['t'];
     
    	if ($t=='a')
    	{
    		$txt="Le client a été ajouté avec succès.";
    	}
    	elseif ($t=='m')
    	{
    		$txt="Le client a été modifié avec succès.";
    	}
    	elseif ($t=='s')
    	{
    		$txt="Le client a été supprimé avec succès.";
    	}
    }
     
    if (isset($_GET['tri']))
    { 
     switch($_GET['tri'])
     {
    	case "ti" : $tri = " ORDER BY titre"; break;
    	case  "d" : $tri = " ORDER BY date_publication"; break; 
    	case  "a" : $tri = " ORDER BY attribuee"; break;
      	default : $tri = " ORDER BY no_offre";
     }
    }
    else 
    { 
     $tri = " ORDER BY no_offre";
    }
     
    if (isset($_GET['ord']))
    { 
     switch($_GET['ord'])
     {
    	 case "asc" : $ord = " ASC"; $ord2 = "desc"; break;
    	case  "desc" : $ord = " DESC"; $ord2 = "asc"; break;
      	default : $ord = " ASC"; $ord2 = "desc";
     }
    }
    else 
    {
     $ord = " ASC"; $ord2 = "desc";
    }
     
    $SQL = "SELECT * FROM offre".$tri.$ord;
     
     
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Document sans titre</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    <!--
    .Style2 {color: #33CC99}
    -->
    </style>
    </head>
    <body bgcolor="#99FF99">
    <p align="center" style="font-weight:bold " >Gestion des clients</p>
    <br />
    <a href="Ajout/ajout.php">Ajouter un Client</a>
    <br /><br />
    <?php
    if (!empty($t))
    {
    	?><span class="Style2"><?php echo $txt?> 
    	<br \><br \></span>
    	<?php
    }
    ?>
    	<table border="1">
          <tr align="center">
            <td><a href="clients.php?tri=d&ord=<?php if($_GET['tri'] == "d"){echo $ord2;} else { echo "asc";}?>">Date</a></td>
            <td><a href="clients.php?tri=ti&ord=<?php if($_GET['tri'] == "ti"){echo $ord2;} else { echo "asc";}?>">Titre</a></td>
            <td><a href="clients.php?tri=a&ord=<?php if($_GET['tri'] == "a"){echo $ord2;} else { echo "asc";}?>">Attribu&eacute;e</a></td>
            <td>Formations</td>
            <td colspan="4">Edition</td>
          </tr>

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. problème pour trier un tableau de hashage
    Par Jasmine80 dans le forum Langage
    Réponses: 1
    Dernier message: 25/02/2007, 13h02
  2. [SQL] Fonction pour trier un tableau
    Par ciel65 dans le forum PHP & Base de données
    Réponses: 7
    Dernier message: 15/02/2007, 16h43
  3. [DisplayTag] Problème pour trier les dates dans un tableau
    Par Pascale38 dans le forum Taglibs
    Réponses: 13
    Dernier message: 01/09/2006, 09h58
  4. Réponses: 28
    Dernier message: 24/05/2006, 18h20
  5. [Tableaux] Problème pour contrôler un tableau
    Par renaud26 dans le forum Langage
    Réponses: 5
    Dernier message: 10/04/2006, 11h45

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo