infolink

Friday 15 April 2016

Server side pagination in php

Pagination has two types
Server side pagination in php
pagination (Server side pagination) in php

  1.  Server Side Pagination
  2. Client Side Pagination

In this tutorial we will learn server side pagination technique. To select from these two’s that which one is the best approach, then it’s totally depend on number of rows. Server side pagination technique used when number of rows is more.

Now let’s start example step by step.



Step 1:

Create database like "tutorials" and then create table "BOOKS_TBL" with more then 10 record. below is the script of table and data.

 --  
 -- Table structure for table `books_tbl`  
 --  
 CREATE TABLE IF NOT EXISTS `books_tbl` (  
  `BOOK_ID` int(11) NOT NULL AUTO_INCREMENT,  
  `BOOK_NAME` varchar(100) NOT NULL,  
  PRIMARY KEY (`BOOK_ID`),  
  UNIQUE KEY `BOOK_NAME` (`BOOK_NAME`)  
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=33 ;  
 --  
 -- Dumping data for table `books_tbl`  
 --  
 INSERT INTO `books_tbl` (`BOOK_ID`, `BOOK_NAME`) VALUES  
 (10, 'Biology'),  
 (11, 'Book 1'),  
 (20, 'Book 10'),  
 (21, 'Book 11'),  
 (22, 'Book 12'),  
 (23, 'Book 13'),  
 (24, 'Book 14'),  
 (25, 'Book 15'),  
 (26, 'Book 16'),  
 (27, 'Book 17'),  
 (28, 'Book 18'),  
 (29, 'Book 19'),  
 (12, 'Book 2'),  
 (30, 'Book 20'),  
 (31, 'Book 21'),  
 (32, 'Book 22'),  
 (13, 'Book 3'),  
 (14, 'Book 4'),  
 (15, 'Book 5'),  
 (16, 'Book 6'),  
 (17, 'Book 7'),  
 (18, 'Book 8'),  
 (19, 'Book 9'),  
 (9, 'Chemistry'),  
 (1, 'English A'),  
 (2, 'English B'),  
 (7, 'General Science'),  
 (5, 'Mathematics'),  
 (8, 'Physics'),  
 (6, 'Social Studies'),  
 (3, 'Urdu A'),  
 (4, 'Urdu B');  

Step 2:

create a file with the name of "pagination.php". and insert the following Code in this file.


 <?php   
 error_reporting(0);  
 $num_rec_per_page=10;  // set the number of records display at per page
 $con = mysql_connect('localhost','root','');  
 mysql_select_db('tutorials');  
// bellow mentioned if set the page number, means which page you want to visit like pagination?page=1
 if (isset($_GET["page"]))   
 {   
      $page = $_GET["page"];   
 }   
 else   
 {   
      $page=1;  
 }  
 $start_from = ($page - 1) * $num_rec_per_page;  //  set page wise starting limit like if page 1 and the total records display 10 on per page starting limit from 0 and if page 2 starting limit 10 and so on.

 $sql = "SELECT * FROM BOOKS_TBL order by BOOK_ID LIMIT $start_from, $num_rec_per_page"; // query that returns the records for per page with respect to limit.
     
 $rs_result = mysql_query ($sql); //run the query  
 ?>   
 <html>  
 <head><title>Server Side Pagination Tutorial | www.developerqueries.blogspot.com</title></head>  
 <body>  
 <table>  
 <tr><td>ID</td><td>Name</td></tr>  
<!-- below mention php tag will display the data  -->
 <?php   
 while ($row = mysql_fetch_array($rs_result)) {   
 ?>   
       <tr>  
       <td><?php echo $row['BOOK_ID']; ?></td>  
       <td><?php echo $row['BOOK_NAME']; ?></td>        
       </tr>  
 <?php   
 }  
 ?>   

 </table>  

 <?php   // below mentioned php tags shows the pagination number like on click which page number related data show.

 $sql = "SELECT * FROM BOOKS_TBL";   
 $rs_result = mysql_query($sql); //run the query  
 $total_records = mysql_num_rows($rs_result); //count total number of records  
 $total_pages = ceil($total_records / $num_rec_per_page);   
 echo "<a href='pagination.php?page=1'>".'|<'."</a> "; // Go to first page   
 for ($i=1; $i<=$total_pages; $i++) {   
       echo "<a href='pagination.php?page=".$i."'>".$i."</a> ";   
 }  
 echo "<a href='pagination.php?page=$total_pages'>".'>|'."</a> "; // Go to last page  
 ?>  

 </body>  
 </html>  

No comments:

Post a Comment