PHP sort ASC and DESC from database

Hello Experts!

I have a list in my database with several columns. And I want to sort them in PHP like this:

$sort = $_GET['sort'];
$sql = "SELECT * FROM users ORDER by ".$sort." DESC";
$result = mysql_query($sql) or die(mysql_error());

That works fine. But if the user clicks twice at a header for a column it should switch between ASC and DESC sort.

I'm going to implement this to a lot of files so I dont really want to change the original PHP code. I prefer to sort the data directly from the database query.

One way could be to define a variable

$int = 1;

and for every time the user sort a column:


The problem is that this variable will disappear every page refresh.

Someone got any similar solution?

As simple as possible =)

Thank you!
Who is Participating?
brad2575Connect With a Mentor Commented:
you are using $_GET['sort']; to get the field to sort by, I am assuming this is a form variable?

If so you can create a hidden form variable that holds the ASC or DESC value.  then when they click the header change the value on the form PRIOR to submitting the form/refreshing the page.

Then just get the value from the hidden form variable the same way you are the "sort" field.
Hube02Connect With a Mentor Commented:
I would think that the easiest would be to set a session variable that keeps track of ASC/DESC somehow. Not sure exactly how your script works. But with a session, then you could do what you suggest, or something similar. I would likely do something like:

  if (!isset($_SESSION['sort']) || $_SESSION['sort'] == 'DESC') {
    $_SESSION['sort'] = 'ASC';
  } else {
    $_SESSION['sort'] = 'DESC';

  $sql = "SELECT * FROM users ORDER by ".$sort." ".$_SESSION['sort'];
GodDoesntExistAuthor Commented:
Thank you!

Actually I'm not using the GET from a form, its from a link.

You both gave me good solutions.

I'm considering which one to use.

I will split the points your you!

Thanks again!
another way would be to send the ordering mode also by GET variables. The MySQL Query will have the value of the variable $_GET['sorttype'] as an ordering mode, and you will need to change this value when you click the table header.

Notice that you have do this check of the ordering type before you echo the table head, because you need to include it in the link of the header column:

[...] href="'. $newSortType .'" [...]

this way, when you load the page, the entries in your table will be sorted in the way specified by $_GET['sorttype'] and when you click the table header, the entries will be sorted in the other way.
// default type
$newSortType = 'DESC';
// change the type of ordering for including in the url of table header
if (isset($_GET['sorttype']))
    if ('ASC' == $_GET['sorttype'])
        $newSortType = 'DESC';
        $newSortType = 'ASC';
// sql query will be
$sql = "SELECT * FROM `users` ORDER by ".$sort." ".$_GET['sorttype'];

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.