I have a simple list-type php page, which lists items according to a mysql query, like:
mysql_select_db($database_connBHN, $connBHN);
$query_rsMarket = "SELECT * FROM my_items WHERE active=1 ORDER BY name asc";
$rsMarket = mysql_query($query_rsMarket, $connBHN) or die(mysql_error());
$row_rsMarket = mysql_fetch_assoc($rsMarket);
$totalRows_rsMarket = mysql_num_rows($rsMarket);
then the page list these items and their description in separate tables.
Initially this page lists these items in alphabetical order. Now I would like to put a drop down box on the top of the page, where the user could choose another two or three more sorting options, like date, or itemId, etc, which values are stored in the database.
How could I solve this in a simple way, without leaving that page? (i.e. I do not want to create separate pages for each different result set)
No, it’s easier to keep this as a single script and just allow for the sorting variable to be switched. For security’s sake, it’s best to limit the user input to a per-defined set of options in the PHP script:
Now you can just have the order set with _GET variables:
http://example.com/page.php?field=name&order=descetc. This can be set with javascript (or on form submission) using dropdowns: