Tuesday 14 April 2015

Best Php Pagination System

I have tried lot of php pagination, But this is superb, very easy to use and so short.
connection.php

$db_username = 'root'; // Your MYSQL Username.
$db_password = ''; // Your MYSQL Password.
$db_name = 'database_name_here'; // Your Database name.
$db_host = 'localhost';
  
$conDB = mysqli_connect($db_host, $db_username, $db_password,$db_name)or die('Error: Could not connect to database.');

functions.php
function pagination($query,$per_page=10,$page=1,$url='?'){   
    global $conDB; 
    $query = "SELECT COUNT(*) as `num` FROM {$query}";
    $row = mysqli_fetch_array(mysqli_query($conDB,$query));
    $total = $row['num'];
    $adjacents = "2"; 
      
    $prevlabel = "‹ Prev";
    $nextlabel = "Next ›";
    $lastlabel = "Last ››";
      
    $page = ($page == 0 ? 1 : $page);  
    $start = ($page - 1) * $per_page;                               
      
    $prev = $page - 1;                          
    $next = $page + 1;
      
    $lastpage = ceil($total/$per_page);
      
    $lpm1 = $lastpage - 1; // //last page minus 1
      
    $pagination = "";
    if($lastpage > 1){   
        $pagination .= "
    ";         $pagination .= "
  • Page {$page} of {$lastpage}
  • ";                            if ($page > 1) $pagination.= "
  • {$prevlabel}
  • ";                        if ($lastpage < 7 + ($adjacents * 2)){               for ($counter = 1; $counter <= $lastpage; $counter++){                 if ($counter == $page)                     $pagination.= "
  • {$counter}
  • ";                 else                     $pagination.= "
  • {$counter}
  • ";                                }                    } elseif($lastpage > 5 + ($adjacents * 2)){                            if($page < 1 + ($adjacents * 2)) {                                    for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++){                     if ($counter == $page)                         $pagination.= "
  • {$counter}
  • ";                     else                         $pagination.= "
  • {$counter}
  • ";                                    }                 $pagination.= "
  • ...
  • ";                 $pagination.= "
  • {$lpm1}
  • ";                 $pagination.= "
  • {$lastpage}
  • ";                                     } elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) {                                    $pagination.= "
  • 1
  • ";                 $pagination.= "
  • 2
  • ";                 $pagination.= "
  • ...
  • ";                 for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) {                     if ($counter == $page)                         $pagination.= "
  • {$counter}
  • ";                     else                         $pagination.= "
  • {$counter}
  • ";                                    }                 $pagination.= "
  • ..
  • ";                 $pagination.= "
  • {$lpm1}
  • ";                 $pagination.= "
  • {$lastpage}
  • ";                                     } else {                                    $pagination.= "
  • 1
  • ";                 $pagination.= "
  • 2
  • ";                 $pagination.= "
  • ..
  • ";                 for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) {                     if ($counter == $page)                         $pagination.= "
  • {$counter}
  • ";                     else                         $pagination.= "
  • {$counter}
  • ";                                    }             }         }                        if ($page < $counter - 1) {                 $pagination.= "
  • {$nextlabel}
  • ";                 $pagination.= "
  • {$lastlabel}
  • ";             }                    $pagination.= "
";            }            return $pagination; }
index.php
include_once('connection.php');
include_once('functions.php');
 
$page = (int)(!isset($_GET["page"]) ? 1 : $_GET["page"]);
if ($page <= 0) $page = 1;
 
$per_page = 10; // Set how many records do you want to display per page.
 
$startpoint = ($page * $per_page) - $per_page;
 
$statement = "`records` ORDER BY `id` ASC"; // Change `records` according to your table name.
  
$results = mysqli_query($conDB,"SELECT * FROM {$statement} LIMIT {$startpoint} , {$per_page}");
 
if (mysqli_num_rows($results) != 0) {
     
    // displaying records.
    while ($row = mysqli_fetch_array($results)) {
        echo $row['name'] . '
'; } } else { echo "No records are found."; } // displaying paginaiton. echo pagination($statement,$per_page,$page,$url='?');
style.css
ul.pagination {
    text-align:center;
    color:#829994;
}
ul.pagination li {
    display:inline;
    padding:0 3px;
}
ul.pagination a {
    color:#0d7963;
    display:inline-block;
    padding:5px 10px;
    border:1px solid #cde0dc;
    text-decoration:none;
}
ul.pagination a:hover, 
ul.pagination a.current {
    background:#0d7963;
    color:#fff; 
}
Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
Posts RSSComments RSSBack to top
© 2015 Eraneed ∙ Designed by Templates Crowd
Released under Creative Commons 3.0 CC BY-NC 3.0