Online Course

SA09 - Membuat Pengaturan Daftar Produk Di Halaman Admin

Membuat Daftar Produk Di Halaman Admin

ikuti langkah berikut ini

Pertama, Buat folder baru bernama produk di dalam folder images

Kedua, Buat tabel baru dengan nama tb_produk di database anda

Struktur dari tb_produk seperti ini

tb-produk

Ketiga, Buat file baru beri nama produk.php

Isi dari file produk.php seperti ini

<?php
//pemanggilan file metatag
include "setting_metatag.php";

//pemanggilan file navbar
include "setting_navbar.php";
?>

        <div id="page-wrapper">

            <div class="container">
                <!-- .row -->
                <!-- Page Heading  breadcumb-->
                <div class="row">
                    <div class="col-lg-12">
                        <h3>
                            Data Produk

                            <form action="cari_produk.php" method="get" class="form-inline pull-right">
                            <input type="text" class="form-control" name="qw" placeholder="Cari Produk" required>
                            <button type="submit" class="btn btn-default">Cari</button>
                            <a href="input_produk.php" class="btn btn-primary"><i class="fa fa-plus fa-fw"></i> Tambah Produk</a>
                            </form>

                        </h3>
                        <ol class="breadcrumb">
                            <li class="active">
                                <i class="fa fa-dashboard"></i> Home
                            </li>
                            <li class="active">
                                <i class="fa fa-list"></i> Data Produk
                            </li>
                        </ol>
                    </div>
                </div>
                <!-- /.row -->

                <!-- .row -->
                <div class="row">

                <!-- .col lg 12 -->
                    <div class="col-lg-12">
                            <!-- /.tabel responsive -->
                                <div class="table-responsive">
                                    <table class="table table-bordered table-hover table-striped">
                                        <thead>
                                            <tr>
                                                <th>No</th>
                                                <th>Nama Produk</th>
                                                <th>Harga</th>
                                                <th>Gambar</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                        <?php
                                        $pg = isset( $_GET['pg'] ) ? $_GET['pg'] : "";
                                        $batas = 10; /*batas tampilan setiap halaman*/
                                        if ( empty( $pg ) ) {
                                        $posisi = 0;
                                        $pg = 1;
                                        } else {
                                        $posisi = ( $pg - 1 ) * $batas;
                                        }
                                        /*Jika variabel $pg kosong maka akan menampilkan halaman pertama dengan batas baris 10*/

                                        $ambildata=mysqli_query($conect, "select*from tb_produk, tb_kategori, tb_etalase where tb_produk.id_kategori=tb_kategori.id_kategori and tb_produk.id_etalase=tb_etalase.id_etalase order by id_produk desc limit $posisi, $batas");
                                        $jumlah=mysqli_num_rows($ambildata);  /*mysql_num_rows untuk menghitung total baris di tabel databse*/
                                        if($jumlah == 0){  //jika tidak ada data
                                            ?>
                                        <tr>
                                            <td colspan="11">Tidak Terdapat Data</td>
                                        </tr>
                                        <?php
                                        }else{
                                        //jika ada data di tb_brand
                                        while($a=mysqli_fetch_array($ambildata)){ /*mysql_fetch array untuk mengambil data di setiap field di tabel databse*/
                                        ?>
                                        <tr>
                                            <td><?php echo $posisi=$posisi+1;?></td>
                                            <td><b><?php echo $a['nama_produk'];?></b>
                                            <br>Kategori : <?php echo $a['nama_kategori'];?>
                                            <br>Etalase : <?php echo $a['nama_etalase'];?>
                                            <br>Berat : <?php echo $a['berat_produk'];?> gr
                                            <br>Stok : <?php echo $a['stok_produk'];?>
                                            </td>
                                            <td>Rp. <?php echo number_format($a['harga_produk']);?>
                                            <br>Diskon : <?php echo $a['diskon_produk'];?>%
                                            <br>Terjual : <?php echo $a['terjual'];?> Unit


                                            </td>
                                            <td><img src="<?php echo $hostname;?>/assets/images/produk/<?php echo $a['gambar_produk'];?>" class="img-thumbnail" style="width:100px; height:100px;"></td>
                                            <td><a href="hapus_produk.php?id_produk=<?php echo $a['id_produk'];?>" onclick="return confirm('Yakin akan meghapus data ini')" title="Hapus data"><button class="btn btn-danger btn-sm"><i class="fa fa-trash-o fa-fw"></i> Hapus</button></a>
                                            <a href="edit_produk.php?id_produk=<?php echo $a['id_produk'];?>" title="Edit data"><button class="btn btn-primary btn-sm"><i class="fa fa-pencil-square-o fa-fw"></i> Edit</button> </a>
                                            </td>
                                        </tr>
                                        <?php
                                            }
                                        }
                                        ?>
                                        </tbody>
                                    </table>
                                </div>
                                <!-- /.tabel responsive -->

                                <div class="text-center">
                                    <?php
                                //script paging, untuk menampikan setiap halaman
                                $jml_data = mysqli_num_rows(mysqli_query($conect, "select*from tb_produk, tb_kategori, tb_etalase where tb_produk.id_kategori=tb_kategori.id_kategori and tb_produk.id_etalase=tb_etalase.id_etalase order by id_produk desc"));
                                $JmlHalaman = ceil($jml_data/$batas); //ceil digunakan untuk pembulatan keatas
                                if($jml_data != 0){
                                if ( $pg > 1 ) {
                                $link = $pg-1;
                                $prev = "<a href='?pg=$link'><button name='prev' class='btn btn-primary btn-sm'>Prev</button></a> ";
                                } else {
                                $prev = "<button name='prev' class='btn btn-default btn-sm'>Prev </button> ";
                                }
                                $nmr = '';
                                for ( $i = 1; $i<= $JmlHalaman; $i++ ){

                                if ( $i == $pg ) {
                                $nmr .= "<button name='prev' class='btn btn-primary btn-sm'>$i</button> ";
                                } else {
                                $nmr .= "<a href='?pg=$i'><button name='prev' class='btn btn-default btn-sm'>$i</button></a> ";
                                }
                                }
                                if ( $pg < $JmlHalaman ) {
                                $link = $pg + 1;
                                $next = "<a href='?pg=$link'><button name='prev' class='btn btn-primary btn-sm'>Next</button></a> ";
                                } else {
                                $next = "<button name='prev' class='btn btn-default btn-sm'>Next</button> ";
                                }
                                echo $prev.$nmr.$next;
                                ?>
                                <br><br>
                                <span class="text-muted">Menampilkan <?php echo $jumlah; ?> dari <?php echo $jml_data; ?> Record </span>
                                <?php
                                }
                                ?>
                                </div>

                    </div>
                    <!-- /.col lg 12-->

                </div>
                <!-- /.row -->


           </div>
            <!-- /.container-fluid -->

        </div>
        <!-- /#page-wrapper -->

    </div>
    <!-- /#wrapper -->

