Jump to content

[SOLVED] order by -->> change direction of order


vtx-rider

Recommended Posts

Hello

 

How can I change the direction of the order by clicking the link at the top of the column?

 

Until here it works: This code gives the output of a table with 4 fields. You can order the fields by clicking at the top of the table. Test it here: http://www.vtx-treffen.ch/tabelle_sortiert.php.

 

Now, I want, that the direction of the order changes by every click (asc -> desc -> asc -> desc etc.). Can everbody help me?

 

-->>Sorry, my english is not very good. I hope, that my question was clear enough...

 

Thanks!

 

<head>
<?php 
include ("dbconnect.php");
?>
<title>Table</title>
</head>
<body>
<?php
if ($_GET['orderby']) { 
   $allowed = array('Feld1','Feld2', 'Feld3', 'Feld4'); 
   $order = mysql_real_escape_string($_GET['orderby']);
   $order = (in_array($order, $allowed))? $order : "Feld1"; 
} else { 
   $order = "Feld1"; 
} 
$ergebnis = mysql_query("SELECT Feld1, Feld2, Feld3, Feld4 FROM tabelle ORDER BY $order")or die("Anfrage fehlgeschlagen: " . mysql_error()); 
?>
<table width="700" border="1" bordercolor="#0000FF" > 
<?php
echo <<<LISTCOLS
   <tr>
      <td><a href = '{$_SERVER['PHP_SELF']}?orderby=Feld1'>ABCD</td> 
      <td><a href = '{$_SERVER['PHP_SELF']}?orderby=Feld2'>EFGH</td> 
      <td><a href = '{$_SERVER['PHP_SELF']}?orderby=Feld3'>IJKL</td> 
      <td><a href = '{$_SERVER['PHP_SELF']}?orderby=Feld4'>MNOP</td> 
   </tr>
LISTCOLS;
while ($result = mysql_fetch_array($ergebnis)){ 
?>
<tr>                                  
    <td align="left" nowrap="nowrap" bordercolor="#0000FF" bgcolor="#FFFFFF"><?=$result['Feld1']?></td>  
    <td align="left" nowrap="nowrap" bordercolor="#0000FF" bgcolor="#FFFFFF"><?=$result['Feld2']?></td>
    <td align="left" nowrap="nowrap" bordercolor="#0000FF" bgcolor="#FFFFFF"><?=$result['Feld3']?></td> 
    <td align="left" nowrap="nowrap" bordercolor="#0000FF" bgcolor="#FFFFFF"><?=$result['Feld4']?></td> 
  </tr>
<?php
}
?>
</table> 
<?php
mysql_close($connection);
?>
</body>
</html>

Add another parameter to your links call this parameter odir. add the following the code:

 

Change:

   $order = (in_array($order, $allowed))? $order : "Feld1";

to

   $order = (in_array($order, $allowed))? $order : "Feld1";
   $odir = ($_GET['odir'] == 'asc') ? 'desc' : 'asc';

 

Change:

else { 
   $order = "Feld1"; 
} 

to

else { 
   $order = "Feld1"; 
   $odir = 'asc';
} 

 

Change

ORDER BY $order")or die("Anfrage fehlgeschlagen: " . mysql_error()); 

to

ORDER BY $order $odir")or die("Anfrage fehlgeschlagen: " . mysql_error()); 

 

Change:

<td><a href = '{$_SERVER['PHP_SELF']}?orderby=Feld1'>ABCD</td> 
      <td><a href = '{$_SERVER['PHP_SELF']}?orderby=Feld2'>EFGH</td> 
      <td><a href = '{$_SERVER['PHP_SELF']}?orderby=Feld3'>IJKL</td> 
      <td><a href = '{$_SERVER['PHP_SELF']}?orderby=Feld4'>MNOP</td>

to:

<td><a href = '{$_SERVER['PHP_SELF']}?orderby=Feld1&odir={$odir}'>ABCD</td> 
      <td><a href = '{$_SERVER['PHP_SELF']}?orderby=Feld2&odir={$odir}'>EFGH</td> 
      <td><a href = '{$_SERVER['PHP_SELF']}?orderby=Feld3&odir={$odir}'>IJKL</td> 
      <td><a href = '{$_SERVER['PHP_SELF']}?orderby=Feld4&odir={$odir}'>MNOP</td>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.