vtx-rider Posted September 20, 2008 Share Posted September 20, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/125071-solved-order-by-gtgt-change-direction-of-order/ Share on other sites More sharing options...
wildteen88 Posted September 20, 2008 Share Posted September 20, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/125071-solved-order-by-gtgt-change-direction-of-order/#findComment-646380 Share on other sites More sharing options...
vtx-rider Posted September 20, 2008 Author Share Posted September 20, 2008 Thank you!!! It works! :) Quote Link to comment https://forums.phpfreaks.com/topic/125071-solved-order-by-gtgt-change-direction-of-order/#findComment-646393 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.