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 :

fonction tri d'un tableau [MySQL]


Sujet :

PHP & Base de données

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Homme Profil pro
    Apprenti Ingénieur
    Inscrit en
    Novembre 2011
    Messages
    104
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Apprenti Ingénieur
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Novembre 2011
    Messages : 104
    Par défaut
    Bonjour,

    Je suis en train de développer une fonction TRI pour mon tableau.
    Les liens sont bien généré sur les <th> mais mon tableau n'affiche rien =/

    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
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    <?php session_start(); ?>
    <?php include('../login.html'); ?>
    <?php
     
     
    // Tri sur colonne
    $tri_autorises = array('ui','adresse','client','tel','urs','nature','arrivee','reponse');
    $order_by = (isset($_GET['order']) && in_array($_GET['order'],$tri_autorises)) ? $_GET['order'] : 'reponse';
     
    // Sens du tri
    $order_dir = isset($_GET['inverse']) ? 'DESC' : 'ASC';
     
     
    // Préparation de la requête
    $sql = "
    	SELECT *
    	FROM preetude
    	ORDER BY {$order_by} {$order_dir}
    ";
    $result = mysql_query($sql);
     
     
    // Notre fonction qui affiche les liens
    function sort_link($text, $order=false)
    {
    	global $order_by, $order_dir;
     
    	if(!$order)
    		$order = $text;
     
    	$link = '<a href="?order=' . $order;
    	if($order_by==$order && $order_dir=='ASC')
    		$link .= '&inverse=true';
    	$link .= '"';
    	if($order_by==$order && $order_dir=='ASC')
    		$link .= ' class="order_asc"';
    	elseif($order_by==$order && $order_dir=='DESC')
    		$link .= ' class="order_desc"';
    	$link .= '>' . $text . '</a>';
     
    	return $link;
    }
     
    ?>
     
    <?php include('../doctype.html'); ?>
     
    <html xmlns="http://www.w3.org/1999/xhtml">
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
     
    		<!-- Titre
    		************************************************************-->
    		<?php include('../titre.html'); ?>
     
    		<!-- Feuille de style
    		************************************************************-->
    		<style type="text/css" media="screen">
    			@import url(../styles/style.css);
    		</style>
     
    <style type="text/css">
    a.order_asc,
    a.order_desc:hover { 
    	padding-right:15px;
    	background:transparent url(../styles/s_asc.png) right no-repeat;
    }
    a.order_desc,
    a.order_asc:hover {
    	padding-right:15px;
    	background:transparent url(../styles/s_desc.png) right no-repeat;
    }
    </style>
     
    		<script type="text/javascript" src="functions.js"></script>
     
    	</head>
     
    	<body id="principal">
     
     
     
    	<!-- Conteneur
    	************************************************************-->
    	<div id="conteneur">
     
    		<!-- En-tete
    		************************************************************-->
    		<h1 id="header">
    			<?php include("../titrepage.html"); ?>
    		</h1>
     
     
     
    		<!-- Barre verticale
    		************************************************************-->
    		<div id="navigation">
    			<div id="styles">
    					<?php include('../menu.html'); ?>
    			</div>
     
    			<div id="informations">
    			<h2>Informations</h2>
    				<?php include('../maj.html'); ?>
    			</div>
    		</div>
     
    	<!-- Contenu
    	************************************************************-->
    	<div id="contenu">
    		<div id="main">
    		<div id="projet">
     
     
    		<h2><span>Listing Pré-Etudes</span></h2>
     
    			<p></p>
     
    		</div>
     
    		<div id="base">
     
    		<h3><span>Base de Données Complètes des Pré-Etudes<u></u></span></h3>
     
    		<h4><span>Pour rechercher une Pré-Etude, un simple Ctrl+F suffit.</span></h4>
     
    		<p><a href="archannul.php">- Voir les Pré-Etudes Annulées -</a></p><br>
     
    			<p>
    			<!-- Données de la table
    			************************************************************-->
    			<table id="tab1">
    			  <thead>
    				 <tr>
    					<th><?php echo sort_link('UI', 'ui') ?></th>
    					<th><?php echo sort_link('Adresse', 'adresse') ?></th>
    					<th><?php echo sort_link('Client', 'client') ?></th>
    					<th><?php echo sort_link('Tel', 'tel') ?></th>
    					<th><?php echo sort_link('Nature<br>Etude', 'nature') ?></th>
    					<th><?php echo sort_link('Nom<br>Demandeur', 'urs') ?></th>
    					<th><?php echo sort_link('AS/CAFF', 'ascaff') ?></th>
    					<th><?php echo sort_link('ARRIVEE', 'arrivee') ?></th>
    					<th><?php echo sort_link('Réponse<br>Demandeur', 'reponse') ?></th>
    					<th>Comm<br>Etude</th>
    					<th>Etat</th>
    					<th>MOD</th>
    					<th>DEL</th>
    					<th>ANN</th>
    				 </tr>
    			  </thead>
    			  <tbody>
    <?php while( $row=mysql_fetch_assoc($result) ) : ?>
    				 <tr 
    				 <?php 
    				if ($row['reponse'] == "0000-00-00") {
    					echo "style=\"background: #E1E1E1\"";
    				} else {
    					echo "";
    				}
    				?>
    				>
    					<td><?php echo $row['ui']; ?></td>
    					<td><?php echo $row['adresse']; ?></td>
    					<td><?php echo $row['client']; ?></td>
    					<td><?php echo $row['tel']; ?></td>
    					<td><?php echo $row['nature']; ?></td>
    					<td><?php echo $row['urs']; ?></td>
    					<td><?php echo $row['ascaff']; ?></td>
    					<td><?php echo $row['arrivee']; ?></td>
    					<td><?php 
    					if ($row['reponse'] == "0000-00-00") {
    						echo "";
    					} else {
    						echo $row['reponse'];
    					}
    					?></td>
    					<td ><?php 
    					$com = $row['id_etu'];
    					echo "<a href=comm.php?id=$com target=wclose onclick=window.open('comm.php?id=$com','wclose','width=600,height=800,scrollbars=yes,toolbar=no,status=no,left=20,top=30')><img src=\"../styles/comm.gif\" alt=\"Modifier la ligne\" /></a>";
    					?></td>
    					<td>
    					<?php 
    					if ($row['reponse'] == "0000-00-00") {
    						echo "En Cours";
    					} else {
    						echo "Terminée";
    					}
    					?></td>
    					<td><a href="modifier.php?id=<?php echo $row['id_etu']; ?>" onclick="return confirm('Êtes-vous sur de vouloir modifier ?')"><img src="../styles/icoMod.gif" alt="Modifier la ligne" /></a></td>
    					<td><a href="delete.php?id=<?php echo $row['id_etu']; ?>" onclick="return confirm('Êtes-vous sur de vouloir supprimer ?')"><img src="../styles/icoSup.png" alt="Supprimer la ligne" /></a></td>
    					<td><a href="annul.php?id=<?php echo $row['id_etu']; ?>" onclick="return confirm('Pré-Etude Annulée ? (Manipulation Irreversible)')"><img src="../styles/annul.png" alt="Annuler la Pré Etude" /></a></td>
    				 </tr>
    <?php endwhile ?>
    			  </tbody>
    			</table>	
    			</p>
     
    		</div>
     
    		<!-- Bloc pour mettre des informations en bas de page
    		************************************************************-->
    		<div id="infostyle"><span></span></div>
     
    		<!-- /Fin Contenu
    		************************************************************-->
    		</div>
    	</div>
     
    	<!-- Pied de page
    	************************************************************-->
    	<p id="footer">
    		<?php include('../foot.html'); ?>
    	</p>
     
    <!-- /Fin Conteneur
    ************************************************************-->
    </div>
     
    <!-- Blocs supplémentaires pour ajouter des images
    ************************************************************-->
    <div id="imgbloc1"><span></span></div>
    <div id="imgbloc2"><span></span></div>
    <div id="imgbloc3"><span></span></div>
    <div id="imgbloc4"><span></span></div>
    <div id="imgbloc5"><span></span></div>
     
    <!-- Balises de fermeture
    ************************************************************-->
    </body>
    </html>
    Le code source généré :
    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
    			<p>
    			<!-- Données de la table
    			************************************************************-->
    			<table id="tab1">
    			  <thead>
    				 <tr>
    					<th><a href="?order=ui" class="order_desc">UI</a></th>
    					<th><a href="?order=adresse">Adresse</a></th>
    					<th><a href="?order=client">Client</a></th>
    					<th><a href="?order=tel">Tel</a></th>
    					<th><a href="?order=nature">Nature<br>Etude</a></th>
    					<th><a href="?order=urs">Nom<br>Demandeur</a></th>
    					<th><a href="?order=ascaff">AS/CAFF</a></th>
    					<th><a href="?order=arrivee">ARRIVEE</a></th>
    					<th><a href="?order=reponse">Réponse<br>Demandeur</a></th>
    					<th>Comm<br>Etude</th>
    					<th>Etat</th>
    					<th>MOD</th>
    					<th>DEL</th>
    					<th>ANN</th>
    				 </tr>
    			  </thead>
    			  <tbody>
    			  </tbody>
    			</table>	
    			</p>
    Quelqu'un comprendrait-il pourquoi mon tableau est vide ? =/

    Merci d'avance !

  2. #2
    Membre Expert Avatar de Djakisback
    Profil pro
    Inscrit en
    Février 2005
    Messages
    2 023
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 2 023
    Par défaut
    Salut, c'est sans doute ta requête qui ne retourne rien, t'as essayé d'afficher l'erreur potentielle ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    $result = mysql_query($sql) or die(mysql_error().$sql);

  3. #3
    Membre Expert
    Avatar de ericd69
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Avril 2011
    Messages
    1 919
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2011
    Messages : 1 919
    Billets dans le blog
    1
    Par défaut
    salut,

    plusieurs trucs:
    • quand tu fais une feuille de style tu mets pas de style dans ta page... c'est le but non?
    • les moult <php? ?> qui s'enchainent ça rend le code moins lisible et ça ne devrait former qu'un seul
    • mélanger php et html comme ça ne rend pas très lisible et maintenable ton code
    • l'extension mysql pour se connecter à mysql est à proscrire car plus maintenue... tu dois passer à mysqli ou mieux pdo...
    • tu dois tester que tu as bien une ressource renvoyé avnt ta boucle de lecture pour savoir si un erreur c'est produite ou non en cas de non extraction de données
    • si tu fais des includes pourquoi ne pas sortir le gros du code php de ta page html?

  4. #4
    Membre confirmé
    Homme Profil pro
    Apprenti Ingénieur
    Inscrit en
    Novembre 2011
    Messages
    104
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Apprenti Ingénieur
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Novembre 2011
    Messages : 104
    Par défaut
    En fait non, je ne pense que c'est une erreur de requète vu qu'elle marche très bien sur mon ancienne page sans la fonction tri tableau..

    => Marche très bien sur phpmyadmin ! ^^

    Le style que j'ai ajouté, c'est pour TEST pour le moment, quand ça marchera il finira dans mon CSS bien entendu ^^

    Citation Envoyé par ericd69
    tu dois tester que tu as bien une ressource renvoyé avnt ta boucle de lecture pour savoir si un erreur c'est produite ou non en cas de non extraction de données
    La j'ai pas compris..

    En tout cas même en ajoutant :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    $result = mysql_query($sql) or die(mysql_error().$sql);
    ça ne retourne aucune erreur !

  5. #5
    Membre Expert
    Avatar de ericd69
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Avril 2011
    Messages
    1 919
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2011
    Messages : 1 919
    Billets dans le blog
    1
    Par défaut
    $result est soit false soit un obtet de type resource si un problème est arrivé niveau requête

    de plus, pense toujours que tu peux avoir un soucis lors de l'exécution de ton script et un plantage de la connexion mysql...
    donc tu vois tu peux avoir une section php qui contient presque tout ton code au tout début de ton script et juste des
    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    <?php echo $variable; ?>
    là où tu veux insérer du contenu... ça rend ton code plus lisible et facile à changer ou maintenir...

  6. #6
    Membre confirmé
    Homme Profil pro
    Apprenti Ingénieur
    Inscrit en
    Novembre 2011
    Messages
    104
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Apprenti Ingénieur
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Novembre 2011
    Messages : 104
    Par défaut
    Finalement j'ai trouvé la solution. C'était la boucle WHILE qui ne sortait rien.
    J'ai remplacé par une boucle FOR, et ça roule !

    Mon code en début de page :

    Code php : 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
    <?php
     
     
    // Tri sur colonne
    $tri_autorises = array('ui','adresse','client','tel','urs','nature','arrivee','reponse');
    $order_by = (isset($_GET['order']) && in_array($_GET['order'],$tri_autorises)) ? $_GET['order'] : 'reponse';
     
    // Sens du tri
    $order_dir = isset($_GET['inverse']) ? 'DESC' : 'ASC';
     
     
    // Préparation de la requête
    $sql = "
    	SELECT *
    	FROM preetude
    	ORDER BY ".$order_by." ".$order_dir."
    ";
    $result = mysql_query($sql) or die(mysql_error().$sql);
     
    while($row = mysql_fetch_assoc($result)) {
       $dataHisto[] = $row;
    }
     
     
    // Notre fonction qui affiche les liens
    function sort_link($text, $order=false)
    {
    	global $order_by, $order_dir;
     
    	if(!$order)
    		$order = $text;
     
    	$link = '<a href="?order=' . $order;
    	if($order_by==$order && $order_dir=='ASC')
    		$link .= '&inverse=true';
    	$link .= '"';
    	if($order_by==$order && $order_dir=='ASC')
    		$link .= ' class="order_asc"';
    	elseif($order_by==$order && $order_dir=='DESC')
    		$link .= ' class="order_desc"';
    	$link .= '>' . $text . '</a>';
     
    	return $link;
    }
     
    ?>

    le code de mon tableau :

    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
    			<!-- Données de la table
    			************************************************************-->
    			<table id="tab1">
    			  <thead>
    				 <tr>
    					<th><?php echo sort_link('UI', 'ui') ?></th>
    					<th><?php echo sort_link('Adresse', 'adresse') ?></th>
    					<th><?php echo sort_link('Client', 'client') ?></th>
    					<th><?php echo sort_link('Tel', 'tel') ?></th>
    					<th><?php echo sort_link('Nature<br>Etude', 'nature') ?></th>
    					<th><?php echo sort_link('Nom<br>Demandeur', 'urs') ?></th>
    					<th><?php echo sort_link('AS/CAFF', 'ascaff') ?></th>
    					<th><?php echo sort_link('ARRIVEE', 'arrivee') ?></th>
    					<th><?php echo sort_link('Réponse<br>Demandeur', 'reponse') ?></th>
    					<th>Comm<br>Etude</th>
    					<th>Etat</th>
    					<th>MOD</th>
    					<th>DEL</th>
    					<th>ANN</th>
    				 </tr>
    			  </thead>
    			  <tbody>
    				<?php foreach($dataHisto as $row): ?>
    				 <tr 
    				 <?php 
    				if ($row['reponse'] == "0000-00-00") {
    					echo "style=\"background: #E1E1E1\"";
    				} else {
    					echo "";
    				}
    				?>
    				>
    					<td><?php echo $row['ui']; ?></td>
    					<td><?php echo $row['adresse']; ?></td>
    					<td><?php echo $row['client']; ?></td>
    					<td><?php echo $row['tel']; ?></td>
    					<td><?php echo $row['nature']; ?></td>
    					<td><?php echo $row['urs']; ?></td>
    					<td><?php echo $row['ascaff']; ?></td>
    					<td><?php echo $row['arrivee']; ?></td>
    					<td><?php 
    					if ($row['reponse'] == "0000-00-00") {
    						echo "";
    					} else {
    						echo $row['reponse'];
    					}
    					?></td>
    					<td ><?php 
    					$com = $row['id_etu'];
    					echo "<a href=comm.php?id=$com target=wclose onclick=window.open('comm.php?id=$com','wclose','width=600,height=800,scrollbars=yes,toolbar=no,status=no,left=20,top=30')><img src=\"../styles/comm.gif\" alt=\"Modifier la ligne\" /></a>";
    					?></td>
    					<td>
    					<?php 
    					if ($row['reponse'] == "0000-00-00") {
    						echo "En Cours";
    					} else {
    						echo "Terminée";
    					}
    					?></td>
    					<td><a href="modifier.php?id=<?php echo $row['id_etu']; ?>" onclick="return confirm('Êtes-vous sur de vouloir modifier ?')"><img src="../styles/icoMod.gif" alt="Modifier la ligne" /></a></td>
    					<td><a href="delete.php?id=<?php echo $row['id_etu']; ?>" onclick="return confirm('Êtes-vous sur de vouloir supprimer ?')"><img src="../styles/icoSup.png" alt="Supprimer la ligne" /></a></td>
    					<td><a href="annul.php?id=<?php echo $row['id_etu']; ?>" onclick="return confirm('Pré-Etude Annulée ? (Manipulation Irreversible)')"><img src="../styles/annul.png" alt="Annuler la Pré Etude" /></a></td>
    				 </tr>
    			<?php endforeach; ?>
    			  </tbody>
    			</table>
    Merci

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

Discussions similaires

  1. [XL-2010] Pb VBA fonction tri sur grand tableau
    Par dm217492 dans le forum Excel
    Réponses: 2
    Dernier message: 28/06/2012, 13h29
  2. [XL-2007] Tri d'un tableau en fonction du mois et d'une ComboBox
    Par bob254 dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 09/04/2010, 14h32
  3. tri d'1 tableau en fonction d'un autre avec qsort
    Par Invité dans le forum Débuter
    Réponses: 1
    Dernier message: 20/02/2010, 14h11
  4. Fonction tri Tableau
    Par Invité dans le forum C
    Réponses: 2
    Dernier message: 30/12/2009, 17h37
  5. Réponses: 4
    Dernier message: 06/03/2007, 13h35

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