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> | ";

}

}

?>



Friday, February 24, 2023

Cookie Vanilla Javascript


 

1. Set Cookie

 document.cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";

2. Get Cookie

let cookie = {};

        var a = document.cookie.split(';');

        for(var i=0; i<a.length; i++){

            var b = a[i].split('=');

                var [key, value]  = b;

                cookie[key.trim()] = value;

        }

  console.log(cookie['_ga']);

3. ChangeCookie 

document.cookie = "username=John Smith; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";

Saturday, February 18, 2023

Format Number PHP dan JS

 1. php

<?php

function rupiah($angka){

$hasil_rupiah = "Rp " . number_format($angka,0,',','.');

return $hasil_rupiah;

}

echo rupiah(1000000);

?>


2. Js

<script>

let Rupiah = new Intl.NumberFormat('id-ID');

console.log(` ${Rupiah.format(123958)}`);

</script>

Set Cookie dan Remove Cookie dengan php vanilla.js

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