Create pagination PHP PDO with bootstrap

Posting Komentar

Dalam proyek web, pagination adalah elemen paling penting dimana kita dapat menampilkan sejumlah data dari database Mysql ke halaman web. Kemudian pada saat itu Pagination, pagination akan membantu meningkatkan visibilitas User Interface milik Anda. Tutorial ini akan membantu Anda tentang bagaimana Anda bisa membuat pagination di PDO (PHP Data Object) menggunakan bootstrap. Di sini Kami telah membuat kode yang sangat sederhana namun sangat kuat untuk mengembangkan pagination dengan PHP PDO (PHP Data Object) Bootstrap.

Sebelum memulai project ini silakan buat tabel seperti di bawah ini :

Baca Juga : Cara membuat class crud php mysql

-- phpMyAdmin SQL Dump
-- version 4.2.11
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Oct 19, 2017 at 04:39 PM
-- Server version: 5.6.21
-- PHP Version: 5.6.3

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `database`
--

-- --------------------------------------------------------

--
-- Table structure for table `tbl_users`
--

CREATE TABLE IF NOT EXISTS `tbl_users` (
`id` int(11) NOT NULL,
  `first_name` varchar(25) NOT NULL,
  `last_name` varchar(25) NOT NULL,
  `email_id` varchar(50) NOT NULL,
  `contact_no` bigint(10) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `tbl_users`
--

INSERT INTO `tbl_users` (`id`, `first_name`, `last_name`, `email_id`, `contact_no`) VALUES
(1, 'Test1', 'lacodeid', 'example@gmail.com', 12345678),
(2, 'Test 2', 'yj', 'example@gmail.com', 12345678),
(3, 'Test 3', 'example', 'example@gmail.com', 12345678),
(4, 'example', 'crud', 'example@gmail.com', 12345678);

--
-- Indexes for dumped tables
--

--
-- Indexes for table `tbl_users`
--
ALTER TABLE `tbl_users`
 ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `tbl_users`
--
ALTER TABLE `tbl_users`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=5;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Karena disini saya menggunakan PHP PDO (PHP Data Object) Silakan Anda buat koneksi databasenya seperti dibawah ini name file dbconfig.php :

Baca Juga : PHP captcha code script send email

<?php
class database 
{
 private $dsn;
    public $default = array(
        'datasource' => 'Database/Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'root',
        'password' => '',
        'database' => 'database',
        'prefix' => 'tbl_',
        'encoding' => 'UTF8',
        'port' => '',
    );

    public function dbkoneksi() {
        static $instance;
        if ($instance === null) {
            $opt = array(
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                PDO::ATTR_EMULATE_PREPARES => FALSE,
            );
            $dsn = 'mysql:host=' .$this->default['host'] . ';dbname=' .$this->default['database'] . ';charset=' .$this->default['encoding'];
            $instance = new PDO($dsn, $this->default['login'], $this->default['password'], $opt);
        }
        return $instance;
    }
 
}

$dbcon = new database();
$dbs = $dbcon->dbkoneksi();
include_once 'class.crud.php';
$crud = new crud($dbs);
Lanjut anda buat satu file lagi class.crud.php script kode :
<?php

class crud
{
 private $db;
 function __construct($dbs)
 {
 $this->db = $dbs;
 }
 
 /* paging */
 public function dataview($query)
 {
  $stmt = $this->db->prepare($query);
  $stmt->execute();
  if($stmt->rowCount()>0)
  {
   $no=0;
   while($row=$stmt->fetch(PDO::FETCH_ASSOC))
   {
    $no++;
    ?>
                <tr>
                <td><?php echo $no; ?></td>
                <td><?php print($row['first_name']); ?></td>
                <td><?php print($row['last_name']); ?></td>
                <td><?php print($row['email_id']); ?></td>
                <td><?php print($row['contact_no']); ?></td>
                </tr>
                <?php
                $no++;
   }
  }
  else
  {
   ?>
            <tr>
            <td>Nothing here...</td>
            </tr>
            <?php
  }
  
 }
 
 public function paging($query,$records_per_page)
 {
  $starting_position=0;
  if(isset($_GET["page_no"]))
  {
   $starting_position=($_GET["page_no"]-1)*$records_per_page;
  }
  $query2=$query." limit $starting_position,$records_per_page";
  return $query2;
 }
 
 public function paginglink($query,$records_per_page)
 {
  
  $self = $_SERVER['PHP_SELF'];
  $stmt = $this->db->prepare($query);
  $stmt->execute();
  
  $total_no_of_records = $stmt->rowCount();
  
  if($total_no_of_records > 0)
  {
   ?><ul class="pagination"><?php
   $total_no_of_pages=ceil($total_no_of_records/$records_per_page);
   $current_page=1;
   if(isset($_GET["page_no"]))
   {
    $current_page=$_GET["page_no"];
   }
   
   if($current_page!=1)
   {
    $previous =$current_page-1;
    echo "<li><a href='".$self."?page_no=1'>First</a></li>";
    echo "<li><a href='".$self."?page_no=".$previous."'>Previous</a></li>";
   }

   for($i=1;$i<=$total_no_of_pages;$i++)
   {
    if($i==$current_page)
    {
     echo "<li><a href='".$self."?page_no=".$i."' style='color:red;'>".$i."</a></li>";
    }
    else
    {
     echo "<li><a href='".$self."?page_no=".$i."'>".$i."</a></li>";
    }
   }

   if($current_page!=$total_no_of_pages)
   {
    $next=$current_page+1;
    echo "<li><a href='".$self."?page_no=".$next."'>Next</a></li>";
    echo "<li><a href='".$self."?page_no=".$total_no_of_pages."'>Last</a></li>";
   }
   ?></ul><?php
  }
 }
 /* paging */
 
}
Sekarang buat file header.php script kode :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Pagination Bootstrap</title>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen"> 
</head>

<body>

<div class="navbar navbar-default navbar-static-top" role="navigation">
    <div class="container">
 
        <div class="navbar-header">
            <a class="navbar-brand" href="#" title='Programming Blog'>CRUD</a>
        </div>
 
    </div>
</div>
lalu anda buat file footer.php script kode :
<script src="bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
selanjutnya buat file index.php script kode :
<?php
    include_once 'dbconfig.php';
    include_once 'header.php'; 
?>

<div class="clearfix"></div>

<div class="container">
<a href="add-data.php" class="btn btn-large btn-info"><i class="glyphicon glyphicon-plus"></i> &nbsp; Add Records</a>
</div>

<div class="clearfix"></div><br/>

<div class="container">

<table class='table table-bordered table-responsive'>
     <tr>
     <th>#</th>
     <th>First Name</th>
     <th>Last Name</th>
     <th>E - mail ID</th>
     <th>Contact No</th>
     </tr>
     <?php
  $query = "SELECT * FROM tbl_users";       
  $records_per_page=3;
  $newquery = $crud->paging($query,$records_per_page);
  $crud->dataview($newquery);
  ?>
    <tr>
        <td colspan="7" align="center">
    <div class="pagination-wrap">
            <?php $crud->paginglink($query,$records_per_page); ?>
         </div>
        </td>
    </tr>
</table>
       
</div>
<?php include_once 'footer.php'; ?>
silakan dicoba dan di praktekan, bila ada pertanyaan silakan komentar

Related Posts

Posting Komentar