<?php
//pemanggilan file setting footer
include "setting_footer.php";

?>

Hasil dari file produk.php akan menampilkan halaman seperti ini

daftar-produk

Keempat, Buat file baru beri nama input_produk.php

Isi dari file input_produk.php seperti ini

<?php
//pemanggilan file metatag
include "setting_metatag.php";

//pemanggilan file navbar
include "setting_navbar.php";
?>

        <div id="page-wrapper">

            <div class="container">
                <!-- .row -->
                <!-- Page Heading  breadcumb-->
                <div class="row">
                    <div class="col-lg-12">
                        <h3>
                            Tambah Produk Baru
                        </h3>
                        <ol class="breadcrumb">
                            <li class="active">
                                <i class="fa fa-dashboard"></i> Home
                            </li>
                            <li class="active">
                                <i class="fa fa-list"></i> Daftar Produk
                            </li>
                        </ol>
                    </div>
                </div>
                <!-- /.row -->

                <?php
                //variabel setiap input di form
                $nama=mysqli_real_escape_string($conect, $_POST['nama']);  //variabel dari name input nama
                $kategori=mysqli_real_escape_string($conect, $_POST['kategori']);     //variabel dari name input kategori
                $deskripsi=mysqli_real_escape_string($conect, $_POST['deskripsi']);     //variabel dari name input deskripsi
                $stok=mysqli_real_escape_string($conect, $_POST['stok']);     //variabel dari name input stok
                $harga=mysqli_real_escape_string($conect, $_POST['harga']);     //variabel dari name input harga
                $diskon=mysqli_real_escape_string($conect, $_POST['diskon']);     //variabel dari name input diskon
                $berat=mysqli_real_escape_string($conect, $_POST['berat']);     //variabel dari name input berat
                $beli=mysqli_real_escape_string($conect, $_POST['beli']);     //variabel dari name input berat
                $etalase=mysqli_real_escape_string($conect, $_POST['etalase']);     //variabel dari name input berat

                $foto=$_FILES['foto']['tmp_name'];               //variabel dari temporary foto
                $nama_foto=$_FILES ['foto']['name'];             //variabel dari name input foto
                $type=$_FILES['foto']['type'];                   //variabel dari type foto
                $ukuran=$_FILES['foto']['size'];                 //variabel dari ukuran foto
                $files= strtolower(substr(strrchr($nama_foto,"."),1)); //variabel untuk extensi file

                //jika menekan tombol simpan
                if(isset($_POST['simpan'])){
                if(empty($nama)){  //jika field nama kosong
                $er_nama="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Masukan Nama Produk </div>";
                }
                elseif(empty($kategori)){ //jika field kategori kosong
                $er_kategori="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Pilih Kategori Produk</div>";
                }
                elseif(empty($etalase)){ //jika field supplier kosong
                $er_etalase="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Pilih Etalase Produk</div>";
                }
                elseif(empty($beli)){ //jika field harga kosong
                $er_beli="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Masukan Harga Beli Produk</div>";
                }
                elseif(empty($harga)){ //jika field harga kosong
                $er_harga="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Masukan Harga Jual Produk</div>";
                }
                elseif(empty($deskripsi)){ //jika field deskripsi kosong
                $er_deskripsi="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Masukan Deskripsi Produk</div>";
                }
                elseif(empty($stok)){ //jika field stok kosong
                $er_stok="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Masukan Stok Produk</div>";
                }
                elseif(empty($berat)){ //jika field berat kosong
                $er_berat="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Masukan Berat Produk</div>";
                }
                elseif(empty($foto)){  //jika foto kosong
                $er_foto="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Masukan Foto Produk</div>";
                }
                elseif($files !="jpg" && $files !="png"){ //jika foto tidak berekstensi .jpg atau .png
                $er_foto="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Format Gambar yang diizinkan hanya .jpg dan .png</div>";
                }
                elseif($ukuran > 2000000){   //jika ukuran lebih besar dari 2MB
                $er_foto="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Maksimal ukuran gambar 2MB </div>";
                }
                elseif(strlen($nama_foto) > 100){  //jika jumlah karakter nama foto lebih dari 100 karakter
                $er_foto="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Nama File Gambar Mak 100 karakter</div>";
                }
                else{ //jika semua field terpenuhi maka smpan gambar ke folder produk dan simpan data ke tb_produk
                move_uploaded_file($foto,"../assets/images/produk/$nama_foto");
                $save=mysqli_query($conect, "INSERT INTO tb_produk (id_produk,nama_produk,id_kategori,diskon_produk,berat_produk,harga_produk,stok_produk,gambar_produk,deskripsi_produk,harga_beli,id_etalase)
                values('','$nama','$kategori','$diskon','$berat','$harga','$stok','$nama_foto','$deskripsi','$beli','$etalase')") or die(mysqli_error($conect));
                if($save){ //jika simpan berhasil
                echo "<script>alert('Data Berhasil Disimpan');document.location='produk.php'</script>";
                }
                else{  //jika simpan gagal
                $error="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Error</div>";
                }

                }
                }
                ?>
                <!-- .row -->
                <div class="row">

                <!-- .col lg 12 -->
                    <div class="col-lg-12">
                            <!-- /.form menggunakan form group, pelajari cara membuat form di bootstrap-->

                            <form action="" method="post" enctype="multipart/form-data" role="form">
                            <?php echo $error;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Nama Produk</label>
                                <div class="col-lg-6 col-md-7 col-sm-7 col-xs-12">
                                <input type="text" class="form-control" placeholder="Nama Produk" name="nama" value="<?php echo $_POST['nama'];?>" maxlength="100">
                                </div>
                            </div>
                            <?php echo $er_nama;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Kategori Produk</label>
                                <div class="col-lg-5 col-md-5 col-sm-5 col-xs-12">
                                <select name="kategori" class="form-control">
                                        <option value="">Pilih Kategori Produk</option>
                                        <?php $kategori=mysqli_query($conect, "select *from tb_kategori");
                                        while($q=mysqli_fetch_array($kategori)){
                                        if($q['id_kategori']== $_POST['kategori']){
                                        echo "
                                        <option value='$q[id_kategori]' selected>$q[nama_kategori]</option>";
                                        }
                                        else{
                                        echo "
                                        <option value='$q[id_kategori]'>$q[nama_kategori]</option>";
                                        }

                                        }
                                        ?> </select>
                                        </div>
                            </div>
                            <?php echo $er_kategori;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Etalase Produk</label>
                                <div class="col-lg-5 col-md-5 col-sm-5 col-xs-12">
                                <select name="etalase" class="form-control">
                                        <option value="">Pilih Etalase Produk</option>
                                        <?php $supplier=mysqli_query($conect, "select *from tb_etalase");
                                        while($d=mysqli_fetch_array($supplier)){
                                        if($d['id_etalase']== $_POST['etalase']){
                                        echo "
                                        <option value='$d[id_etalase]' selected>$d[nama_etalase]</option>";
                                        }
                                        else{
                                        echo "
                                        <option value='$d[id_etalase]'>$d[nama_etalase]</option>";
                                        }

                                        }
                                        ?> </select>
                                        </div>
                            </div>
                            <?php echo $er_etalase;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Harga Beli Produk</label>
                                <div class="col-lg-5 col-md-5 col-sm-5 col-xs-12">
                                <div class="input-group"><span class="input-group-addon">Rp.</span>
                                <input type="text" class="form-control" placeholder="Harga Beli Produk" name="beli" value="<?php echo $_POST['beli'];?>" maxlength="100">
                                </div>
                                </div>
                            </div>
                            <?php echo $er_beli;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Harga Jual Produk</label>
                                <div class="col-lg-5 col-md-5 col-sm-5 col-xs-12">
                                <div class="input-group"><span class="input-group-addon">Rp.</span>
                                <input type="text" class="form-control" placeholder="Harga Jual Produk" name="harga" value="<?php echo $_POST['harga'];?>" maxlength="100">
                                </div>
                                </div>
                            </div>
                            <?php echo $er_harga;?>
                            <div class="form-group row">
                                 <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Deskripsi Produk</label>
                                   <div class="col-lg-8 col-md-7 col-sm-7 col-xs-12">
                                   <textarea name="deskripsi" class="form-control" rows="5" placeholder="Deskripsi Produk"><?php echo $_POST['deskripsi'];?></textarea>
                                   </div>
                            </div>
                            <?php echo $er_deskripsi;?>

                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Stok Produk</label>
                                <div class="col-lg-4 col-md-3 col-sm-3 col-xs-12">
                                <div class="input-group">
                                <input type="text" class="form-control" placeholder="Stok Produk" name="stok" value="<?php echo $_POST['stok'];?>" maxlength="100">
                                <span class="input-group-addon">item</span></div>
                                </div>
                            </div>
                            <?php echo $er_stok;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Berat Produk (gram)</label>
                                <div class="col-lg-4 col-md-3 col-sm-3 col-xs-12">
                                <div class="input-group">
                                <input type="text" class="form-control" placeholder="Berat Produk dalam gram" name="berat" value="<?php echo $_POST['berat'];?>" maxlength="100">
                                <span class="input-group-addon">gram</span></div>
                                </div>
                            </div>
                            <?php echo $er_berat;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Diskon Produk</label>
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
                                <div class="input-group">
                                <input type="number" class="form-control" placeholder="Diskon Produk" name="diskon" value="<?php echo $_POST['diskon'];?>" maxlength="3">
                                <span class="input-group-addon">%</span></div>
                                </div>
                            </div>
                            <?php echo $er_diskon;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Gambar Produk</label>
                                <div class="col-lg-8 col-md-7 col-sm-7 col-xs-12">
                                <input type="file" class="form-control-file" name="foto" accept=".jpg, .png">
                                </div>
                            </div>
                            <?php echo $er_foto;?>
                            <hr>
                            <button type="submit" name="simpan" class="btn btn-success">Simpan</button>
                            <a href="produk.php" class="btn btn-default">Kembali Ke Daftar Produk</a>

                            </form>
                                <!-- /.form -->
                    </div>
                    <!-- /.col lg 12-->

                </div>
                <!-- /.row -->


           </div>
            <!-- /.container-fluid -->

        </div>
        <!-- /#page-wrapper -->

    </div>
    <!-- /#wrapper -->

<?php
//pemanggilan file setting footer
include "setting_footer.php";

?>

Hasil dari file tersebut menampilkan halaman seperti ini

input-produk

Kelima, Buat file baru beri nama edit_produk.php

Isi dari file edit_produk.php seperti ini

<?php
//pemanggilan file metatag
include "setting_metatag.php";

//pemanggilan file navbar
include "setting_navbar.php";
?>

        <div id="page-wrapper">

            <div class="container">
                <!-- .row -->
                <!-- Page Heading  breadcumb-->
                <div class="row">
                    <div class="col-lg-12">
                        <h3>
                            Edit Produk
                        </h3>
                        <ol class="breadcrumb">
                            <li class="active">
                                <i class="fa fa-dashboard"></i> Home
                            </li>
                            <li class="active">
                                <i class="fa fa-list"></i> Daftar Produk
                            </li>
                        </ol>
                    </div>
                </div>
                <!-- /.row -->

                <?php
                ///ambil data berdasarkan id
                $tampildata=mysqli_query($conect, "select*from tb_produk where id_produk='$_GET[id_produk]'");
                $b=mysqli_fetch_array($tampildata);

                //variabel setiap input di form

                $lama=$_POST['foto_lama'];  //variabel foto lama
                $nama=mysqli_real_escape_string($conect, $_POST['nama']);  //variabel dari name input nama
                $kategori=mysqli_real_escape_string($conect, $_POST['kategori']);     //variabel dari name input kategori
                $deskripsi=mysqli_real_escape_string($conect, $_POST['deskripsi']);     //variabel dari name input deskripsi
                $stok=mysqli_real_escape_string($conect, $_POST['stok']);     //variabel dari name input stok
                $harga=mysqli_real_escape_string($conect, $_POST['harga']);     //variabel dari name input harga
                $diskon=mysqli_real_escape_string($conect, $_POST['diskon']);     //variabel dari name input diskon
                $berat=mysqli_real_escape_string($conect, $_POST['berat']);     //variabel dari name input berat
                $beli=mysqli_real_escape_string($conect, $_POST['beli']);     //variabel dari name input berat
                $etalase=mysqli_real_escape_string($conect, $_POST['etalase']);     //variabel dari name input berat

                $foto=$_FILES['foto']['tmp_name'];               //variabel dari temporary foto
                $nama_foto=$_FILES ['foto']['name'];             //variabel dari name input foto
                $type=$_FILES['foto']['type'];                   //variabel dari type foto
                $ukuran=$_FILES['foto']['size'];                 //variabel dari ukuran foto
                $files= strtolower(substr(strrchr($nama_foto,"."),1)); //variabel untuk extensi file

                //jika menekan tombol simpan
                if(isset($_POST['simpan'])){
                if(empty($nama)){  //jika field nama kosong
                $er_nama="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Masukan Nama Produk </div>";
                }
                elseif(empty($kategori)){ //jika field kategori kosong
                $er_kategori="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Pilih Kategori Produk</div>";
                }
                elseif(empty($etalase)){ //jika field supplier kosong
                $er_etalase="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Pilih Etalase Produk</div>";
                }
                elseif(empty($beli)){ //jika field harga kosong
                $er_beli="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Masukan Harga Beli Produk</div>";
                }
                elseif(empty($harga)){ //jika field harga kosong
                $er_harga="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Masukan Harga Jual Produk</div>";
                }

                elseif(empty($deskripsi)){ //jika field deskripsi kosong
                $er_deskripsi="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Masukan Deskripsi Produk</div>";
                }
                elseif(empty($stok)){ //jika field stok kosong
                $er_stok="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Masukan Stok Produk</div>";
                }
                elseif(empty($berat)){ //jika field berat kosong
                $er_berat="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Masukan Berat Produk</div>";
                }
                else{
                if(empty($foto)){  //jika foto tidak diubah maka
                $save=mysqli_query($conect, "UPDATE tb_produk set nama_produk='$nama',id_kategori='$kategori',diskon_produk='$diskon',berat_produk='$berat',harga_produk='$harga',stok_produk='$stok',deskripsi_produk='$deskripsi',harga_beli='$beli', id_etalase='$etalase' where id_produk='$_GET[id_produk]'");
                if($save){ //jika simpan berhasil
                echo "<script>alert('Data Berhasil Ditambahkan');document.location='produk.php'</script>";
                }
                else{  //jika simpan gagal
                $error="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Error</div>";
                }
                }
                else{ //jika foto ikut diubah maka
                if($files !="jpg" && $files !="png"){ //jika foto tidak berekstensi .jpg atau .png
                $er_foto="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Format Gambar yang diizinkan hanya .jpg dan .png</div>";
                }
                elseif($ukuran > 2000000){   //jika ukuran lebih besar dari 2MB
                $er_foto="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Maksimal ukuran gambar 2MB </div>";
                }
                elseif(strlen($nama_foto) > 100){  //jika jumlah karakter nama foto lebih dari 100 karakter
                $er_foto="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Nama File Gambar Mak 100 karakter</div>";
                }
                else{
                unlink('../assets/images/brand/'.$lama); //hapus foto lama di folder
                move_uploaded_file($foto,"../assets/images/produk/$nama_foto"); //upload foto baru
                $save=mysqli_query($conect, "UPDATE tb_produk set nama_produk='$nama',id_kategori='$kategori',diskon_produk='$diskon',berat_produk='$berat',harga_produk='$harga',stok_produk='$stok',gambar_produk='$nama_foto',deskripsi_produk='$deskripsi',harga_beli='$beli', id_etalase='$etalase' where id_produk='$_GET[id_produk]'");
                if($save){ //jika simpan berhasil
                echo "<script>alert('Data Berhasil Disimpan');document.location='produk.php'</script>";
                }
                else{  //jika simpan gagal
                $error="<div class='alert alert-warning alert-dismissable'>
                <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
                <i class='fa fa-info-circle'></i> Error</div>";
                }
                }
                }
                }
                }
                ?>
                <!-- .row -->
                <div class="row">

                <!-- .col lg 12 -->
                    <div class="col-lg-12">

                            <form action="" method="post" enctype="multipart/form-data" role="form">
                            <?php echo $error;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Nama Produk</label>
                                <div class="col-lg-8 col-md-7 col-sm-7 col-xs-12">
                                <input type="text" class="form-control" placeholder="Nama Produk" name="nama" value="<?php echo $b['nama_produk'];?>" maxlength="100">
                                </div>
                            </div>
                            <?php echo $er_nama;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Kategori Produk</label>
                                <div class="col-lg-5 col-md-5 col-sm-5 col-xs-12">
                                <select name="kategori" class="form-control">
                                        <option value="">Pilih Kategori Produk</option>
                                        <?php $kategori=mysqli_query($conect, "select *from tb_kategori");
                                        while($q=mysqli_fetch_array($kategori)){
                                        if($q['id_kategori']== $b['id_kategori']){
                                        echo "
                                        <option value='$q[id_kategori]' selected>$q[nama_kategori]</option>";
                                        }
                                        else{
                                        echo "
                                        <option value='$q[id_kategori]'>$q[nama_kategori]</option>";
                                        }

                                        }
                                        ?> </select>
                                        </div>
                            </div>
                            <?php echo $er_kategori;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Etalase Produk</label>
                                <div class="col-lg-5 col-md-5 col-sm-5 col-xs-12">
                                <select name="etalase" class="form-control">
                                        <option value="">Pilih Etalase Produk</option>
                                        <?php $supplier=mysqli_query($conect, "select *from tb_etalase");
                                        while($d=mysqli_fetch_array($supplier)){
                                        if($d['id_etalase']== $b['id_etalase']){
                                        echo "
                                        <option value='$d[id_etalase]' selected>$d[nama_etalase]</option>";
                                        }
                                        else{
                                        echo "
                                        <option value='$d[id_etalase]'>$d[nama_etalase]</option>";
                                        }

                                        }
                                        ?> </select>
                                        </div>
                            </div>
                            <?php echo $er_etalase;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Harga Beli Produk</label>
                                <div class="col-lg-5 col-md-5 col-sm-5 col-xs-12">
                                <div class="input-group"><span class="input-group-addon">Rp.</span>
                                <input type="text" class="form-control" placeholder="Harga Beli Produk" name="beli" value="<?php echo $b['harga_beli'];?>" maxlength="100">
                                </div>
                                </div>
                            </div>
                            <?php echo $er_beli;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Harga Jual Produk</label>
                                <div class="col-lg-5 col-md-5 col-sm-5 col-xs-12">
                                <div class="input-group"><span class="input-group-addon">Rp.</span>
                                <input type="text" class="form-control" placeholder="Harga Produk" name="harga" value="<?php echo $b['harga_produk'];?>" maxlength="100">
                                </div>
                                </div>
                            </div>
                            <?php echo $er_harga;?>
                            <div class="form-group row">
                                 <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Deskripsi Produk</label>
                                   <div class="col-lg-8 col-md-7 col-sm-7 col-xs-12">
                                   <textarea name="deskripsi" class="form-control" rows="5" placeholder="Deskripsi Produk"><?php echo $b['deskripsi_produk'];?></textarea>
                                   </div>
                            </div>
                            <?php echo $er_deskripsi;?>

                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Stok Produk</label>
                                <div class="col-lg-4 col-md-3 col-sm-3 col-xs-12">
                                <div class="input-group">
                                <input type="text" class="form-control" placeholder="Stok Produk" name="stok" value="<?php echo $b['stok_produk'];?>" maxlength="100">
                                <span class="input-group-addon">item</span></div>
                                </div>
                            </div>
                            <?php echo $er_stok;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Berat Produk (gram)</label>
                                <div class="col-lg-4 col-md-3 col-sm-3 col-xs-12">
                                <div class="input-group">
                                <input type="text" class="form-control" placeholder="Berat Produk dalam gram" name="berat" value="<?php echo $b['berat_produk'];?>" maxlength="100">
                                <span class="input-group-addon">gram</span></div>
                                </div>
                            </div>
                            <?php echo $er_berat;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Diskon Produk</label>
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
                                <div class="input-group">
                                <input type="number" class="form-control" placeholder="Diskon Produk" name="diskon" value="<?php echo $b['diskon_produk'];?>" maxlength="3">
                                <span class="input-group-addon">%</span></div>
                                </div>
                            </div>
                            <?php echo $er_diskon;?>
                            <div class="form-group row">
                                <label class="col-lg-2 col-md-3 col-sm-3 col-xs-12">Gambar Produk</label>
                                <div class="col-lg-8 col-md-7 col-sm-7 col-xs-12">
                                <input type="file" class="form-control-file" name="foto" accept=".jpg, .png">
                                <input type="hidden" name="foto_lama" value="<?php echo $b['gambar_produk'];?>">
                                </div>
                            </div>
                            <?php echo $er_foto;?>
                            <hr>
                            <button type="submit" name="simpan" class="btn btn-success">Simpan</button>
                            <a href="produk.php" class="btn btn-default">Kembali Ke Daftar Produk</a>

                            </form>
                                <!-- /.form -->
                    </div>
                    <!-- /.col lg 12-->

                </div>
                <!-- /.row -->


           </div>
            <!-- /.container-fluid -->

        </div>
        <!-- /#page-wrapper -->

    </div>
    <!-- /#wrapper -->

<?php
//pemanggilan file setting footer
include "setting_footer.php";

?>

Hasil dari file tersebut menampilkan halaman seperti ini

edit-produk

Keenam, Buat file baru beri nama hapus_produk.php

Isi dari file hapus_produk.php seperti ini

<?php
session_start();
//panggil koneksi
include "../assets/relasi/koneksi.php";

$id=$_GET['id_produk'];  //dapatkan id

if(isset($id)){ //jika ada id
//ambil data di tabel berdasarkan id
$data=mysqli_fetch_array(mysqli_query($conect, "select * from tb_produk where id_produk='$id'"));
//hapus gambar di folder
unlink("../assets/images/produk/$data[gambar_produk]");
//hapus data di tabel
$hapus=mysqli_query($conect, "DELETE from tb_produk where id_produk='$id'");
//jika hapus berhasil
if($hapus){
    echo"<script>alert('Data Berhasil di Hapus');document.location='produk.php'</script>";
}
else{ //jika gagal
    echo"<script>alert('Error Bos !!');document.location='produk.php'</script>";
}

}
?>

 

Keempat, Buat file baru beri nama cari_produk.php

 

Isi dari file cari_produk.php seperti ini

<?php
//pemanggilan file metatag
include "setting_metatag.php";

//pemanggilan file navbar
include "setting_navbar.php";
?>

        <div id="page-wrapper">

            <div class="container">
                <!-- .row -->
                <!-- Page Heading  breadcumb-->
                <div class="row">
                    <div class="col-lg-12">
                        <h3>
                            Pencarian Produk

                            <form action="cari_produk.php" method="get" class="form-inline pull-right">
                            <input type="text" class="form-control" name="qw" placeholder="Cari Produk" required>
                            <button type="submit" class="btn btn-default">Cari</button>
                            <a href="input_produk.php" class="btn btn-primary"><i class="fa fa-plus fa-fw"></i> Tambah Produk</a>
                            </form>

                        </h3>
                        <ol class="breadcrumb">
                            <li class="active">
                                <i class="fa fa-dashboard"></i> Home
                            </li>
                            <li class="active">
                                <i class="fa fa-list"></i> Data Produk
                            </li>
                        </ol>
                    </div>
                </div>
                <!-- /.row -->

                <!-- .row -->
                <div class="row">

                <!-- .col lg 12 -->
                    <div class="col-lg-12">
                            <!-- /.tabel responsive -->
                                <div class="table-responsive">
                                    <table class="table table-bordered table-hover table-striped">
                                        <thead>
                                            <tr>
                                                <th>No</th>
                                                <th>Nama Produk</th>
                                                <th>Harga</th>
                                                <th>Gambar</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                        <?php
                                        $pg = isset( $_GET['pg'] ) ? $_GET['pg'] : "";
                                        $batas = 10; /*batas tampilan setiap halaman*/
                                        if ( empty( $pg ) ) {
                                        $posisi = 0;
                                        $pg = 1;
                                        } else {
                                        $posisi = ( $pg - 1 ) * $batas;
                                        }
                                        /*Jika variabel $pg kosong maka akan menampilkan halaman pertama dengan batas baris 10*/

                                        $ambildata=mysqli_query($conect, "select*from tb_produk, tb_kategori, tb_etalase where tb_produk.id_kategori=tb_kategori.id_kategori and tb_produk.id_etalase=tb_etalase.id_etalase and nama_produk LIKE '$_GET[qw]%' order by id_produk desc limit $posisi, $batas");
                                        $jumlah=mysqli_num_rows($ambildata);  /*mysql_num_rows untuk menghitung total baris di tabel databse*/
                                        if($jumlah == 0){  //jika tidak ada data
                                            ?>
                                        <tr>
                                            <td colspan="11">Tidak Terdapat Data</td>
                                        </tr>
                                        <?php
                                        }else{
                                        //jika ada data di tb_brand
                                        while($a=mysqli_fetch_array($ambildata)){ /*mysql_fetch array untuk mengambil data di setiap field di tabel databse*/
                                        ?>
                                        <tr>
                                            <td><?php echo $posisi=$posisi+1;?></td>
                                            <td><b><?php echo $a['nama_produk'];?></b>
                                            <br>Kategori : <?php echo $a['nama_kategori'];?>
                                            <br>Etalase : <?php echo $a['nama_etalase'];?>
                                            <br>Berat : <?php echo $a['berat_produk'];?> gr
                                            <br>Stok : <?php echo $a['stok_produk'];?>
                                            </td>
                                            <td>Rp. <?php echo number_format($a['harga_produk']);?>
                                            <br>Diskon : <?php echo $a['diskon_produk'];?>%
                                            <br>Terjual : <?php echo $a['terjual'];?> Unit


                                            </td>
                                            <td><img src="<?php echo $hostname;?>/assets/images/produk/<?php echo $a['gambar_produk'];?>" class="img-thumbnail" style="width:100px; height:100px;"></td>
                                            <td><a href="hapus_produk.php?id_produk=<?php echo $a['id_produk'];?>" onclick="return confirm('Yakin akan meghapus data ini')" title="Hapus data"><button class="btn btn-danger btn-sm"><i class="fa fa-trash-o fa-fw"></i> Hapus</button></a>
                                            <a href="edit_produk.php?id_produk=<?php echo $a['id_produk'];?>" title="Edit data"><button class="btn btn-primary btn-sm"><i class="fa fa-pencil-square-o fa-fw"></i> Edit</button> </a>
                                            </td>
                                        </tr>
                                        <?php
                                            }
                                        }
                                        ?>
                                        </tbody>
                                    </table>
                                </div>
                                <!-- /.tabel responsive -->

                                <div class="text-center">
                                    <?php
                                //script paging, untuk menampikan setiap halaman
                                $jml_data = mysqli_num_rows(mysqli_query($conect, "select*from tb_produk, tb_kategori, tb_etalase where tb_produk.id_kategori=tb_kategori.id_kategori and tb_produk.id_etalase=tb_etalase.id_etalase and nama_produk LIKE '$_GET[qw]%' order by id_produk desc"));
                                $JmlHalaman = ceil($jml_data/$batas); //ceil digunakan untuk pembulatan keatas
                                if($jml_data != 0){
                                if ( $pg > 1 ) {
                                $link = $pg-1;
                                $prev = "<a href='?pg=$link'><button name='prev' class='btn btn-primary btn-sm'>Prev</button></a> ";
                                } else {
                                $prev = "<button name='prev' class='btn btn-default btn-sm'>Prev </button> ";
                                }
                                $nmr = '';
                                for ( $i = 1; $i<= $JmlHalaman; $i++ ){

                                if ( $i == $pg ) {
                                $nmr .= "<button name='prev' class='btn btn-primary btn-sm'>$i</button> ";
                                } else {
                                $nmr .= "<a href='?pg=$i'><button name='prev' class='btn btn-default btn-sm'>$i</button></a> ";
                                }
                                }
                                if ( $pg < $JmlHalaman ) {
                                $link = $pg + 1;
                                $next = "<a href='?pg=$link'><button name='prev' class='btn btn-primary btn-sm'>Next</button></a> ";
                                } else {
                                $next = "<button name='prev' class='btn btn-default btn-sm'>Next</button> ";
                                }
                                echo $prev.$nmr.$next;
                                ?>
                                <br><br>
                                <span class="text-muted">Menampilkan <?php echo $jumlah; ?> dari <?php echo $jml_data; ?> Record </span>
                                <?php
                                }
                                ?>
                                </div>

                    </div>
                    <!-- /.col lg 12-->

                </div>
                <!-- /.row -->


           </div>
            <!-- /.container-fluid -->

        </div>
        <!-- /#page-wrapper -->

    </div>
    <!-- /#wrapper -->

<?php
//pemanggilan file setting footer
include "setting_footer.php";

?>

Selesai

OK

 

Develindo Web | Tutorial Pemrograman Website