Page 1 of 1
List MySQL results in different order?
Posted: Sun Apr 26, 2009 5:28 am
by tomsace
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...
Re: List MySQL results in different order?
Posted: Sun Apr 26, 2009 8:39 am
by jayshields
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?
Posted: Sun Apr 26, 2009 8:41 am
by Yossarian
page one.php contains 2 links:
Code: Select all
<a href="list-games.php?date_order=desc">Newest</a>
<a href="list-games.php?date_order=asc">Oldest</a>
page list-games.php does this:
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.
?>
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