Sorting records data by column header in paging script

Read the Basic paging script here
Paging script can be customized further to add more features. One of the feature is to manage the display the records in order of the data. This way data can be sorted based on the columns names. Please read the basic php paging script to understand how the paging works. Now to this script we will add column header sorting feature. Here users can click the column names as hyperlinks and manage the display order by sorting the data.

Here we will use all column headers ad hyperlink and take the column name to the address bar. Then collect the column name from address bar and add the order by query using the column name. Note the hyper link for a column header here.

<a href='$page_name?column_name=name'>Name</a>

Now the page will reload with the column name at the address bar. Then we will collect this column name by this line.

@$column_name=$_GET['column_name'];

Since the data for the variable $column_name we are getting from the query string and we are going to use in our sql , we must check and sanitize the data before using. The data should be alpha numeric only, so let us use the function ctype_alnum and check.

if(strlen($column_name) > 0 and !ctype_alnum($column_name)){
echo "Data Error";
exit;
}

After this we will use one if condition to add order by clause to query. Like this .

if(isset($column_name) and strlen($column_name)>0){
$query = $query . " order by $column_name";
}


After this for each subsequent page we have to carry the column name through bottom links, so we have to add &column_name=$column_name to each link.

When we are not in the starting page and we decide to sort any column head then the script will return to first page or starting page. This is kept like this as we are inside the paging pages ( say 3rd page ) and on click of the column head then the total order of the query changes so the records of 3rd page changes. This may create some confusion or may not be required. So it is better to start from staring page if we are sorting by column head.

However if you want the page to stay at 3rd page only with different set of records matching the new sql then you can just add

&start=$start

to the hyper link of the column heads. This way we can tell the script to start from different page. For example here is the hyper link for the column name.

<a href='$page_name?column_name=name&start=$start'>Name</a>

Demo of paging script with Column header control

download Php Paging  Script
Visitors Rating
Your Rating




Google+

Post Comment This is for short comments only. Use the forum for more discussions.




HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer