Membuat Web dengan MongoDB dan PHP

Sntaxmenggunakan database MongoDB  sebagai penyimpan database.
Config.php
<?php
require_once __DIR__ . "/vendor/autoload.php";
$collection = (new MongoDB\Client)->responsi->kelompokJogjaAntiCovid;
?>
index.php
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
<title>MongoDB</title>
<link rel="stylesheet" href="./vendor/twbs/bootstrap/dist/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<br>
<CENTER><h1>Data Covid Daerah Istimewa Yogyakarta</h1></CENTER>
<a href="create.php" class="btn btn-success">Tambah Kabupaten</a>
<?php
if (isset($_SESSION['success'])) {
echo "<div class='alert alert-success'>".$_SESSION['success']."</div>";
}
?>
<table class="table">
<thead>
<tr>
<th scope="col">Kabupaten</th>
<th style="background-color: #FF0000;" scope="col">Positif</th>
<th style="background-color: #FFA500;" scope="col">PDP</th>
<th style="background-color: #808080;"scope="col">ODP</th>
<th style="background-color: green;" scope="col">Sembuh</th>
                        <th style="background-color: #FFFF00;" scope="col">Meninggal</th>
                        
                        
</tr>
</thead>
<?php
require 'config.php';
$kelompokJogjaAntiCovid = $collection->find();
foreach ($kelompokJogjaAntiCovid as $kb) {
echo "<tr>";
echo "<th scope='row'>".$kb->Kabupaten."</th>";
echo "<td>".$kb->Positif."</td>";
echo "<td>".$kb->PDP."</td>";
echo "<td>".$kb->ODP."</td>";
echo "<td>".$kb->Sembuh."</td>";
echo "<td>".$kb->Meninggal."</td>";
echo "<td>";
echo "<a href = 'edit.php?id=".$kb->_id."'class='btn btn-primary'>EDIT</a>";
echo "<a href = 'hapus.php?id=".$kb->_id."'class='btn btn-danger'>HAPUS</a>";
echo "</td>";
echo "</tr>";
}
?>
</table>
</div>
</body>
</html>
create.php
<?php session_start();
   if(isset($_POST['submit'])){
      require 'config.php';
      $insertOneResult = $collection->insertOne([
          'Kabupaten' => $_POST['Kabupaten'],
          'Positif' => $_POST['Positif'],
          'PDP' => $_POST['PDP'],
          'ODP' => $_POST['ODP'],
  'Sembuh' => $_POST['Sembuh'],
          'Meninggal' => $_POST['Meninggal'],
      ]);
      $_SESSION['success'] = "Data Kabupaten Berhasil di tambahkan";
      header("Location: index.php");
   }
?>

<!DOCTYPE html>
<html>
   <head>
      <title>MongoDB</title>
      <link rel="stylesheet" href="./vendor/twbs/bootstrap/dist/css/bootstrap.min.css">
   </head>
   <body>
      <div class="container">
         <br>
         <CENTER><h1>Tambah Data Kabupaten</h1></CENTER>
         <form method="POST">
            <div class="form-group">
      <label for="Kabupaten"><strong>Kabupaten:</strong></label>
<select name="Kabupaten" id="Kabupaten" class="form-control">
                    <option value="Kota Yogyakarta">Kota Yogyakarta</option>
                    <option value="Sleman">Sleman</option>
                    <option value="Bantul">Bantul</option>
                    <option value="Gunungkidu">Gunungkidul</option>
                    <option value="KulonProgo">Kulon Progo</option>
                </select>
                <label for="Positif"><strong>Positif:</strong></label>
                <input type="text" class="form-control" name="Positif" required="" placeholder="1-99">
                <label for="PDP"><strong>PDP:</strong></label>
                <input type="text" class="form-control" name="PDP" placeholder="1-99">
                <label for="ODP"><strong>ODP:</strong></label>
                <input type="text" class="form-control" name="ODP" placeholder="1-99">
<label for="Sembuh"><strong>Sembuh:</strong></label>
                <input type="text" class="form-control" name="Sembuh" placeholder="1-99">
                <label for="Meninggal"><strong>Meninggal:</strong></label>
                <input type="text" class="form-control" name="Meninggal" placeholder="1-99">                
                
                <br>
                <button type="submit" name="submit" class="btn btn-success">Tambah</button>
                <a href="index.php" class="btn btn-primary">Kembali</a>
            </div>
         </form>
      </div>
   </body>
