Jump to content

[SOLVED] Sorting by column DESC & ASC


Siggles

Recommended Posts

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

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

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.