MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
It's more for semantics. It'll change asc or deSc into ASC and DESC respectively so that the query looks correct. It'd work either way, but its always good form to create queries that are properly formatted. Same goes for code.
All but the last field would be sorted as ascending, but it'd work. If you wanted pairs, you could explode the variables by , and use them in a foreach to generate the ORDER BY.
I also tried using that end piece of code on other pages to do the same thing and are getting errors ? all I tried to do was paste this piece of code on the end of the query.
PHP Code:
ORDER BY "' . $_GET['sort'] . '" ' . strtoupper($_GET['order]));
and then replaced the " at the start of the line with a ' like you did with the copy you edited for me so I must be missing something there or was that for use with only that query.
Velox is right , the uppercasing of DESC etc is just semantics and not required in MySQL and I think my original snippet was missing a space
just posting to note that if you are only selecting from 1 table then there is no reason to prefix the fieldnames (even in a join you don't always need them)
PHP Code:
<?php $sort = mysql_real_escape_string($_GET['sort']); $order=''; if(strtolower($_GET['order'])=='desc'){ $order='DESC'; } $result = mysql_query(" SELECT surname, firstname, nickname, email, DATE_FORMAT(signup, '%d %b %Y') AS signup, mobile, bay, aisle, seat, oa_id, club_member, seasontix, anchor FROM oamembers ORDER BY $sort $order "); ?>
MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
Velox is right , the uppercasing of DESC etc is just semantics and not required in MySQL and I think my original snippet was missing a space
just posting to note that if you are only selecting from 1 table then there is no reason to prefix the fieldnames (even in a join you don't always need them)
PHP Code:
<?php
$sort = mysql_real_escape_string($_GET['sort']);
$order='';
if(strtolower($_GET['order'])=='desc'){
$order='DESC';
}
$result = mysql_query("
SELECT surname, firstname, nickname, email,
DATE_FORMAT(signup, '%d %b %Y') AS signup,
mobile, bay, aisle, seat, oa_id, club_member, seasontix, anchor
FROM oamembers
ORDER BY $sort
$order
");
?>
Ive tested your piece of code in a few other pages Im usingand it works fine and I want to know if its possible using this snippet to sort by two fields instead of just one?..
example using one field to sort by
php?sort=bay&order=desc
how could I use this so it would sort by two fields for example the field bay then sort by aisle as well.
so if I had 5 records like below which were sorted just by the bay field
bay 1 aisle 3
bay 1 aisle 2
bay 1 aisle 1
bay 1 aisle 4
bay 1 aisle 2
how could I use this to make them sort by the bay field first then the aisle field ?
bay 1 aisle 1
bay 1 aisle 2
bay 1 aisle 3
bay 1 aisle 4
bay 1 aisle 5
I have tried using the link below without any success.