Simple tutorial of pagination in php

In this PHP tutorial We will learn how to create simple pagination in PHP. The pagination can be ajax based but here i am creating pagination with NON ajax.In this tutorial I am using bootstrap3 and PHP5.I am assuming you have table and data.

I am following below simple steps:

Step 1: I am connecting database with table.
Step 2: I am filling selected data into tables.
Step 3: I am creating pagination link and added bottom of table.

Checkout other tutorials of pagination,

The below source code help you to create pagination in PHP.Its very simple code to generate pagination with help of bootstrap.

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<link href="bootstrap3.css" rel="stylesheet">  
<?php  
$dbhost = 'localhost';  
$dbuser = 'root';  
$dbpass = ";  
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');  
$dbname = 'test';  
$connection = mysql_select_db($dbname);  
  
$limit = 2;  
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; };  
$start_from = ($page-1) * $limit;  
  
$sql = "SELECT * FROM posts ORDER BY title ASC LIMIT $start_from, $limit";  
$rs_result = mysql_query ($sql);  
?>  
<table class="table table-bordered table-striped">  
<thead>  
<tr>  
<th>title</th>  
<th>body</th>  
</tr>  
<thead>  
<tbody>  
<?php  
while ($row = mysql_fetch_assoc($rs_result)) {  
?>  
            <tr>  
            <td><? echo $row["title"]; ?></td>  
            <td><? echo $row["body"]; ?></td>  
            </tr>  
<?php  
};  
?>  
</tbody>  
</table>  
<?php  
$sql = "SELECT COUNT(id) FROM posts";  
$rs_result = mysql_query($sql);  
$row = mysql_fetch_row($rs_result);  
$total_records = $row[0];  
$total_pages = ceil($total_records / $limit);  
$pagLink = "<div class='pagination'>";  
for ($i=1; $i<=$total_pages; $i++) {  
             $pagLink .= "<a href='index.php?page=".$i."'>".$i."</a>";  
};  
echo $pagLink . "</div>";  
?>
Result:

pagination in php

you can check live domo and download source code

  • Proxenix

    Hello.
    I’ve tried some scripts to make the active state on navigation, but with no success. Can you provide me some directions for how to generate the active state nav on active page? Thanks.

    • Ya sure,
      You can use jQuery live event on nav link like below:
      $(‘.navlink’).live(‘click’, function({
      $(‘.navlink’).removeClass(‘active’);
      $(this).addClass(‘active’);

      });

      This code fired on your navigation links which has ‘navlink’ class.We first remove active class and then add active class on current clicked navigation.
      I hope its help.

  • adminawala

    not work dude..

  • Nehara

    tnx dude! it’s wrkng perfectly!

  • thank…

    dwi from Indonesia

  • Dileep Kumar

    thanks

  • Chintan Panchal

    thank you so much man you have saved my time. you have done job without any jquery or ajax plugins.

    • Nurwani Enn

      Do you know why I get ‘SQL error’?

      • can u plz post full error which r u getting

  • Ido N

    Hi!
    Thanks for this useful code!
    What if I’ve hundreds of pages?
    Any way to shrink it to few pages and “next” and “previous” buttons?

    • you can apply same method but on anchor link set ur pages url

  • Gautam

    Thx man works perfect!!!!!!!!!!!!!!!!!!!!!!!!!!

  • Pulkit Kashyap

    this is awesome, thanks man

  • Santosh Lourembam

    $pagLink = “”;

    can any1 please explain this line where this class is written

    • this line will create pagination main div