Query to change the display order of records on a field dynamicallyWe can reverse the display order or change field on which the order of display is applied by managing the order by sql query. Even we can give this option to visitors to change the order of display as per their requirement. We will learn how to do this here. We will be using PHP in managing or changing the query as per the user clicks on the column header hyper link of the field.
Now let us take our student table we discussed in our basic sql order by tutorial. For our case we will take the records of class four only. We can write our query to display the records in the order of marks obtained by the students from highest to lowest. But here we will give an option for the visitor to change the order from lowest to highest or vice versa if required.
Here is the php code
Note that the value of $by we are setting before by an if logical condition and when ever user clicks the header link of the mark table the value of $by changes and the order by query also changes. The value of $by gets either “asc” or “desc”.
Here is the output of this query, click the field header of mark column and see how the order changes
Read how order by is used in php paging script.
This article is written by plus2net.com team.
▼ More on getting records from table with different combinations of commands