CREATE TABLE `blogpost` ( `id` int(11) NOT NULL, `title` text NOT NULL, `crDate` varchar(20) NOT NULL, `crEditdate` varchar(20) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
<?php class Database { private static $dbName = 'demo'; private static $dbHost = 'localhost'; private static $dbUsername = 'xxxxx'; private static $dbUserPassword = 'xxxxx'; private static $lan = 'utf8'; private static $con = null; public function _construct(){ die('Init function is not allowed'); } public static function connect(){ if( null == self::$con){ try{ self::$con = new PDO("mysql:host=".self::$dbHost.";"."dbname=".self::$dbName, self::$dbUsername, self::$dbUserPassword); self::$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e){ die($e->getMessage()); } } return self::$con; } public static function disconnect(){ self::$con = null; } } class paging{ public function paging_data($table_name , $wherecaluse , $perpage ){ $pdo = database::connect(); $sql = "SELECT count(*) FROM $table_name where $wherecaluse"; $stmt_paging = $pdo->prepare($sql); $stmt_paging->execute(); $number = $row_paging = $stmt_paging->fetch(); $total_page=$number[0]; $totalpages = ceil($total_page/$perpage); return $totalpages; } } ?>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Pagination using php </title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <?php include"database.php"; $table_name="blogpost"; $wherecaluse="1"; $obj = new paging(); $page = (isset($_GET['page']) && $_GET['page'] > 0) ? (int)$_GET['page'] : 1; $perpage = 5; $limit = ($page > 0) ? ($page*$perpage) - $perpage : 0; $pdo = Database::connect(); $paging_result = $obj->paging_data($table_name,$wherecaluse , $perpage); ?> <div class="container"> <table class="table table-striped table-bordered" style="margin-top:120px;"> <thead> <tr class="danger"> <th>SN</th> <th>Title <?php echo $totalpages; ?></th> </tr> </thead> <tbody> <?php $pdo=Database::connect(); $sql = "SELECT * FROM blogpost ORDER by id DESC LIMIT {$limit} , {$perpage} "; $stmt = $pdo->prepare($sql); $stmt->execute(); if($stmt) { $sn=1; while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { ?> <tr> <td><?php echo $sn; ?></td> <td><?php echo $row['title']; ?></td> </tr> <?php $sn++; } } else { ?> <tr><td colspan="6"><P class="alert alert-info">No Record Found</P></td></tr> <?php } Database::disconnect(); ?> </tbody> </table> </div> <div class="container" style="margin-top:20px;"> <div class="col-sm-12 col-lg-12"> <ul style="list-style-type:none;"> <li style="display:inline-block;"><a href="?page=<?php $pagep=$page-1; echo $pagep; ?>" class="btn btn-default"> << Pre</a></li> <?php $totalpages; for($i=1; $i<=$paging_result; $i++) { ?> <li style="display:inline-block;"><a href="?page=<?php echo $i; ?>" class="btn btn-default"><?php echo $i; ?></a></li> <?php } ?> <li style="display:inline-block;"><a href="?page=<?php $pagen = $page+1; echo $pagen;?>" class="btn btn-default">next >></a></li> </ul> </div> </div> </body> </html>