
- PHP Tutorial
- PHP - Home
- PHP - Introduction
- PHP - Environment Setup
- PHP - Syntax Overview
- PHP - Variable Types
- PHP - Constants
- PHP - Operator Types
- PHP - Decision Making
- PHP - Loop Types
- PHP - Arrays
- PHP - Strings
- PHP - Web Concepts
- PHP - GET & POST
- PHP - File Inclusion
- PHP - Files & I/O
- PHP - Functions
- PHP - Cookies
- PHP - Sessions
- PHP - Sending Emails
- PHP - File Uploading
- PHP - Coding Standard
- Advanced PHP
- PHP - Predefined Variables
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Bugs Debugging
- PHP - Date & Time
- PHP & MySQL
- PHP & AJAX
- PHP & XML
- PHP - Object Oriented
- PHP - For C Developers
- PHP - For PERL Developers
- PHP Form Examples
- PHP - Form Introduction
- PHP - Validation Example
- PHP - Complete Form
- PHP login Examples
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP AJAX Examples
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML Example
- PHP - XML Introduction
- PHP - Simple XML
- PHP - Simple XML GET
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Frame Works
- PHP - Frame Works
- PHP - Core PHP vs Frame Works
- PHP Design Patterns
- PHP - Design Patterns
- PHP Function Reference
- PHP - Built-In Functions
- PHP Useful Resources
- PHP - Questions & Answers
- PHP - Useful Resources
- PHP - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Using Paging through PHP
Its always possible that your SQL SELECT statement query may result into thousand of records. But its is not good idea to display all the results on one page. So we can divide this result into many pages as per requirement.
Paging means showing your query result in multiple pages instead of just put them all in one long page.
MySQL helps to generate paging by using LIMIT clause which will take two arguments. First argument as OFFSET and second argument how many records should be returned from the database.
Below is a simple example to fetch records using LIMIT clause to generate paging.
Example
Try out following example to display 10 records per page.
<html> <head> <title>Paging Using PHP</title> </head> <body> <?php $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $rec_limit = 10; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } mysql_select_db('test_db'); /* Get total number of records */ $sql = "SELECT count(emp_id) FROM employee "; $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not get data: ' . mysql_error()); } $row = mysql_fetch_array($retval, MYSQL_NUM ); $rec_count = $row[0]; if( isset($_GET{'page'} ) ) { $page = $_GET{'page'} + 1; $offset = $rec_limit * $page ; }else { $page = 0; $offset = 0; } $left_rec = $rec_count - ($page * $rec_limit); $sql = "SELECT emp_id, emp_name, emp_salary ". "FROM employee ". "LIMIT $offset, $rec_limit"; $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not get data: ' . mysql_error()); } while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) { echo "EMP ID :{$row['emp_id']} <br> ". "EMP NAME : {$row['emp_name']} <br> ". EMP SALARY : {$row['emp_salary']} <br> ". "--------------------------------<br>"; } if( $page > 0 ) { $last = $page - 2; echo "<a href = \"$_PHP_SELF?page = $last\">Last 10 Records</a> |"; echo "<a href = \"$_PHP_SELF?page = $page\">Next 10 Records</a>"; }else if( $page == 0 ) { echo "<a href = \"$_PHP_SELF?page = $page\">Next 10 Records</a>"; }else if( $left_rec < $rec_limit ) { $last = $page - 2; echo "<a href = \"$_PHP_SELF?page = $last\">Last 10 Records</a>"; } mysql_close($conn); ?> </body> </html>
php_and_mysql.htm
Advertisements