Skip to content

Instantly share code, notes, and snippets.

@khannedy
Created September 22, 2011 16:58
Show Gist options
  • Select an option

  • Save khannedy/1235336 to your computer and use it in GitHub Desktop.

Select an option

Save khannedy/1235336 to your computer and use it in GitHub Desktop.
Membuat Pagination Sederhana di PHP dan Combo Box HTML
--
-- Table structure for table `employees`
--
CREATE TABLE IF NOT EXISTS `employees` (
`employee_id` int(6) NOT NULL DEFAULT '0',
`first_name` varchar(20) DEFAULT NULL,
`last_name` varchar(25) NOT NULL
) ENGINE=InnoDB;
<?php
/*
* Copyright (c) 2011, StripBandunk and/or its affiliates. All rights reserved.
*
* http://stripbandunk.com/
*
* STRIPBANDUNK PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
/**
* Description of Employee
*
* @author echo
*/
class Employee {
private $id;
private $firstName;
private $lastName;
public function getId() {
return $this->id;
}
public function setId($id) {
$this->id = $id;
}
public function getFirstName() {
return $this->firstName;
}
public function setFirstName($firstName) {
$this->firstName = $firstName;
}
public function getLastName() {
return $this->lastName;
}
public function setLastName($lastName) {
$this->lastName = $lastName;
}
}
?>
<?php
require_once 'Employee.php';
/*
* Copyright (c) 2011, StripBandunk and/or its affiliates. All rights reserved.
*
* http://stripbandunk.com/
*
* STRIPBANDUNK PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
/**
* Description of EmployeeDatabase
*
* @author Eko Kurniawan Khannedy
*/
class EmployeeDatabase {
private $connection;
function __construct(mysqli $connection) {
$this->connection = $connection;
}
public function count() {
$result = $this->connection->query('SELECT count(*) as total FROM employees');
$row = $result->fetch_array(MYSQLI_ASSOC);
return $row['total'];
}
public function find($first, $limit) {
$employees = array();
$statement = $this->connection->prepare("SELECT employee_id, first_name, last_name FROM employees LIMIT ?, ?");
$statement->bind_param("ii", $param1, $param2);
$param1 = $first;
$param2 = $limit;
$statement->execute();
$statement->bind_result($id, $firstName, $lastName);
while ($statement->fetch()) {
$employee = new Employee();
$employee->setId($id);
$employee->setFirstName($firstName);
$employee->setLastName($lastName);
$employees[] = $employee;
}
$statement->close();
return $employees;
}
}
?>
<?php
require_once 'Employee.php';
/*
* Copyright (c) 2011, StripBandunk and/or its affiliates. All rights reserved.
*
* http://stripbandunk.com/
*
* STRIPBANDUNK PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
/**
* Description of EmployeeDatabase
*
* @author Eko Kurniawan Khannedy
*/
class EmployeeDatabase {
private $connection;
function __construct(mysqli $connection) {
$this->connection = $connection;
}
public function count() {
}
public function find($first, $limit) {
}
}
?>
--
-- Dumping data for table `employees`
--
INSERT INTO `employees` (`employee_id`, `first_name`, `last_name`) VALUES
(100, 'Steven', 'King'),
(101, 'Neena', 'Kochhar'),
(102, 'Lex', 'De Haan'),
(103, 'Alexander', 'Hunold'),
(104, 'Bruce', 'Ernst'),
(105, 'David', 'Austin'),
(106, 'Valli', 'Pataballa'),
(107, 'Diana', 'Lorentz'),
(108, 'Nancy', 'Greenberg'),
(109, 'Daniel', 'Faviet'),
(110, 'John', 'Chen'),
(111, 'Ismael', 'Sciarra'),
(112, 'Jose Manuel', 'Urman'),
(113, 'Luis', 'Popp'),
(114, 'Den', 'Raphaely'),
(115, 'Alexander', 'Khoo'),
(116, 'Shelli', 'Baida'),
(117, 'Sigal', 'Tobias'),
(118, 'Guy', 'Himuro'),
(119, 'Karen', 'Colmenares'),
(120, 'Matthew', 'Weiss'),
(121, 'Adam', 'Fripp'),
(122, 'Payam', 'Kaufling'),
(123, 'Shanta', 'Vollman'),
(124, 'Kevin', 'Mourgos'),
(125, 'Julia', 'Nayer'),
(126, 'Irene', 'Mikkilineni'),
(127, 'James', 'Landry'),
(128, 'Steven', 'Markle'),
(129, 'Laura', 'Bissot'),
(130, 'Mozhe', 'Atkinson'),
(131, 'James', 'Marlow'),
(132, 'TJ', 'Olson'),
(133, 'Jason', 'Mallin'),
(134, 'Michael', 'Rogers'),
(135, 'Ki', 'Gee'),
(136, 'Hazel', 'Philtanker'),
(137, 'Renske', 'Ladwig'),
(138, 'Stephen', 'Stiles'),
(139, 'John', 'Seo'),
(140, 'Joshua', 'Patel'),
(141, 'Trenna', 'Rajs'),
(142, 'Curtis', 'Davies'),
(143, 'Randall', 'Matos'),
(144, 'Peter', 'Vargas'),
(145, 'John', 'Russell'),
(146, 'Karen', 'Partners'),
(147, 'Alberto', 'Errazuriz'),
(148, 'Gerald', 'Cambrault'),
(149, 'Eleni', 'Zlotkey'),
(150, 'Peter', 'Tucker'),
(151, 'David', 'Bernstein'),
(152, 'Peter', 'Hall'),
(153, 'Christopher', 'Olsen'),
(154, 'Nanette', 'Cambrault'),
(155, 'Oliver', 'Tuvault'),
(156, 'Janette', 'King'),
(157, 'Patrick', 'Sully'),
(158, 'Allan', 'McEwen'),
(159, 'Lindsey', 'Smith'),
(160, 'Louise', 'Doran'),
(161, 'Sarath', 'Sewall'),
(162, 'Clara', 'Vishney'),
(163, 'Danielle', 'Greene'),
(164, 'Mattea', 'Marvins'),
(165, 'David', 'Lee'),
(166, 'Sundar', 'Ande'),
(167, 'Amit', 'Banda'),
(168, 'Lisa', 'Ozer'),
(169, 'Harrison', 'Bloom'),
(170, 'Tayler', 'Fox'),
(171, 'William', 'Smith'),
(172, 'Elizabeth', 'Bates'),
(173, 'Sundita', 'Kumar'),
(174, 'Ellen', 'Abel'),
(175, 'Alyssa', 'Hutton'),
(176, 'Jonathon', 'Taylor'),
(177, 'Jack', 'Livingston'),
(178, 'Kimberely', 'Grant'),
(179, 'Charles', 'Johnson'),
(180, 'Winston', 'Taylor'),
(181, 'Jean', 'Fleaur'),
(182, 'Martha', 'Sullivan'),
(183, 'Girard', 'Geoni'),
(184, 'Nandita', 'Sarchand'),
(185, 'Alexis', 'Bull'),
(186, 'Julia', 'Dellinger'),
(187, 'Anthony', 'Cabrio'),
(188, 'Kelly', 'Chung'),
(189, 'Jennifer', 'Dilly'),
(190, 'Timothy', 'Gates'),
(191, 'Randall', 'Perkins'),
(192, 'Sarah', 'Bell'),
(193, 'Britney', 'Everett'),
(194, 'Samuel', 'McCain'),
(195, 'Vance', 'Jones'),
(196, 'Alana', 'Walsh'),
(197, 'Kevin', 'Feeney'),
(198, 'Donald', 'OConnell'),
(199, 'Douglas', 'Grant'),
(200, 'Jennifer', 'Whalen'),
(201, 'Michael', 'Hartstein'),
(202, 'Pat', 'Fay'),
(203, 'Susan', 'Mavris'),
(204, 'Hermann', 'Baer'),
(205, 'Shelley', 'Higgins'),
(206, 'William', 'Gietz');
$employees = $database->find(($currentPage - 1) * $pageSize, $pageSize);
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>StripBandunk - Change Pagination</title>
</head>
<body>
<?php
require_once 'Employee.php';
require_once 'EmployeeDatabase.php';
$connection = new mysqli('localhost', 'root', 'root', 'belajar_hr');
$database = new EmployeeDatabase($connection);
$currentPage = 1;
if (isset($_GET['page'])) {
$currentPage = $_GET['page'];
}
$pageSize = 10;
$total = $database->count();
$totalHalaman = ceil($total / $pageSize);
$employees = $database->find(($currentPage - 1) * $pageSize, $pageSize);
?>
<form method="get">
Halaman :
<select name="page" onchange="this.form.submit();">
<?php
for ($i = 1; $i <= $totalHalaman; $i++) {
?>
<option value="<?php echo $i; ?>"
<?php
if ($i == $currentPage) {
echo 'selected="selected"';
}
?>
>
<?php echo $i; ?>
</option>
<?php
}
?>
</select>
</form>
<table border="1">
<thead>
<tr>
<th>Employee Id</th>
<th>First name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<?php
foreach ($employees as $employee) {
?>
<tr>
<td><?php echo $employee->getId(); ?></td>
<td><?php echo $employee->getFirstName(); ?></td>
<td><?php echo $employee->getLastName(); ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
$connection->close();
?>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>StripBandunk - Change Pagination</title>
</head>
<body>
<?php
// put your code here
?>
</body>
</html>
require_once 'Employee.php';
require_once 'EmployeeDatabase.php';
$connection = new mysqli('localhost', 'root', 'root', 'belajar_hr');
$database = new EmployeeDatabase($connection);
$currentPage = 1;
if (isset($_GET['page'])) {
$currentPage = $_GET['page'];
}
$pageSize = 10;
$total = $database->count();
$totalHalaman = ceil($total / $pageSize);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment