Siggles Posted July 2, 2008 Share Posted July 2, 2008 Hi, I already have ... <? if (isset($_GET['sort'])){ $orderedby = $_GET['sort']; }else{ $orderedby = userlevel; } $result = mysql_query("SELECT username,userlevel,timestamp,paidplayer,joined, DATE_format( joined, '%d/%m/%y' ) AS newdate FROM users ORDER BY $orderedby DESC, username"); which works fine. The username column has this link to get it to work, for example... <a href="?sort=username"><u>Username</u></a> I need to make it so that if you click username column heading again it sorts it by ASC instead of DESC and then vice versa. I have played around with adding a variable that changes each time but have so far not got it to work. Any suggestions? Also, is this the best way to work column sorting or can someone suggest a better way? Thank you Link to comment https://forums.phpfreaks.com/topic/112907-solved-sorting-by-column-desc-amp-asc/ Share on other sites More sharing options...
kenrbnsn Posted July 2, 2008 Share Posted July 2, 2008 I would use a session variable to keep track of the sort order. Here's an example: <?php session_start(); $asc_desc = 'ASC'; if (isset($_GET['username'])) $asc_desc = (!isset($_SESSION['asc_desc']) || $_SESSION['asc_desc'] == 'DESC')?'ASC':'DESC'; $_SESSION['asc_desc'] = $asc_desc; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title></title> </head> <body> <?php echo 'The current sort order is: ' . $asc_desc . '<br>'; ?> <a href="?username=xxx">Change sort order</a> </body> </html> See if you can adapt this idea to your code. Ken Link to comment https://forums.phpfreaks.com/topic/112907-solved-sorting-by-column-desc-amp-asc/#findComment-579980 Share on other sites More sharing options...
Siggles Posted July 2, 2008 Author Share Posted July 2, 2008 Will it be a problem that I use a user system and so a session is created already for the user account? Link to comment https://forums.phpfreaks.com/topic/112907-solved-sorting-by-column-desc-amp-asc/#findComment-580002 Share on other sites More sharing options...
revraz Posted July 2, 2008 Share Posted July 2, 2008 Shouldn't be any problem at all. Link to comment https://forums.phpfreaks.com/topic/112907-solved-sorting-by-column-desc-amp-asc/#findComment-580004 Share on other sites More sharing options...
Siggles Posted July 2, 2008 Author Share Posted July 2, 2008 yeah sorry, should have just tried it and guess what, it works. Simple as that. thank you very much. This site is a life saver at times :-) Link to comment https://forums.phpfreaks.com/topic/112907-solved-sorting-by-column-desc-amp-asc/#findComment-580006 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.