-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
46 lines (37 loc) · 1.4 KB
/
functions.php
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
<?php
error_reporting(E_ERROR | E_PARSE);
include 'db.php';
$conn = new mysqli($servername, $username, $password, $dbname);
function query($query) {
global $conn;
$result = mysqli_query($conn, $query);
$rows = [];
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
return $rows;
}
function pagination($dataPerHalaman) {
// configure pagination
global $conn;
// menghitung jumlah data
$jumlahData = count(query("SELECT * FROM books"));
// menghitung jumlah halaman dgn cara membulatkan angka ke bilangan bulat keatas
$jumlahHalaman = ceil($jumlahData / $dataPerHalaman);
// menghitung halaman aktif
$halamanAktif = (isset($_GET['page'])) ? $_GET['page'] : 1;
if($halamanAktif <= 0) {
$halamanAktif = 1;
echo "<script>window.location.href=books.php?page=$halamanAktif;</script>";
}
elseif($halamanAktif > $jumlahHalaman) {
$halamanAktif = $jumlahHalaman;
echo "<script>window.location.href=books.php?page=$halamanAktif;</script>";
}
// menghitung data awal untuk setiap halaman
$dataAwal = ($dataPerHalaman * $halamanAktif) - $dataPerHalaman;
$sql = "SELECT * FROM books LIMIT $dataAwal,$dataPerHalaman";
$result = $conn->query($sql);
return array($result,$jumlahData,$dataAwal,$dataPerHalaman,$halamanAktif,$jumlahHalaman);
}
?>