Postingan lainnya
Message: trying to get property 'id_produk' of non-object
Mohon bantuannya para master
Model:
// Gambar
public function gambar($id_produk)
{
$this->db->select('*');
$this->db->from('gambar');
$this->db->where('id_produk', $id_produk);
$this->db->order_by('id_gambar', 'desc');
$query = $this->db->get();
return $query->result();
}
//Tambah gambar
public function tambah_gambar($data)
{
$this->db->insert('gambar', $data);
}
Controller:
// Gambar
public function gambar($id_produk)
{
$produk = $this->produk_model->detail($id_produk);
$gambar = $this->produk_model->gambar($id_produk);
//Validasi input
$valid = $this->form_validation;
$valid->set_rules('judul_gambar','Judul/Nama Gambar','required',
array( 'required' => '%s harus diisi'));
if($valid->run()) {
$config['upload_path'] = './assets/upload/image';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '2400';//Dalam KB
$config['max-width'] = '2024';
$config['max_height'] = '2024';
$this->load->library('upload', $config);
if( ! $this->upload->do_upload('gambar')){
//Validasi End
$data = array('title' => 'TOOSWEET - Tambah Gambar Produk: '.$produk->nama_produk,
'produk' => $produk,
'gambar' => $gambar,
'error' => $this->upload->display_errors(),
'isi' => 'admin/produk/gambar'
);
$this->load->view('admin/layout/wrapper', $data, FALSE);
// Masuk database
}else{
$upload_gambar = array('upload_data' => $this->upload->data());
// Create Thumbnails
$config['image_library'] = 'gd2';
$config['source_image'] = './assets/upload/image/'.$upload_gambar['upload_data']['file_name'];
// Lokasi folder thumbnail
$config['new_image'] = './assets/upload/image/thumbs/';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 250;
$config['height'] = 250;
$config['thumb_marker'] = '';
$this->load->library('image_lib', $config);
$this->image_lib->resize();
// End Create Thumbnails
$i = $this->input;
$data = array( 'id_produk' => $id_produk,
'judul_gambar' => $i->post('judul_gambar'),
// Disimpan nama file gambar
'gambar' => $upload_gambar['upload_data']['file_name'],
);
$this->produk_model->tambah_gambar($data);
$this->session->set_flashdata('Selamat', 'Data Gambar Telah Ditambahkan Ke Database');
redirect(base_url('admin/produk/gambar/'.$id_produk),'refresh');
}}
// End masuk database
$data = array('title' => 'TOOSWEET - Tambah Gambar Produk',
'produk' => $produk,
'gambar' => $gambar,
'isi' => 'admin/produk/gambar'
);
$this->load->view('admin/layout/wrapper', $data, FALSE);
}
View:
<?php
// Error upload
if(isset($error)) {
echo '<p class="alert alert-warning">';
echo $error;
echo '</p>';
}
//Notifikasi error
echo validation_errors('<div class="alert alert-warning">','</div>');
//Form open
echo form_open_multipart(base_url('admin/produk/gambar/'.$produk->id_produk),' class="form-horizontal"');
?>
<div class="form-group">
<label class="col-sm-2 control-label">Judul Gambar</label>
<div class="col-sm-5">
<input type="text" name="judul_gambar" class="form-control" placeholder="Judul Gambar Produk" value="<?php echo set_value('judul_gambar') ?>" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Upload Gambar</label>
<div class="col-sm-5">
<input type="file" name="gambar" class="form-control" placeholder="Upload Gambar Produk" value="<?php echo set_value('gambar') ?>" required>
</div>
<div class="col-md-2">
<button class="btn btn-success btn-sm" name="submit" type="submit">
<i class="fa fa-save"></i> Upload
</button>
<button class="btn btn-warning btn-sm" name="reset" type="reset">
<i class="fa fa-times"></i> Reset
</button>
</div>
</div>
<?php echo form_close(); ?>
<?php
// Notifikasi
if($this->session->flashdata('sukses')) {
echo '<p class="alert alert-success">';
echo $this->session->flashdata('sukses');
echo '</div>';
}
?>
<table class="table table-bordered" id="example1">
<thead>
<tr>
<th>NO</th>
<th>GAMBAR</th>
<th>JUDUL</th>
<th>ACTION</th>
</tr>
</thead>
<tbody>
<?php $no=1; foreach($gambar as $gambar) { ?>
<tr>
<td><?php echo $no ?></td>
<td>
<img src="<?php echo base_url('assets/upload/image/thumbs/'.$gambar->gambar) ?>" class="img img-responsive img thumbnail" width="60">
</td>
<td><?php echo $gambar->judul_gambar ?></td>
<td>
<a href="<?php echo base_url('admin/produk/delete_gambar/'.$produk->id_produk.'/'.$gambar->id_gambar) ?>" class="btn btn-danger btn-xs" onclick="return confirm('Yakin Ingin Menghapus Gambar Ini?')"><i class="fa fa-trash-o"></i> Hapus</a>
</td>
</tr>
<?php $no++; } ?>
</tbody>
</table>
0
Belum ada Jawaban. Jadi yang pertama Jawaban
Login untuk ikut Jawaban