<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.
&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
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.