01.
02.
03.
04.
05.
06.
07.
08.
09.
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.
|
|
<?php
/******************************************************************************/
/* */
/* __ ____ */
/* ___ / / ___ / __/__ __ _____________ ___ */
/* / _ \/ _ \/ _ \_\ \/ _ \/ // / __/ __/ -_|_-< */
/* / .__/_//_/ .__/___/\___/\_,_/_/ \__/\__/___/ */
/* /_/ /_/ */
/* */
/* */
/******************************************************************************/
/* */
/* Titre : Trier les colonnes d'un tableau */
/* */
/* URL : http://www.phpsources.org/scripts494-PHP.htm */
/* Auteur : moderator */
/* Date édition : 31 Jan 2009 */
/* Website auteur : http://www.annonces.up1.fr */
/* */
/******************************************************************************/
#########################################################################
# #
# TRIER UN TABLEAU PAR COLONNES #
# #
#########################################################################
# #
# Script cuisiné par Moderator #
# #
# site : http://www.annonces.up1.fr #
# mail : contact@annonces.up1.fr #
# #
#########################################################################
// -----------------------------------------------------------------------------
//-------------------------------
// PETIT FORMULAIRE POUR SELECTIONNER LA COLONNE POUR FAIRE LE TRI....
// PEUT ETRE REMPLACE PAR DES LIENS A LA LIGNE ENTETES DE COLONNES.
print("<form method=\"post\" action=\"\"> \n");
print("<SELECT name=\"choix\"> \n");
print("<OPTION VALUE=\"$choix\">Sélectionnez</OPTION> \n");
print("<OPTION VALUE=\"tricol1\">Colonne 1</OPTION> \n");
print("<OPTION VALUE=\"tricol2\">Colonne 2</OPTION> \n");
print("<OPTION VALUE=\"tricol3\">Colonne 3</OPTION> \n");
print("</SELECT> \n");
print("<input type=\"submit\" value=\"ok\"> \n");
print("</form> \n");
// -----------------------------------------------------------------------------
//-------------------------------
// TABLEAU
$tableau = array(array('Petit','Chaperon','Rouge'),array('Le','Petit','Poucet'),
array('Grand','Méchant','Loup'),array('Bon','Brute','Truand'),array('Sea','Sex',
'Sun'),array('Athos','Porthos','Aramis'),array('Raphael','Léonard','Renoir'));
// -----------------------------------------------------------------------------
//-------------------------------
// FONCTIONS PERMETANT DE FAIRE UN TRI SUIVANT LE N° DE COLONNE
function tricol2($a,$b) {
if ($a[1] == $b[1])
// 0= 1er champ, 1=2eme champ, etc (champ=colonne) ...
return ;
return ($a[1] < $b[1]) ? -1 : 1;
// < ou > determine ordre croissant ou decroissant
}
function tricol1($a,$b) {
if ($a[] == $b[])
return cmp1($a,$b);
return ($a[] < $b[]) ? -1 : 1;
}
function tricol3($a,$b) {
if ($a[2] == $b[2])
return cmp1($a,$b);
return ($a[2] < $b[2]) ? -1 : 1;
}
// -----------------------------------------------------------------------------
//-------------------------------
// TRI
usort($tableau, "$choix");
// -----------------------------------------------------------------------------
//-------------------------------
// AFFICHAGE
print("<table>");
// ENTETES DE COLONNES...
print(
"<tr><td width=\"120\">COLONNE 1</td><td width=\"120\">COLONNE 2</td><td" .
" width=\"120\">COLONNE 3</td></tr>");
foreach($tableau as $champ) { // "pour toutes les lignes du tableau..."
// ... on affiche les champs qui nous intéressent !
print(
"<tr><td width=\"120\">$champ[0]</td><td width=\"120\">$champ[1]</td><td" .
" width=\"120\">$champ[2]</td></tr>");
}
print("</table>");
?>
|