Monday, February 27, 2023

Membuat Api Pagination dengan PHP MYSQL

API PHP SCRIPT

      <?php

      error_reporting(0);

      header("Access-Control-Allow-Origin: *");

header("Access-Control-Allow-Credentials: true");

header("Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS");

header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");

header("Content-Type: application/json; charset=utf-8");

$koneksi = mysqli_connect("localhost","root","","db_toko_online");

$batas=2;

$halaman=$_GET['halaman'];

if(empty($halaman)){

$posisi=0;

$halaman=1;

}else{

$posisi=($halaman-1)*$batas;

}

$sql = mysqli_query($koneksi,"select * from produk limit $posisi,$batas");

$row = array();

while($data = mysqli_fetch_assoc($sql)){

$row[] = $data;

}

echo json_encode($row);

?>

CALL DATA API PHP

<?php

    error_reporting(0);

if(empty($_GET['halaman'])){

$data = file_get_contents('http://localhost/testing-disini/trik/api_paging.php');

}else{

$data = file_get_contents('http://localhost/testing-disini/trik/api_paging.php?halaman='.$_GET['halaman']);

}

$array = json_decode($data,true);

for($i=0;$i<count($array);$i++){

echo "<p>".$array[$i]['id_produk']."/".$array[$i]['nama_produk']."</p>";

}


$total = 9;

for($i=1;$i<=$total;$i++){

if ((($i >= $_GET['halaman'] - 3) && ($i <= $_GET['halaman'] + 3)) || ($i == 1) || ($i == $total)){

echo "<a href='?halaman=".$i."'>".$i."</a> | ";

}

}

?>



No comments:

Post a Comment

Set Cookie dan Remove Cookie dengan php vanilla.js

< html lang = "en" > < head >     < meta charset = "UTF-8" >     < meta name = "viewport...