</html>

edit.php
<?php session_start();
   require 'config.php';
   if (isset($_GET['id'])) {
      $mhs = $collection->findOne(['_id' => new MongoDB\BSON\ObjectID($_GET['id'])]);
   }
   if(isset($_POST['submit'])){
      $collection->updateOne(
          ['_id' => new MongoDB\BSON\ObjectID($_GET['id'])],
          ['$set' => ['Kabupaten' => $_POST['Kabupaten'], 'Positif' => $_POST['Positif'], 
                      'PDP' => $_POST['PDP'],'ODP' => $_POST['ODP'],'Sembuh' => $_POST['Sembuh'],
      'Meninggal' => $_POST['Meninggal'],
          ]]
      );
      $_SESSION['success'] = "Data Kabupaten berhasil diubah";
      header("Location: index.php");
   }
?>

<!DOCTYPE html>
<html>
   <head>
      <title>MongoDB</title>
      <link rel="stylesheet" href="./vendor/twbs/bootstrap/dist/css/bootstrap.min.css">
   </head>
   <body>
      <div class="container">
         <br>
         <CENTER><h1>Edit Data Kabupaten</h1></CENTER>
         <form method="POST">
            <div class="form-group">
                <label for="Prodi"><strong>Prodi:</strong></label>
<select name="Kabupaten" id="Kabupaten" class="form-control">
                    <option value="Kota Yogyakarta">Kota Yogyakarta</option>
                    <option value="Sleman">Sleman</option>
                    <option value="Bantul">Bantul</option>
                    <option value="Gunungkidu">Gunungkidul</option>
                    <option value="KulonProgo">Kulon Progo</option>
                </select>
                <label for="Positif"><strong>Positif:</strong></label>
                <input type="text" class="form-control" name="Positif" required="" placeholder="Positif">
                <label for="PDP"><strong>PDP:</strong></label>
                <input type="text" class="form-control" name="PDP" placeholder="PDP">
                <label for="ODP"><strong>ODP:</strong></label>
                <input type="text" class="form-control" name="ODP" placeholder="ODP">
<label for="Sembuh"><strong>Sembuh:</strong></label>
                <input type="text" class="form-control" name="Sembuh" placeholder="Sembuh">
                <label for="Meninggal"><strong>Meninggal:</strong></label>
                <input type="text" class="form-control" name="Meninggal" placeholder="Meninggal">                
                
                <br>
                <button type="submit" name="submit" class="btn btn-success">Ubah</button>
                <a href="index.php" class="btn btn-primary">Kembali</a>
            </div>
         </form>
      </div>
   </body>
</html>
hapus.php
<?php session_start();
   require 'config.php';
   if (isset($_GET['id'])) {
      $kb = $collection->findOne(['_id' => new MongoDB\BSON\ObjectID($_GET['id'])]);
   }
   if(isset($_POST['submit'])){
         require 'config.php';
   $collection->deleteOne(['_id' => new MongoDB\BSON\ObjectID($_GET['id'])]);
   $_SESSION['success'] = "Data Kabupaten Berhasil dihapus";
   header("Location: index.php");
   }
?>

<!DOCTYPE html>
<html>
   <head>
      <title>APLIKASI INTERAKTIF</title>
      <link rel="stylesheet" href="./vendor/twbs/bootstrap/dist/css/bootstrap.min.css">
   </head>
   <body>
      <div class="container">
         <br>
         <CENTER><h1>Hapus Data Kabupaten</h1></CENTER>
         <h3> Yang bernama <?php echo "$kb->Kabupaten"; ?> dengan Kabupaten <?php echo "$kb->Kabupaten"; ?> ? </h3>
         <form method="POST">
            <div class="form-group">
               <input type="hidden" value="<?php echo "$kb->Kabupaten"; ?>" class="form-control" name="Kabupaten">
               <a href="index.php" class="btn btn-primary">Kembali</a>
               <button type="submit" name="submit" class="btn btn-danger">Hapus</button>
            </div>
         </form>
      </div>
   </body>
</html>


Komentar

Postingan populer dari blog ini

REDIS

Cassandra