Bonjour,

Je cherche à afficher le résultat d'une requette et ensuite, pouvoir trier sur chacune des colonnes.

J'execute la première requette pour récupérer les entêtes de colonnes a trier.

Ensuite, j'affiche le résultat avec les closes order by.

Je voudrai améliorer le code pour ne pas avoir à lancer 2 requettes à chaque évènement sur la page. Quelqu'un a-t-il une idée?


voici mon bout de 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
<?php
	include "conf.php";
 
$dbconn = pg_connect(" $host $port $dbname $user $password ") or die("Connexion impossible");
  $stat = pg_connection_status($dbconn);
  if ($stat === PGSQL_CONNECTION_OK) {
      echo "Connexion ok \n";
      echo "<br />\n";
  } else {
      echo "Connexion erronée \n";
  } 
$sql0 = " Select * FROM matable";
 
$result0 = pg_query($dbconn, $sql0);
if (!$result0) {
  echo "Une erreur est survenue.\n";
 
  exit;
}
 
// Définit les colonnes à trier
$i=pg_num_fields($result0);
 for ($j = 0; $j < $i; $j++) {	
	$tri_autorises [$j] = pg_field_name($result0, $j);
 }
 
 
 
 
// Tri sur colonne
$order_by = in_array($_GET['order'],$tri_autorises) ? $_GET['order'] : $tri_autorises [0];
 
// Sens du tri
$order_dir = isset($_GET['inverse']) ? 'DESC' : 'ASC';
 
$sql1 = "$sql0 ORDER BY {$order_by} {$order_dir}";
echo "sql1 = $sql1";
 
$result1 = pg_query($dbconn, $sql1);
 
// 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;
}
 
echo '<table border="1"><tr>';
$i=pg_num_fields($result1);
 for ($j = 0; $j < $i; $j++) {
	echo '<th>';
	echo sort_link(pg_field_name($result1, $j),pg_field_name($result1, $j)) ;
	echo '</th>';
 }
echo '</tr>';
 
while ($row = pg_fetch_row($result1)) {
	echo '<tr>';
	for	($j = 0; $j < count($row); $j++) {
		echo '<td>';     		
		echo ($row[$j] == NULL) ? '<i></i>' : $row[$j];
		echo '</td>';
	}
	echo '</tr>';
}
 
 
 
 
 
?>
<style type="text/css">
a.order_asc,
a.order_desc:hover { 
    padding-right:15px;
    background:transparent url(s_asc.png) right no-repeat;
}
a.order_desc,
a.order_asc:hover {
    padding-right:15px;
    background:transparent url(s_desc.png) right no-repeat;
}
</style>