PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hello,
I'm querying a Mysql database & displaying the results on a html page. This part is working fine.
I now want to sort the query results ascending by clicking on the column headers. I'm using a code which I got from another forum but I don't know how to edit it. This code is working fine but it re-queries & sorts all the records in the whole database. I don't want it to re-query & sort all the records in the whole database. I want it to sort only the query results from the 1st query.
//the sorting part
if(isset($_GET['sortby'])) $query .= ' ORDER BY '.$_GET['sortby'].' ASC';
//... and for good measure:
$result=mysql_query($query) or die('query error: '.MySQL_error());
The old way, you were assigning something to the field variable, which then existed, no matter what!
It' s pretty simple: you add the code given by Kieran Huggins to your existing code (the one that displays the unsorted data) and you put in you table headers a link to current page in which you add sort=NameAsc something like this
You mean like this? No it's still sorting the whole database instead of sorting only the query results from the 1st query. I don't want it to re-query & sort all the records in the whole database. I want it to sort only the query results from the 1st query.
session_start(); // this starts/resumes the session
//google "PHP session tutorial" if you don't know what this is
//the 1st query that comes from a html form
if(isset($_POST['Text_Box_17'])){
$Text_Box_17 = $_SESSION['Text_Box_17'] = $_POST['Text_Box_17']; // double assignment, they're all equal
}else{
$Text_Box_17 = $_SESSION['Text_Box_17']; // re-assign from the session variable
}
also your first column link is "sort=date" and should be "sortby=date"
//the sorting part
if(isset($_GET['sortby'])) $query .= ' ORDER BY '.$_GET['sortby'].' ASC';
//... and for good measure:
$result=mysql_query($query) or die('query error: '.MySQL_error());
The old way, you were assigning something to the field variable, which then existed, no matter what!
Keiran Huggins in "putting unescaped $_GET data straight into SQL" shocker!