PHP Pagination
Submitted by GeePee on Monday, March 16, 2015 - 21:08.
In this tutorial I’m going to show you how to create a pagination. The concept behind pagination, let’s say we have one million records in our database and we probably we don’t want to see that entire record in the our web browser at once. Instead we’re going to return a subset of results and the first page may consist 20 records on it. And then we have another link to our next page that has other sets of record and you go back to the previous records.
Any Paginated web page may require two sets of information, the Subset of records on the current page and the information about the complete set of records.
To start with this application, create a new folder in our root directory called ”pagination”. Then if you don’t have a twitter bootstrap framework, you need to download this framework now for this well be using it for this project. Then extract it and copy the three folders such as: css,fonts and js and paste it inside pagination folder located at the local server.
Here’s the folder structure:
pagination/
├── css/
│ ├── bootstrap.css
│ ├── bootstrap.min.css
│ ├── bootstrap-theme.css
│ ├── bootstrap-theme.min.css
├── js/
│ ├── bootstrap.js
│ ├── bootstrap.min.js
└── fonts/
├── glyphicons-halflings-regular.eot
├── glyphicons-halflings-regular.svg
├── glyphicons-halflings-regular.ttf
└── glyphicons-halflings-regular.woff
Next, here’s the MySQL table structure used in this application.
At this time we will create a new PHP file called “pagination.php”, then add the following code.
The code below has an explanation in every line so that it could guides you to understand the concepts and the importance of using the pagination.
After executing this application, it should look like as shown below:
- CREATE TABLE IF NOT EXISTS `employees` (
- `EMPLOYEE_ID` INT(11) NOT NULL,
- `FIRST_NAME` VARCHAR(255) DEFAULT NULL,
- `LAST_NAME` VARCHAR(255) DEFAULT NULL,
- `EMAIL` VARCHAR(255) DEFAULT NULL,
- `PHONE_NUMBER` VARCHAR(255) DEFAULT NULL,
- `HIRE_DATE` datetime DEFAULT NULL,
- `JOB_ID` VARCHAR(255) DEFAULT NULL,
- `SALARY` INT(11) DEFAULT NULL,
- `COMMISSION_ID` INT(11) DEFAULT NULL,
- `MANAGER_ID` INT(11) DEFAULT NULL,
- `DEPARTMENT_ID` INT(11) DEFAULT NULL,
- PRIMARY KEY (`EMPLOYEE_ID`),
- KEY `COMMISSION_ID` (`COMMISSION_ID`),
- KEY `DEPARTMENT_ID` (`DEPARTMENT_ID`),
- KEY `JOB_ID` (`JOB_ID`),
- KEY `MANAGER_ID` (`MANAGER_ID`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
- --
- -- Dumping data for table `employees`
- --
- INSERT INTO `employees` (`EMPLOYEE_ID`, `FIRST_NAME`, `LAST_NAME`, `EMAIL`, `PHONE_NUMBER`, `HIRE_DATE`, `JOB_ID`, `SALARY`, `COMMISSION_ID`, `MANAGER_ID`, `DEPARTMENT_ID`) VALUES
- (100, 'steven', 'King', 'sKing', '213-231-2313', '1987-06-17 00:00:00', 'AD_PRESS', 24000, NULL, 100, 90),
- (101, 'neena', 'Kochar', 'nKochar', '123-1231-212', '1989-09-21 00:00:00', 'AD_VP', 17000, NULL, 100, 90),
- (102, 'lex', 'De Haan', 'lDe Haan', '113-2122-2121', '1990-01-13 00:00:00', 'AD_VP', 17000, NULL, 100, 90),
- (103, 'alexander', 'Hunold', 'aHunold', '211-4515-1515', '1991-05-21 00:00:00', 'IT_PROG', 9000, NULL, 102, 60),
- (104, 'Bruce', 'Ernst', 'bErnst', '515-541-151', '1999-07-02 00:00:00', 'IT_PROG', 6000, NULL, 103, 60),
- (107, 'Diana', 'Lorents', 'dLorents', '515-515-5156', '1999-11-16 00:00:00', 'IT_PROG', 4200, NULL, 103, 60),
- (124, 'Kevin', 'Mourgos', 'kMourgos', '559-887-333', '1995-10-17 00:00:00', 'IT_PROG', 5800, NULL, 100, 50),
- (141, 'Trenne', 'Rajs', 'tRajs', '212-548-989', '1997-01-29 00:00:00', 'ST_MAN', 3500, NULL, 124, 50),
- (142, 'Curtis', 'Davies', 'cDavies', '333-999-777', '1998-03-15 00:00:00', 'ST_CLERK', 3100, NULL, 124, 50),
- (143, 'Randal', 'Matos', 'rMatos', '4444-5654-54', '1998-09-07 00:00:00', 'ST_CLERK', 2600, NULL, 124, 50),
- (144, 'Peter', 'Vargas', 'pVargas', '6565-5641-87', '2000-01-10 00:00:00', 'ST_CLERK', 2500, NULL, 124, 50),
- (149, 'Ellen', 'Zlotkey', 'eZlotkey', '656-4847-45', '1996-11-05 00:00:00', 'ST_CLERK', 10500, NULL, 100, 50),
- (174, 'Jonathan', 'Abel', 'jAbel', '5988-4556-564', '1999-05-24 00:00:00', 'SA_MAN', 11000, 0, 100, 80),
- (176, 'Kimberly', 'Taylor', 'kTaylor', '687-695-8754', '1987-09-17 00:00:00', 'SA_REP', 8600, 0, 149, 80),
- (178, 'Jinnefer', 'Grant', 'jGrant', '552-6541-897', '1996-02-17 00:00:00', 'SA_REP', 7000, 0, 149, 80),
- (200, 'Michael', 'Whalen', 'mWhalen', '2121-5465-541', '1997-08-17 00:00:00', 'SA_REP', 4400, 0, 149, NULL),
- (201, 'Pat', 'Hartstein', 'pHartstein', '14564-541-45', '1994-07-07 00:00:00', 'AD_ASST', 13000, NULL, 101, 10),
- (205, 'Shelley', 'Fay', 'sFay', '515-215-1156', '1994-07-07 00:00:00', 'MK_MAN', 6000, NULL, 100, 20),
- (206, 'William', 'Higgins', 'wHiggins', '566-112-5156', '1995-09-26 00:00:00', 'AC_MGR', 12000, NULL, 201, 20),
- (207, 'hatch', 'Glets', 'hGlets', '556-5465-515', '1989-03-07 00:00:00', 'AC_ACCOUNT', 8300, NULL, 101, 110);
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <meta content="width=device-width, initial-scale=1.0" name="viewport">
- <meta content="" name="description">
- <meta content="" name="author">
- <link href="" rel="shortcut icon">
- <title>Pagination</title><!-- Bootstrap core CSS -->
- <link href="css/bootstrap.css" rel="stylesheet">
- <link href="css/bootstrap-responsive.css" rel="stylesheet">
- </head>
- <body>
- <div class="container">
- <div class="well">
- <h2>Pagination</h2>
- </div>
- <div class="well">
- <table class="table table-condensed">
- <thead>
- <tr>
- <th>Employee ID</th>
- <th>Last Name</th>
- <th>First Name</th>
- <th>Email</th>
- <th>Salary</th>
- </tr>
- </thead>
- <tbody>
- <?php
- //create a mySQL connection
- $dbhost = 'localhost';
- $dbuser = 'root';
- $dbpass = '';
- if (!$conn) {
- }
- /* Get total number of records */
- $sql = "SELECT count(*) FROM employees ";
- if (!$retval) {
- }
- //this is the current page per number ($current_page)
- //record per Page($per_page)
- $per_page = 5;
- //total count record ($total_count)
- $total_count = $row[0];
- //it gets the result of total_count over per page
- $total_pages = $total_count/$per_page;
- //get the off set current page minus 1 multiply by record per page
- $offset = ($current_page - 1) * $per_page;
- //move to previous record by subtracting one into the current record
- $previous_page = $current_page - 1;
- //mvove to next record by incrementing the current page by one
- $next_page = $current_page + 1;
- //check if previous record is still greater than one then it returns to true
- $has_previous_page = $previous_page >= 1 ? true : false;
- //check if Next record is still lesser than one total pages then it returns to true
- $has_next_page = $next_page <= $total_pages ? true : false;
- //find records of employee and we specify the offset and the limit record per page
- $sql = "SELECT employee_id, LAST_NAME, FIRST_NAME, EMAIL, salary FROM employees LIMIT {$per_page} OFFSET {$offset}";
- if (!$retval) {
- }
- echo '<tr>';
- echo '<td>' . $row['employee_id'] . '</td>';
- echo '<td>' . $row['LAST_NAME'] . '</td>';
- echo '<td>' . $row['FIRST_NAME'] . '</td>';
- echo '<td>' . $row['EMAIL'] . '</td>';
- echo '<td>' . $row['salary'] . '</td>';
- }
- echo '</tr>';
- echo '</tbody>';
- echo '</table>';
- echo '<ul class="pagination" align="center">';
- if ($total_pages > 1){
- //this is for previous record
- if ($has_previous_page){
- echo ' <li><a href=pagination.php?page='.$previous_page.'>« </a> </li>';
- }
- //it loops to all pages
- for($i = 1; $i <= $total_pages; $i++){
- //check if the value of i is set to current page
- if ($i == $current_page){
- //then it sset the i to be active or focused
- echo '<li class="active"><span>'. $i.' <span class="sr-only">(current)</span></span></li>';
- }else {
- //display the page number
- echo ' <li><a href=pagination.php?page='.$i.'> '. $i .' </a></li>';
- }
- }
- //this is for next record
- if ($has_next_page){
- echo ' <li><a href=pagination.php?page='.$next_page.'>»</a></li> ';
- }
- }
- echo '</ul>';
- ?>
- </tbody>
- </table>
- </div>
- </div>
- </body>
- </html>
Add new comment
- 264 views