Tuesday, 24 May 2016

How to Make Simple Pagination using PHP MySql


Are you searching for simple PHP Pagination for a starting level of php programmer then you are at the correct place and this PHP pagination script or example is very well helpful to you. Pagination is a style to make partition between content into different pages. Here we can define every pages as a separate URL. By click on that URL or number of that page, user can visit the content of that Page. For every page has number which is in incremental Page number. You can use this simple PHP pagination script or PHP pagination code into your project. For making simple pagination I have used php programming code with mysqli. I Hope with this php pagination video or php pagination tutorial using PHP MYSQL will be helpful to you to get the simple learning about the pagination as a beginner in php.

Source Code

Database


 --  
 -- Table structure for table `tbl_student`  
 --  
 CREATE TABLE IF NOT EXISTS `tbl_student` (  
  `student_id` int(11) NOT NULL AUTO_INCREMENT,  
  `student_name` varchar(250) NOT NULL,  
  `student_phone` varchar(20) NOT NULL,  
  PRIMARY KEY (`student_id`)  
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;  
 --  
 -- Dumping data for table `tbl_student`  
 --  
 INSERT INTO `tbl_student` (`student_id`, `student_name`, `student_phone`) VALUES  
 (1, 'Pauline S. Rich', '412-735-0224'),  
 (2, 'Sarah C. White', '320-552-9961'),  
 (3, 'Samuel L. Leslie', '201-324-8264'),  
 (4, 'Norma R. Manly', '478-322-4715'),  
 (5, 'Kimberly R. Castro', '479-966-6788'),  
 (6, 'Elaine R. Davis', '701-685-8912'),  
 (7, 'Concepcion S. Gardner', '607-829-8758'),  
 (8, 'Patricia J. White', '803-789-0429'),  
 (9, 'Michael M. Bothwell', '214-585-0737'),  
 (10, 'Ronald C. Vansickle', '630-571-4107'),  
 (11, 'Clarence A. Rich', '904-459-3747'),  
 (12, 'Elizabeth W. Peterson', '404-380-9481'),  
 (13, 'Renee R. Hewitt', '323-350-4973'),  
 (14, 'John K. Love', '337-229-1983'),  
 (15, 'Teresa J. Rincon', '216-394-6894'),  
 (16, 'Erin S. Huckaby', '503-284-8652');  

pagination.php


 <?php  
 $connect = mysqli_connect("localhost", "root", "", "test_db");  
 $record_per_page = 5;  
 $page = '';  
 if(isset($_GET["page"]))  
 {  
      $page = $_GET["page"];  
 }  
 else  
 {  
      $page = 1;  
 }  
 $start_from = ($page-1)*$record_per_page;  
 $query = "SELECT * FROM tbl_student order by student_id DESC LIMIT $start_from, $record_per_page";  
 $result = mysqli_query($connect, $query);  
 ?>  
 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>Webslesson Tutorial | Simple Pagination using PHP</title>  
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
           <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
      </head>  
      <body>  
           <br /><br />  
           <div class="container">  
                <h3 align="center">Simple Pagination using PHP MySql</h3><br />  
                <div class="table-responsive">  
                     <table class="table table-bordered">  
                          <tr>  
                               <td>Name</td>  
                               <td>Phone</td>  
                          </tr>  
                          <?php  
                          while($row = mysqli_fetch_array($result))  
                          {  
                          ?>  
                          <tr>  
                               <td><?php echo $row["student_name"]; ?></td>  
                               <td><?php echo $row["student_phone"]; ?></td>  
                          </tr>  
                          <?php  
                          }  
                          ?>  
                     </table>  
                     <div align="center">  
                     <?php  
                     $page_query = "SELECT * FROM tbl_student ORDER BY student_id DESC";  
                     $page_result = mysqli_query($connect, $page_query);  
                     $total_records = mysqli_num_rows($page_result);  
                     $total_pages = ceil($total_records/$record_per_page);  
                     for($i=1; $i<=$total_pages; $i++)  
                     {  
                          echo "<a href='pagination.php?page=".$i."'>".$i."</a> ";  
                     }  
                     ?>  
                     </div>  
                </div>  
           </div>  
      </body>  
 </html>  

1 comment:

  1. if pages accedes more 300 and if we hide some of page links like google etc, than what be the script

    ReplyDelete