Menampilkan data dari database admin

 

Untuk menampilkan data dari database dapat dengan cara foreach.

index.php

<?php
// Menampilkan header
include "../template/headeradmin.php";

if (!isset($_SESSION['role'])) {
    header("location: login.php");
    exit;
}

require '../template/config/function.php';
$produk = query("SELECT * FROM produk");
?>

<div class="container">
        <div class="col">
            <div class="display-5 fw-bold">Daftar Produk</div>
                <div class="col-8">
                    <a href="<?= BASEURL; ?>/produk/insert.php" class="btn btn-primary my-3">Input</a>
                    <table class="table">
                        <thead class="thead-dark">
                            <tr>
                                <th scope="col">No</th>
                                <th scope="col">Nama Produk</th>
                                <th scope="col">Deskripsi Produk</th>
                                <th scope="col">Harga</th>
                                <th scope="col">Aksi</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $i = 1; ?>
                            <?php foreach($produk as $row) : ?>
                            <tr>
                                <th scope="row"><?= $i++; ?></th>
                                <td><?= $row['nama_produk']; ?></td>
                                <td><?= $row['deskripsi_produk']; ?></td>
                                <td><?= $row['harga_produk']; ?></td>
                                <td>
                                    <a href="<?= BASEURL; ?>/produk/edit.php?id=<?= $row["id"]; ?>"
                                        class="btn btn-warning">Edit</a>
                                    <a href="<?= BASEURL; ?>/produk/delete.php?id=<?= $row["id"]; ?>"
                                        onclick="return confirm('Apakah anda yakin?');" class="btn btn-danger">
                                        Hapus</a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
</div>


Insert.php

<?php
require '../template/config/function.php';
// Menampilkan header
include "../template/headeradmin.php";

if (!isset($_SESSION['role'])) {
    header("location: login.php");
    exit;
}

// cek apakah tombol submit sudah ditekan atau belum
if( isset($_POST["submit"]) ){
    // cek data berhasil di input atau tidak
    if( insert($_POST) > 0 ){
        echo "
            <script>
                    alert('Berhasil input data!');
                    document.location.href = '../produk/index.php';
            </script>
            ";
    } else {
        echo "
            <script>
                alert('Gagal input data!');
                document.location.href = '../produk/index.php';
            </script>
            ";
    }

}
?>

<div class="container">
    <div class="row my-5">
        <div class="col">
            <div class="display-5 fw-bold text-center mb-3">Tambah Produk</div>
            <div class="row justify-content-center">
                <div class="col-6">
                    <form action="" method="post">
                        <div class="mb-3">
                            <label for="nama_produk" class="form-label">Nama Produk</label>
                            <input type="text" class="form-control" id="nama_produk" name="nama_produk" required>
                        </div>
                        <div class="mb-3">
                            <label for="deskripsi_produk" class="form-label">Deskripsi Produk</label>
                            <input type="text" class="form-control" id="deskripsi_produk" name="deskripsi_produk"
                                required>
                        </div>
                        <div class="mb-3">
                            <label for="harga_produk" class="form-label">Harga Produk</label>
                            <input type="text" class="form-control" id="harga_produk" name="harga_produk" required>
                        </div>
                        <div class="d-grid gap-2">
                            <button type="submit" name="submit" class="btn btn-primary">Tambah Data</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>


edit.php

<?php
require '../template/config/function.php';
// Menampilkan header
include "../template/headeradmin.php";

if (!isset($_SESSION['role'])) {
    header("location: login.php");
    exit;
}

// ambil data di url
$id = $_GET["id"];

// query data produk berdasarkan id
$pdk = query("SELECT * FROM produk WHERE id = $id")[0];

if( isset($_POST["submit"]) ){
    // cek data berhasil di ubah atau tidak
    if( update($_POST) > 0 ){
        echo "
            <script>
                    alert('data berhasil diubah!');
                    document.location.href = '../produk/index.php';
            </script>
            ";
    } else {
        echo "
            <script>
                alert('data gagal diubah!');
                document.location.href = '../produk/index.php';
            </script>
            ";
    }
}
?>

<div class="container">
    <div class="row my-5">
        <div class="col">
            <div class="display-5 fw-bold text-center mb-3">Update Produk</div>
            <div class="row justify-content-center">
                <div class="col-6">
                    <form action="" method="post">
                        <input type="hidden" name="id" value="<?= $pdk["id"]; ?>">
                        <div class="mb-3">
                            <label for="nama_produk" class="form-label">Nama Produk</label>
                            <input type="text" class="form-control" id="nama_produk" name="nama_produk"
                                value="<?= $pdk["nama_produk"]; ?>" required>
                        </div>
                        <div class="mb-3">
                            <label for="deskripsi_produk" class="form-label">Deskripsi Produk</label>
                            <input type="text" class="form-control" id="deskripsi_produk" name="deskripsi_produk"
                                value="<?= $pdk["deskripsi_produk"]; ?>" required>
                        </div>
                        <div class="mb-3">
                            <label for="harga_produk" class="form-label">Harga Produk</label>
                            <input type="text" class="form-control" id="harga_produk" name="harga_produk"
                                value="<?= $pdk["harga_produk"]; ?>" required>
                        </div>
                        <div class="d-grid gap-2">
                            <button type="submit" name="submit" class="btn btn-primary">Update Data</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>


delete.php

<?php
require '../template/config/function.php';

$id = $_GET["id"];

if( delete($id) > 0 ){
    echo "
        <script>
                alert('Data deleted successfully!');
                document.location.href = '../produk/index.php';
        </script>
        ";
} else {
    echo "
        <script>
            alert('Data failed to delete!');
            document.location.href = '../produk/index.php';
        </script>
        ";
}
?>


Komentar