Pagination of Data (Paging) using PHP (Hyper Text Pre Processor) and MySQL (the great database)
First: Create a table named user_result in your MySQL Server
|
CREATE TABLE user_result( |
Second: Insert some large amount of data into the Table. As without large amount of data you cannot see the full functionality of this script .l
So I think you must insert near about 50 rows to the table.
Thrid: Test this script
Create a file named pagination.php and put the below contents to that file. Upload that file to your local server or main server.
I have uploaded that on my local server where php is running. If you are testing this script elsewhere then change http://localhost with your server host name.
|
<?php
$link = mysql_connect(’server_name’,'user_name’,'password’) mysql_select_db(‘test_db’) or //**EDIT TO YOUR TABLE NAME, ETC. $t = mysql_query(“SELECT count(*) FROM `user_result` as cnt’”);
echo “Total Number of records in Database: “.$total_items; $limit= $_GET['limit']; //Set default if: $limit is empty, non numerical, //less than 10, greater than 50 //Set default if: $page is empty, non numerical, if((!$page) || (is_numeric($page) == false) //calculate total pages $total_pages = ceil($total_items / $limit); //query: **EDIT TO YOUR TABLE NAME, ETC. $q = mysql_query(“SELECT * FROM `user_result` if(!$q) die(mysql_error()); $no_row = mysql_num_rows($q); if($no_row == 0) die(“No matches met your criteria.”); //Results per page: **EDIT LINK PATH** echo(“ //show data matching query: while($code = mysql_fetch_object($q)) { //print your data here. echo(“————————————–”); } //prev. page: **EDIT LINK PATH** $prev_page = $page – 1; if($prev_page >= 1) { //Display middle pages: **EDIT LINK PATH** for($a = 1; $a <= $total_pages; $a++) //Next page: **EDIT THIS LINK PATH** $next_page = $page + 1; //all done |
Four: Run this script in browser and see pagination with php and mysql in action.
Explanation: First this script will query the database and get total number of rows by the “SELECT count(*) FROM `user_result` as cnt’” query. It will return the count as cnt. Now get the count and store it in the valirable $total_items.
Now get the limit and page number from the GET variables. For the first time both will be null, so by default we will set the limit to 10 and page to 1.
If this variables are not blank, i.e. if the users click on the next or previous or page links then the page numeber and number of items to displayed will be not null and we will display data accordingly.
If some how this limit and page values are greater than the specified values or less than the specified values we will set them as default.
Now we will calculate how many pages the data will span. i.e. if we select 10 data to displayed at one time and total number of data are 85 then there will be 9 pages and by default the first page will be displayed. In this way if we choose to display 25 data at one time then the total number of pages will be 4, first 3 will display 25 data each and the last will display 10 data.
Hope this tutorial will help you to explre the vast world of pagination using php and mysql.
Kindly send me any queries that you might have.
Filed under: Basic Scripts, Php-MySQL, Scripts, mysql, php, sql | Tagged: get, limit, limit data, mysql, mysql-limit, page data, pagination, paging, php, request, set, sql limit

Help full script
thank you for the script,
I had to clean up the code a bit to make it work,
much happier when it looks like:
<?php
$per_page = 10;
//REMEMBER TO CONNECT TO DATABASE!
$link=mysql_connect('localhost','USERNAME','PASSWORD');
mysql_select_db('DATABASE');
//**EDIT TO YOUR TABLE NAME, ECT.
$t = mysql_query('SELECT * FROM `users`');
if(!$t) die(mysql_error());
$a= mysql_fetch_object($t);
$total_items= mysql_num_rows($t);
echo 'Total'.$total_items;
$limit= $_GET['limit'];
$page= $_GET['page'];
//set default if: $limit is empty, non numerical, less than 10, greater than 50
if((!$limit) || (is_numeric($limit) == false) || ($limit < $per_page) ) {
$limit = $per_page; //default
}
//set default if: $page is empty, non numerical, less than zero, greater than total available
if((!$page) || (is_numeric($page) == false) || ($page $total_items)) {
$page = 1; //default
}
//calcuate total pages
$total_pages = ceil($total_items / $limit);
$set_limit = ($page * $limit) - $limit;
echo '' . $set_limit . '';
//query: **EDIT TO YOUR TABLE NAME, ECT.
$q = mysql_query("SELECT * FROM `users` LIMIT $set_limit, $limit");
if(!$q) die(mysql_error());
if(mysql_num_rows($q) == 0) die('No matches met your criteria.');
//Results per page: **EDIT LINK PATH**
echo('
10 |
25 |
50
');
//show data matching query:
while($code = mysql_fetch_object($q)) {
//print your data here.
echo('————————————–');
echo('Name: '.$code->username.'');
echo('Math Marks: '.$code->email.'');
echo('Physics Marks: '.$code->usertype.'');
echo('————————————–');
}
//prev. page: **EDIT LINK PATH**
$prev_page = $page - 1;
if($prev_page >= 1) {
echo('<< Prev.');
}
//Display middle pages: **EDIT LINK PATH**
for($a = 1; $a <= $total_pages; $a++)
{
if($a == $page) {
echo(" $a | "); //no link
} else {
echo(" $a | ");
}
}
//next page: **EDIT THIS LINK PATH**
$next_page = $page + 1;
if($next_page <= $total_pages) {
echo("Next > >");
}
//all done
?>