I have started working on a PHP based page which runs a SELECT query on a Postgres table and prints the result.
The page includes a form (POST method) which is being used to filter the table records, e.g. only show records which have values which lie within a particular min/max range for a particular field. The user is able to input values into the min/max fields of the form, and once they press the forms input button, the SELECT query is run again with an appropriate WHERE constraint. This part is working fine.
Now, I wanted to also include the ability for the user to sort the table according to each field. So, with some simple use of GET, I'm able to pass the parameters needed to sort the table via a href on each field header, e.g. "a href="index.php?field=x&order=asc..", which applies an appropriate ORDER BY constraint. This also works fine.
The problem is, when the user clicks one of the table headers in order to sort the table by that field, the page is obviously refreshed and any filters which were applied via the form are lost.
What's my best option to incorporate both of these features? That is, the user should be able to filter the table and then sort the the filtered records according to their desired field.
Do I need to re-think how I'm going about it completely?
You can always use sessions to store the post.
Then just check if there is a post store it in the session and always filter by your session.
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$_SESSION['filter'] = $_POST;
}
if(isset($_SESSION['filter'])){
//Run you query
}