Hey,
I have searched the net but I don't know what to search for? I have listed my list of games on my games website in order of newest first, how would I go about adding a drop down menu to allow the user to re-order the list in different ways e.g.
Newest first,
Oldest first etc...
List MySQL results in different order?
Moderator: General Moderators
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
Re: List MySQL results in different order?
Just have a form which when submitted executes a query which fetches the same results but with a different ORDER BY clause?
Re: List MySQL results in different order?
page one.php contains 2 links:
page list-games.php does this:
So, filter incoming variables against a white list, and "build up" an sql query.
Its possible to "build up" an sql query with other variables, like LIMIT and WHERE clauses too.
It can get very messy if you have too many, and you should investigate other methods of doing this once it gets too complicated (eg using OOP).
HTH
Code: Select all
<a href="list-games.php?date_order=desc">Newest</a>
<a href="list-games.php?date_order=asc">Oldest</a>
Code: Select all
<?php
$order = $_GET['date_order'] ;
if( !in_array( $order, array( 'desc', 'asc' ) ) {
// send away - out of range of the provided "white list"
die();
}
$sql = "select name from games order by production_date " . $order ;
// send that sql into your database and show results.
?>
Its possible to "build up" an sql query with other variables, like LIMIT and WHERE clauses too.
It can get very messy if you have too many, and you should investigate other methods of doing this once it gets too complicated (eg using OOP).
HTH