tags:

views:

188

answers:

5

Hello,

I would like to know which is the best way to sort table columns with PHP and MySQL. Is there a way to do this without having to set a variables like the following?

$strASC = $_GET["order"];

if ($strASC == "ASC") 
{
    $strASC = "DESC";
}

or does exist an SQL query that reverses the ASC or DESC depending on current status?

A: 

Using only PHP and the database, I do not think there is a better way that you could do that.

But, if you wanted to try using javascript and not running another query, you could take a look at jquery and tablesorter.

Magic Hat
+1  A: 

no mysql doesnt do it automatically. u can reduce it though

$strASC = $_GET['order']=='ASC'?'DESC':'ASC';
Funky Dude
Which is not really a notation that is very readable. If you want to use it, at least write it ($_GET['order'] == 'ASC) ? 'DESC' : 'ASC';
tharkun
tharkun, that's splitting hairs. it's perfectly readable.
brianreavis
if you can't read this simple statement, you're n the wrong bussiness, parentices only clutter the code more in this case, imo
Nicky De Maeyer
+2  A: 

When you get a result set from a database it is in the order you specify. The database does not reorder the data in it's tables so therefore it does not know which "status" it is currently in.

Also watch out for SQL Injection in your example.

kerchingo
Thanks for pointing the injection
Ole Media
how would you avoid injection on something like this?
Ole Media
Escape the contents of the query string by using:$strADC = mysql_real_escape_string($_GET["order"])
kerchingo
+3  A: 

There's no function in particular for flipping the sort order, but you could do something like this:

$sortDir = $_GET['order'] == 'ASC' ? 'DESC' : 'ASC';
mysql_query('SELECT * FROM table ORDER BY col ' . $sortDir);
brianreavis
$sortDir seems like a bad variable name IMO since Dir is most often used for Directory. I called it $sortDirection which makes the code more readable!
tharkun
Oh please. When are you going to have a directory that's referred to as the "sort" directory? And besides, look at the variable in context. If `$sortDir` is one line above a `mysql_query` call, I'd say that's plenty readable/understandable.
brianreavis
I'd agree with tharkun - a quick scan of that section of code might make you think you were talking about sorting directories.
Skilldrick
Also, shouldn't the 'DESC' and 'ASC' be swapped in the second half of the ternary statement?
Skilldrick
the variable naming BS is very off topic, it's just a code example, which is written fast...
Nicky De Maeyer
Sorry, I get it now :P
Skilldrick
+2  A: 

You're having a $_GET in that code so I assume that you want to use url params to set the order direction of your query.

Your code looks a bit confusing, you're setting $strASC to "DESC" which is a little bit of a contradiction ;)

What about something like that:

$sortDirection = $_GET['order'];

if ($sortDirection == 'ASC' || $sortDirection == 'DESC')
{
    $sql = "SELECT mystuff FROM mytable WHERE mycrit ORDER BY " . $sortDirection;
}
else
{
    echo 'Invalid sort direction';
}
tharkun