DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Zen
Zen

Posted on

Source code of JSON Server app (written in PHP)

Database structure

Database: json
Table: jsonData
Field: id, kunci, json

PHP codes

halaman/beranda.php

<!DOCTYPE html>
<html>
<head>
    <title>JSON</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="JSON Server">
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.min.css">
    <style type="text/css">
        h3 {
            text-align: center;
        }
        .tombolTambah {
            margin: auto;
        }
    </style>
</head>
<body>
    <h3>JSON Server</h3>
    <form method="post" action="olah">
        <input type="hidden" value="<?= md5(rand()) ?>" name="kunci">
        <textarea name="json" required></textarea>
        <input type="submit" class="tombolTambah" value="Tambahkan" name="">
    </form>
    <script type="text/javascript">
        tinggi = () => document.querySelector('textarea').style.height = `${window.innerHeight - 130}px`
        tinggi()
        window.addEventListener('resize', tinggi)
    </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

halaman/olah.php

<?php 
$db->prepare('insert into jsonData (kunci, json) values (:kunci, :json)')->execute([
    ':kunci' => $_POST['kunci'],
    ':json' => $_POST['json']
]);
header('Location: ' . $_POST['kunci']);
Enter fullscreen mode Exit fullscreen mode

halaman/tampil.php

<?php 
$data = $db->query('select json from jsonData where kunci = "' . $routes[1] . '"');
$hasil = '';
while ($row = $data->fetch()) {
    $hasil = $row['json'];
}
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST");
header('Content-Type: application/json');
echo $hasil;
Enter fullscreen mode Exit fullscreen mode

halaman/ubah.php

<?php 
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST");
if (!empty($_POST['json'])) {
    $db->prepare('update jsonData set json = :json where kunci = :kunci')->execute([
        ':json' => $_POST['json'],
        ':kunci' => $routes[1]
    ]);
}
Enter fullscreen mode Exit fullscreen mode

.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Enter fullscreen mode Exit fullscreen mode

index.php

<?php 
$db = new PDO('mysql:host=localhost;dbname=json', 'root', 'kucing');
$routes = explode('/', $_SERVER['PATH_INFO']);
if ($routes[1] == '') {
    include 'halaman/beranda.php';
} elseif ($routes[1] == 'olah') {
    include 'halaman/olah.php';
} elseif (strlen($routes[1]) > 0 && $routes[2] == 'ubah') {
    include 'halaman/ubah.php';
} elseif (strlen($routes[1]) > 0) {
    include 'halaman/tampil.php';
}
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
hyftar profile image
Simon Landry

If you'd like to take your repo to the next step you can try my educative / barebone PHP Framework

github.com/Hyftar/hyftar-php-frame...

50 CLI Tools You Can't Live Without

>> Check out this classic DEV post <<