Postingan lainnya
Stuck di form_validation cover di function rules saat upload gambar di codeigniter.
Isi kode controller buku.php
<?php
class Buku extends CI_Controller{
public function index()
{
$data['buku'] = $this->buku_model->tampil_data()->result();
$this->load->view('templates_administrator/header');
$this->load->view('templates_administrator/sidebar');
$this->load->view('administrator/buku',$data);
$this->load->view('templates_administrator/footer');
}
public function input()
{
$data['buku'] = $this->buku_model->tampil_data('buku')->result();
$data = array(
'id' => set_value('id'),
'judul' => set_value('judul'),
'pengarang' => set_value('pengarang'),
'kategori_id' => set_value('kategori_id'),
'penerbit' => set_value('penerbit'),
'cover' => set_value('cover'),
'tahun_terbit' => set_value('tahun_terbit'),
'volum' => set_value('volum'),
'halaman' => set_value('halaman'),
'lokasi' => set_value('lokasi'),
'stok' => set_value('stok'),
);
$data['penerbit'] = $this->penerbit_model->tampil_data('penerbit')->result();
$data['kategori'] = $this->kategori_model->tampil_data('kategori')->result();
$this->load->view('templates_administrator/header');
$this->load->view('templates_administrator/sidebar');
$this->load->view('administrator/buku_form',$data);
$this->load->view('templates_administrator/footer');
}
public function input_aksi()
{
$this->_rules();
if($this->form_validation->run() == FALSE) {
$this->input();
}else{
$judul = $this->input->post('judul');
$pengarang = $this->input->post('pengarang');
$kategori_id = $this->input->post('kategori_id');
$penerbit = $this->input->post('penerbit');
$tahun_terbit = $this->input->post('tahun_terbit');
$volum = $this->input->post('volum');
$halaman = $this->input->post('halaman');
$lokasi = $this->input->post('lokasi');
$stok = $this->input->post('stok');
$cover = $this->input->post('cover');
if ($cover=''){}else{
$config['upload_path'] = './assets/uploads';
$config['allowed_types'] = 'jpg|png|giff';
$this->load->library('upload',$config);
if(!$this->upload->do_upload('cover')){
echo"Gagal Upload"; die();
}else{
$cover=$this->upload->data('file_name');
}
}
$data = array(
'judul' => $judul,
'pengarang' => $pengarang,
'kategori_id' => $kategori_id,
'penerbit' => $penerbit,
'cover' => $cover,
'tahun_terbit' => $tahun_terbit,
'volum' => $volum,
'halaman' => $halaman,
'lokasi' => $lokasi,
'stok' => $stok,
);
$this->buku_model->input_data($data,'buku');
$this->session->set_flashdata('pesan','<div class="alert alert-danger alert-dismissible fade show" role="alert">
Data Buku Berhasil Ditambahkan!
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>');
redirect('administrator/buku');
}
}
public function _rules()
{
$this->form_validation->set_rules('judul','judul','required',['required'=> 'Judul wajib diisi!']);
$this->form_validation->set_rules('pengarang','pengarang','required',['required'=> 'Pengarang wajib diisi!']);
$this->form_validation->set_rules('kategori_id','kategori_id','required',['required'=> 'Kategori wajib diisi!']);
$this->form_validation->set_rules('penerbit','penerbit','required',['required'=> 'Penerbit wajib diisi!']);
$this->form_validation->set_rules('cover','cover','required',['required'=> 'Cover wajib diupload!']);
$this->form_validation->set_rules('tahun_terbit','tahun_terbit','required',['required'=> 'Tahun Terbit wajib diisi!']);
$this->form_validation->set_rules('volum','volum','required',['required'=> 'Volum wajib diisi!']);
$this->form_validation->set_rules('halaman','halaman','required',['required'=> 'Jumlah Halaman wajib diisi!']);
$this->form_validation->set_rules('lokasi','lokasi','required',['required'=> 'Lokasi Penyimpanan wajib diisi!']);
$this->form_validation->set_rules('stok','stok','required',['required'=> 'Jumlah stok wajib diisi!']);
}
public function update($id)
{
$where = array('id'=>$id);
$data['buku'] = $this->buku_model->edit_data($where,'buku')->result();
$data['buku'] = $this->db->query("select * from buku buk, kategori kat where buk.kategori_id=kat.id_kategori and buk.id='$id'")->result();
$data['buku'] = $this->buku_model->edit_data($where,'buku')->result();
$data['penerbit'] = $this->penerbit_model->tampil_data('penerbit')->result();
$data['kategori'] = $this->kategori_model->tampil_data('kategori')->result();
$this->load->view('templates_administrator/header');
$this->load->view('templates_administrator/sidebar');
$this->load->view('administrator/buku_update',$data);
$this->load->view('templates_administrator/footer');
}
public function update_aksi()
{
$id = $this->input->post('id');
$judul = $this->input->post('judul');
$pengarang = $this->input->post('pengarang');
$kategori_id = $this->input->post('kategori_id');
$penerbit = $this->input->post('penerbit');
$cover = $this->input->post('cover');
$tahun_terbit = $this->input->post('tahun_terbit');
$volum = $this->input->post('volum');
$halaman = $this->input->post('halaman');
$lokasi = $this->input->post('lokasi');
$stok = $this->input->post('stok');
$data = array(
'judul' => $judul,
'pengarang' =>$pengarang,
'kategori_id' => $kategori_id,
'penerbit' => $penerbit,
'cover' => $cover,
'judul' => $judul,
'tahun_terbit' => $tahun_terbit,
'volum' => $volum,
'halaman' => $halaman,
'lokasi' => $lokasi,
'stok' => $stok,
);
$where = array(
'id' => $id
);
$this->buku_model->update_data($where,$data,'buku');
$this->session->set_flashdata('pesan','<div class="alert alert-success alert-dismissible fade show" role="alert">
Data Buku Berhasil Diupdate!
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>');
redirect('administrator/buku');
}
public function delete($id)
{
$where = array('id' => $id);
$this->buku_model->hapus_data($where,'buku');
$this->session->set_flashdata('pesan','<div class="alert alert-danger alert-dismissible fade show" role="alert">
Data Buku Berhasil Dihapus!
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>');
redirect('administrator/buku');
}
}
Isi kode view buku.php
<div class="container-fluid">
<div class="alert alert-success" role="alert">
<i class="fa fa-tachometer-alt"></i> Form Input Buku
</div>
<?php echo form_open_multipart('administrator/buku/input_aksi')?>
<div class="form-group">
<label>Judul</label>
<input type="text" name="judul" placeholder="Masukkan Judul" class="form-control">
<?php echo form_error('judul','<div class="text-danger small" ml-3>')?>
</div>
<div class="form-group">
<label>Pengarang</label>
<input type="text" name="pengarang" placeholder="Masukkan Nama Pengarang" class="form-control">
<?php echo form_error('pengarang','<div class="text-danger small" ml-3>')?>
</div>
<div class="form-group">
<label>Kategori</label>
<select name="kategori_id" class="form-control">
<option value="">Pilih Kategori</option>
<?php foreach($kategori as $kat) : ?>
<option value="<?php echo $kat->id_kategori ?>"><?php echo $kat->id_kategori; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label>Penerbit</label>
<select name="penerbit" class="form-control">
<option value="">Pilih Penerbit</option>
<?php foreach ($penerbit as $pen) :?>
<option value="<?php echo $pen->nama_penerbit?>"><?php echo $pen->nama_penerbit; ?></option>
<?php endforeach ?>
</select>
</div>
<div class="form-group">
<label>Tahun Terbit</label>
<input type="text" name="tahun_terbit" placeholder="Masukkan Tahun Terbit" class="form-control">
<?php echo form_error('tahun_terbit','<div class="text-danger small" ml-3>')?>
</div>
<div class="form-group">
<label>Volum</label>
<input type="text" name="volum" placeholder="Masukkan Volum Buku" class="form-control">
<?php echo form_error('volum','<div class="text-danger small" ml-3>')?>
</div>
<div class="form-group">
<label>Jumlah Halaman</label>
<input type="text" name="halaman" placeholder="Masukkan Jumlah Halaman" class="form-control">
<?php echo form_error('halaman','<div class="text-danger small" ml-3>')?>
</div>
<div class="form-group">
<label>Lokasi Penyimpanan</label>
<input type="text" name="lokasi" placeholder="Masukkan Lokasi Penyimpanan" class="form-control">
<?php echo form_error('lokasi','<div class="text-danger small" ml-3>')?>
</div>
<div class="form-group">
<label>Stok</label>
<input type="text" name="stok" placeholder="Masukkan Jumlah Stok Buku" class="form-control">
<?php echo form_error('stok','<div class="text-danger small" ml-3>')?>
</div>
<div class="form-group">
<label>Cover</label><br>
<?php echo "<input type='file' name='cover' size='20'>"; ?>
<?php echo form_error('cover','<div class="text-danger small" ml-3>')?>
</div>
<button type="submit" class="btn btn-primary">Simpan</button>
</form>
<?php form_close() ?>
</div>
Isi kode model buku_model.php
<?php
class Buku_model extends CI_Model{
public function tampil_data()
{
return $this->db->get('buku');
}
public function input_data($data)
{
$this->db->insert('buku',$data);
}
public function edit_data($where,$table)
{
return $this->db->get_where($table,$where);
}
public function update_data($where,$data,$table)
{
$this->db->where($where);
$this->db->update($table,$data);
}
public function hapus_data($where,$table)
{
$this->db->where($where);
$this->db->delete($table);
}
}
0