ci44- perpustakaan
Controllers – input.php
<?php
namespace App\Controllers;
use App\Models\M_buku;
use CodeIgniter\CodeIgniter;
use CodeIgniter\Config\Config;
class Input extends BaseController
{
protected $M_buku;
public function __construct()
{
$this->M_buku = new M_buku();
}
public function index()
{
$data = [
'title'
=> "Form Data Perpustakaan",
'validation'
=> \config\Services::Validation()
];
return view('v_form_perpus', $data);
}
public function mhs()
{
$data = ['title' => "Form
Data Mahasiswa"];
return view('v_form_mhs', $data);
}
//============ CRUD Perpus =============
public function sperpus()
{
//Kelola Gambar
$buku = $this->request->getFile('sampul_buku');
$RName = $buku->getRandomName();
$buku->move('gambar', $RName);
// dd($nmBuku);
$this->M_buku->simpan([
'kd_buku'
=> $this->request->getVar('kode_buku'),
'judul_buku'
=> $this->request->getVar('judul_buku'),
'harga_buku'
=> $this->request->getVar('harga_buku'),
'jenis_buku'
=> $this->request->getVar('jenis_buku'),
'jenis_sampul'
=> $this->request->getVar('sampul'),
'sinopsis'
=> $this->request->getVar('sinopsis_buku'),
'sampul'
=> $RName,
]);
session()->setFlashdata('sukses', '<h2><font
color=green>Data berhasil disimpan</font></h2>');
return redirect()->to('/input');
}
public function dperpus()
{
$f = new M_buku();
$data = [
'title'
=> "Data Perpustakaan",
'data'
=> $f->ambilData(),
];
return view('v_dperpus', $data);
}
public function hbuku($kd_buku)
{
$this->M_buku->hbuku($kd_buku);
session()->setFlashdata('hapus', '<h2><font
color=red>Data Berhasil Dihapus</font></h2>');
return redirect()->to('/input/dperpus');
}
public function ubuku($kd_buku)
{
$data = [
'title'
=> 'Ubah Data Buku',
'data'
=> $this->M_buku->ambilData($kd_buku)->getRow()
];
return view('v_eperpus', $data);
}
public function uaksibuku()
{
$kd_buku = $this->request->getVar('Kode_buku');
//Kelola Gambar
$buku = $this->request->getFile('Sampul_buku');
$RName = $buku->getRandomName();
$bukku->move('gambar',$RName);
// dd($nmBuku)
$this->M_buku->ubuku([
'Judul_buku'
=> $this->request->getVar('Judul_buku'),
'Harga_buku'
=> $this->request->getVar('Harga_buku'),
'Jenis_buku'
=> $this->request->getVar('Jenis_buku'),
'Jenis_sampul'
=> $this->request->getVar('Jenis_ampul'),
'Sinopsis'
=> $this->request->getVar('Sinopsis_buku'),
'Sampul'
=> $this->request->getVar('Sampul_buku'),
], $kd_buku);
session()->setFlashdata('ubah', 'Data
Berhasil Diubah');
return redirect()->to('/input/dperpus');
}
//============ CRUD Perpus =============
}
Controllers_home.php
<?php
namespace App\Controllers;
class Home extends BaseController
{
public function index()
{
return view("welcome_message");
}
public function Data()
{
return view('v_Data');
}
public function sperpus()
{
return view('v_sperpus');
}
public function form_mhs()
{
return view('v_form_mhs');
}
public function Buku()
{
return view('v_buku');
}
}
Models M_buku.php
<?php
namespace App\Models;
use CodeIgniter\Model;
class M_buku extends Model
{
protected $table = 'buku';
public function simpan($data)
{
$simpan = $this->db->table($this->table);
return $simpan->insert($data);
}
public function ambilData($kd_buku = false)
{
if ($kd_buku === false)
{
return $this->db->table($this->table)->get()->getResultArray();
} else
{
return $this->getWhere(['kd_buku' => $kd_buku]);
}
}
public function hbuku($kd_buku)
{
return $this->db->table($this->table)->delete(['kd_buku'=> $kd_buku]);
}
public function ubuku($data, $kd_buku)
{
return $this->db->table($this->table)->where(['kd_buku' => $kd_buku])->update($data);
}
}
View
V_dperpus.php
<!DOCTYPE html>
<html lang="en">
<head>
<title><?= $title; ?></title>
</head>
<body>
<h1 alignn="center">Data
Perpustakaan</h1>
<center>
<?php
if (session()->getFlashdata('hapus')) {
?>
<h2>
<font color="Yellow"><?=session()->getFlashdata('hapus'); ?></font>
</h2>
<?php } else { ?>
<h2>
<font color="Yellow"><?= session()->getFlashdata('ubah'); ?></font>
</h2>
<?php } ?>
<a href="<?= base_url('/input'); ?>"><button>Tambah
Data</button></a>
</center><br>
<table border="1" align="center">
<thead style="background-color:
black; color: white">
<tr height="60">
<th width="40">No</th>
<th width="100">Kode
Buku</th>
<th width="170">Judul
Buku</th>
<th width="100">Harga
Buku</th>
<th width="100">Jenis
Buku</th>
<th width="100">Jenis
Sampul</th>
<th width="200">Sinopis</th>
<th>Sampul</th>
<th width="150">Aksi</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
foreach ($data as $d) {
?>
<tr>
<td><?=$no++ ?></td>
<td><?=$d['kd_buku'] ?></td>
<td><?=$d['judul_buku'] ?></td>
<td><?=$d['harga_buku'] ?></td>
<td><?=$d['jenis_buku'] ?></td>
<td><?=$d['jenis_sampul'] ?></td>
<td><?=$d['sinopsis'] ?></td>
<td><img src="<?=base_url('/gambar/' .$d['sampul']) ?>" width="80px" alt="Image"></td>
<td align="center"><a href="<?= base_url('/input/ubuku/' .$d['kd_buku']); ?>"><button>Ubah</button></a> ||
<a href="<?= base_url('/input/hbuku/' .$d['kd_buku']); ?>"onclick="return confirm('Yakin
ingin menghapus data ini?')">
<button>Hapus</button>
</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>
V_eperpus.php
<!DOCTYPE html>
<html long="en">
<head>
<title><?= $title; ?></title>
</head>
<body>
<center>
<marquee>
<h2><font color="red">Form
Tambah Data Buku Perpustakaan</font></h2>
</marquee>
<h2><font color="green">Form
Ubah Data Buku</font></h2>
<p>
<?= session()->getFlashdata('suskes'); ?>
</p>
<form action="<?= base_url('/Input/uaksibuku'); ?>" method="post" enctype="multipart/form-data">
<table border="1">
<tr>
<td> <font color="blue"> Kode
Buku</font></td>
<td>:</td>
<td>
<Input type="text" name="kode_buku" value="<?= $data->kd_buku; ?>" readonly size=30 placeholder="Masukkan
Kode Buku" required>
</td>
</tr>
<tr>
<td>Judul
Buku</td>
<td>:</td>
<td>
<Input type="text" name="judul_buku" value="<?=$data->judul_buku; ?>" placeholder="Masukkan
Judul Buku" required>
</td>
</tr>
<tr>
<td>Harga
Buku</td>
<td>:</td>
<td>
<Input type="number" name="harga_buku" value="<?= $data->harga_buku; ?>" placeholder="Masukkan
Harga Buku" required>
</td>
</tr>
<tr>
<td>Jenis
Buku</td>
<td>:</td>
<td>
<select name="jenis_Buku" required>
<option value="">-pilih-</option>
<option value="Komik"<?php if ($data->jenis_buku == "Komik") {
echo "selected";
} ?>>Komik</option>
<option value="Komputer" <?php if ($data->jenis_buku == "Komputer") {
echo "selected";
} ?>>Komputer</option>
<option value="Politik" <?php if ($data->jenis_buku == "Politik") {
echo "selected";
} ?>>Politik</option>
<option value="Novel" <?php if ($data->jenis_buku == "Novel") {
echo "selected";
} ?>>Novel</option>
</select>
</td>
</tr>
<tr>
<td>Jenis
Sampul</td>
<td>:</td>
<td>
<Input type="radio" name="sampul" value="Softcover" <?php if ($data->jenis_sampul == "Softcover") {
echo "checked";
} ?>>Softcover
<Input type="radio" name="sampul" value="Hardcover" <?php if ($data->jenis_sampul == "Hardcover") {
echo "checked";
} ?>>Hardcover
</td>
</tr>
<tr>
<td>Sinopsis</td>
<td>:</td>
<td>
<textarea cols="40" name="sinopsis_buku" rows="10" placeholder="Masukkan
Sinopsis" required><?= $data->sinopsis; ?></textarea>
</td>
</tr>
<tr>
<td>Sampul</td>
<td>:</td>
<td><input type="file" name="sampul_buku" required></td>
<tr>
<tr align="center">
<td colspan="3"><button type="submit">Ubah</button> <a href="<?= base_url('/input/dperpus'); ?>"<button>Batal</button></a></td>
</tr>
</table>
</form>
</center>
</body>
</html>
V_form_perpus.php
<!DOCTYPE html>
<html long="en">
<head>
<title><?= $title; ?></title>
</head>
<body>
<center>
<marquee>
<h2><font color="red">Form
Tambah Data Buku Perpustakaan</font></h2>
</marquee>
<h2><font color="green">Form
Ubah Data Buku</font></h2>
<p>
<?= session()->getFlashdata('suskes'); ?>
</p>
<form action="<?= base_url('/Input/uaksibuku'); ?>" method="post" enctype="multipart/form-data">
<table border="1">
<tr>
<td> <font color="blue"> Kode
Buku</font></td>
<td>:</td>
<td>
<Input type="text" name="kode_buku" value="<?= $data->kd_buku; ?>" readonly size=30 placeholder="Masukkan
Kode Buku" required>
</td>
</tr>
<tr>
<td>Judul
Buku</td>
<td>:</td>
<td>
<Input type="text" name="judul_buku" value="<?=$data->judul_buku; ?>" placeholder="Masukkan
Judul Buku" required>
</td>
</tr>
<tr>
<td>Harga
Buku</td>
<td>:</td>
<td>
<Input type="number" name="harga_buku" value="<?= $data->harga_buku; ?>" placeholder="Masukkan
Harga Buku" required>
</td>
</tr>
<tr>
<td>Jenis
Buku</td>
<td>:</td>
<td>
<select name="jenis_Buku" required>
<option value="">-pilih-</option>
<option value="Komik"<?php if ($data->jenis_buku == "Komik") {
echo "selected";
} ?>>Komik</option>
<option value="Komputer" <?php if ($data->jenis_buku == "Komputer") {
echo "selected";
} ?>>Komputer</option>
<option value="Politik" <?php if ($data->jenis_buku == "Politik") {
echo "selected";
} ?>>Politik</option>
<option value="Novel" <?php if ($data->jenis_buku == "Novel") {
echo "selected";
} ?>>Novel</option>
</select>
</td>
</tr>
<tr>
<td>Jenis
Sampul</td>
<td>:</td>
<td>
<Input type="radio" name="sampul" value="Softcover" <?php if ($data->jenis_sampul == "Softcover") {
echo "checked";
} ?>>Softcover
<Input type="radio" name="sampul" value="Hardcover" <?php if ($data->jenis_sampul == "Hardcover") {
echo "checked";
} ?>>Hardcover
</td>
</tr>
<tr>
<td>Sinopsis</td>
<td>:</td>
<td>
<textarea cols="40" name="sinopsis_buku" rows="10" placeholder="Masukkan
Sinopsis" required><?= $data->sinopsis; ?></textarea>
</td>
</tr>
<tr>
<td>Sampul</td>
<td>:</td>
<td><input type="file" name="sampul_buku" required></td>
<tr>
<tr align="center">
<td colspan="3"><button type="submit">Ubah</button> <a href="<?= base_url('/input/dperpus'); ?>"<button>Batal</button></a></td>
</tr>
</table>
</form>
</center>
</body>
</html>
Komentar
Posting Komentar