Jump to content

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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.