I am currently using column header's as links that when clicked will sort the results by the column name by adding a get variable to the url. Here is an example:
<a href="
<?php
// Sorts by order id. If already sorted by order id, then it will change the link to sort descending
if(!isset($_GET['sortby']) || $_GET['sortby'] != 'order_id'){
echo $_SERVER['SCRIPT_NAME'] . '?sortby=order_id'; //example: tracker.php?sortby=order_id
} elseif(isset($_GET['sortby']) || $_GET['sortby'] == 'order_id'){
echo $_SERVER['SCRIPT_NAME'] . '?sortby=order_id_desc'; //example: tracker.php?sortby=order_id_desc
}?>
">Order ID</a>
I also have a form where users can enter pick a category from a selectbox and then enter a searchterm. I am using if statements and switch statements to check if the $_GET['sortby'] variable and the $_POST['search_submit'] variable is set and if so, to run a certain sql statement based on the value of the GET variable.
There are 4 different scenarios.
1. Default: If neither sort nor search is set. This one works fine:
if(!isset($_GET['sortby']) && !isset($_POST['search_submit'])){ //Default, If no sort or search is set
$sql = 'SELECT *
FROM orders
ORDER BY order_id DESC';
}
2. If the search is set but the sort is not. This one works fine:
if(isset($_POST['search_submit'])) {
$search_string = ' WHERE ' . $_POST['searchby'] . '= "' . $_POST['search_input'] . '" ';
}
if(!isset($_GET['sortby']) && isset($_POST['search_submit']) ){ //If the search is set but no sort
$sql = "SELECT *
FROM orders"
. $search_string .
"ORDER BY order_id DESC";
}
3. If the sort is set, but the search is not. This one works fine:
if(isset($_GET['sortby']) && !isset($_POST['search_submit'])) { //If the sort is set but no search
switch ($_GET['sortby']) {
case "order_id":
$sql = "SELECT *
FROM orders
ORDER BY order_id ASC";
break;
case "order_id_desc":
$sql = "SELECT *
FROM orders
ORDER BY order_id DESC";
break;
}
}
4. If the search AND sort is set. All 3 of the above if statements work, but the last one is giving me problems.
if(isset($_GET['sortby']) && isset($_POST['search_submit'])) { //If the sort AND search is set
switch ($_GET['sortby']) {
case "order_id":
$sql = "SELECT *
FROM orders"
. $search_string .
"ORDER BY order_id ASC";
break;
case "order_id_desc":
$sql = "SELECT *
FROM orders"
. $search_string .
"ORDER BY order_id DESC";
break;
}
}
What happens is that you can search, but as soon as you click on one of the column headers and it reloads the page with the new GET variable, it will get rid of the current POST variable, thereby showing all results again. I tried to load the current POST variable into a session after the $_POST['search_submit'] isset and then make the last if statement check to see if the session variable is set, but what happens then is that the session is always set and if i try to go back to the homepage, it will keep those search results.
Perhaps I need to destroy the session somewhere? Perhaps there is an overall better approach I could be taking to combining sort and search features?