Time for action – sorting and retrieving data
Let's
look at the following code. This code is made up of two parts; the first part is taking care of preparing the sorting statement by looking into the sort_column
and sort_direction
items. Using these, it builds an ORDER BY
SQL statement.
The second part is then selecting different fields from two combined tables and returning the items in the requested sorting order by using the previously defined SQL statement:
// Take care of the sorting, did the user select any column // to be sorted ? if ( isset($_REQUEST["sort_column"])) { // Did the user select a column that is actually sortable ? if ( ( $_REQUEST["sort_column"] == 'Id' ) || ( $_REQUEST["sort_column"] == 'hostId' ) || ( $_REQUEST["sort_column"] == 'contactAddress' ) || ( $_REQUEST["sort_column"] == 'longitude' ) || ( $_REQUEST["sort_column"] == 'latitude' ) ) { // What direction should the table be sorted, ascending...