1. Membuat tabel berita
CREATE TABLE `berita` (
`id_berita` int(11) NOT NULL AUTO_INCREMENT,
`kd_admin` varchar(10) NOT NULL,
`id_kategori` int(4) NOT NULL,
`st_berita` varchar(6) NOT NULL,
`tgl_berita` date NOT NULL,
`jam_berita` time NOT NULL,
`judul_berita` varchar(100) NOT NULL,
`isi_berita` text NOT NULL,
`img_berita` varchar(100) NOT NULL,
PRIMARY KEY (`id_berita`)
)
`id_berita` int(11) NOT NULL AUTO_INCREMENT,
`kd_admin` varchar(10) NOT NULL,
`id_kategori` int(4) NOT NULL,
`st_berita` varchar(6) NOT NULL,
`tgl_berita` date NOT NULL,
`jam_berita` time NOT NULL,
`judul_berita` varchar(100) NOT NULL,
`isi_berita` text NOT NULL,
`img_berita` varchar(100) NOT NULL,
PRIMARY KEY (`id_berita`)
)
2. Tambahkan function untuk gambar pada helper directory application\helpers\islogin_helper.php
//-->format_img
function UploadImg($file,$dst,$style,$size){
$date= date('YmdHis').'_'.$style;
//Penjabaran File
$filename = $file['name'];
$filetype = $file['type'];
$filetmp = $file['tmp_name'];
$fileupload = $dst.$filename;
//upload ukuran sebenarnya
move_uploaded_file($filetmp, $fileupload);
//Identifikasi Gambar
if ($filetype == 'image/jpeg' || $filetype == 'image/jpg') {
$src = imagecreatefromjpeg($fileupload);
}elseif ($filetype == 'image/png') {
$src = imagecreatefrompng($fileupload);
}
$wsrc = imageSX($src);
$hsrc = imageSY($src);
//Set Ukuran Gambar
$wdst = $size;
$hdst = ($wdst / $wsrc) * $hsrc;
//Proses Perubahan Ukuran
$filecreate = imagecreatetruecolor($wdst, $hdst);
imagecopyresampled($filecreate, $src, 0, 0, 0, 0, $wdst, $hdst, $wsrc, $hsrc);
//Nama Acak
$x = explode(".", $filename);
$name = $x[0];
$extension = $x[1];
$filename = $date.'.'.$extension;
//Reupload
if ($filetype == 'image/jpeg' || $filetype == 'image/jpg') {
imagejpeg($filecreate,$dst.$filename);
}elseif ($filetype == 'image/png') {
imagepng($filecreate,$dst.$filename);
}
//Hapus Foto Lama
unlink($fileupload);
return $filename;
}
//-->Endformat_img
function UploadImg($file,$dst,$style,$size){
$date= date('YmdHis').'_'.$style;
//Penjabaran File
$filename = $file['name'];
$filetype = $file['type'];
$filetmp = $file['tmp_name'];
$fileupload = $dst.$filename;
//upload ukuran sebenarnya
move_uploaded_file($filetmp, $fileupload);
//Identifikasi Gambar
if ($filetype == 'image/jpeg' || $filetype == 'image/jpg') {
$src = imagecreatefromjpeg($fileupload);
}elseif ($filetype == 'image/png') {
$src = imagecreatefrompng($fileupload);
}
$wsrc = imageSX($src);
$hsrc = imageSY($src);
//Set Ukuran Gambar
$wdst = $size;
$hdst = ($wdst / $wsrc) * $hsrc;
//Proses Perubahan Ukuran
$filecreate = imagecreatetruecolor($wdst, $hdst);
imagecopyresampled($filecreate, $src, 0, 0, 0, 0, $wdst, $hdst, $wsrc, $hsrc);
//Nama Acak
$x = explode(".", $filename);
$name = $x[0];
$extension = $x[1];
$filename = $date.'.'.$extension;
//Reupload
if ($filetype == 'image/jpeg' || $filetype == 'image/jpg') {
imagejpeg($filecreate,$dst.$filename);
}elseif ($filetype == 'image/png') {
imagepng($filecreate,$dst.$filename);
}
//Hapus Foto Lama
unlink($fileupload);
return $filename;
}
//-->Endformat_img
3. tambahkan folder baru pada assets dengan nama folder img_berita pada directory assets \img_berita
4. script pada control Berita.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Berita extends CI_Controller{
private $view = "backend/v_berita/";
private $redirect = "Berita";
public function __construct()
{
parent::__construct();
$this->load->model('M_berita');
$this->load->model('M_kategori');
IsAdmin();
}
function index(){
$read = $this->M_berita->GetAll();
$data = array(
'judul' => "DATA ADMIN",
'sub' => "Lihat Admin",
'read'=> $read
);
//$this->load->view($this->view.'read', $data);
$this->template->load('backend/template',$this->view.'read', $data);
}
public function create(){
$data = array(
'judul' => "DATA ADMIN",
'sub' => "Tambah Admin",
'kategori' => $this->M_kategori->GetAll(),
'create' => ''
);
//$this->load->view($this->view.'create', $data);
$this->template->load('backend/template',$this->view.'create', $data);
}
public function save(){
//img_berita
$name_imgberita = $_FILES['img_berita']['name'];
$type_imgberita = $_FILES['img_berita']['type'];
$tmp_imgberita = $_FILES['img_berita']['tmp_name'];
//upload img
if (!empty($tmp_imgberita)){
if ($type_imgberita != "image/jpeg" AND $type_imgberita != "image/jpg" AND $type_imgberita != "image/png"){
echo "<script>alert('Format yang digunakan jpeg|jpg|png');</script>";
redirect($this->redirect,'refresh');
}
else{
$img_berita = UploadImg($_FILES['img_berita'],'./assets/img_berita/','berita',500);
$data = array(
'judul_berita'=> $this->input->post('judul_berita'),
'id_kategori'=> $this->input->post('id_kategori'),
'st_berita'=> 'Blokir',
'isi_berita'=> $this->input->post('isi_berita'),
'tgl_berita'=> date('Y-m-d'),
'jam_berita'=> date('H:i:s'),
'kd_admin' => $this->session->userdata('kd_admin'),
'img_berita'=> $img_berita
);
$this->M_berita->save($data);
redirect($this->redirect,'refresh');
}
}
}
public function edit(){
$kd = $this->uri->segment(3);
$data = array(
'judul' => "DATA ADMIN",
'sub' => "Ubah Admin",
'kategori' => $this->M_kategori->GetAll(),
'edit' => $this->M_berita->edit($kd)
);
//$this->load->view($this->view.'edit', $data);
$this->template->load('backend/template',$this->view.'edit', $data);
}
public function update(){
$kd = $this->uri->segment(3);
//img_berita
$name_imgberita = $_FILES['img_berita']['name'];
$type_imgberita = $_FILES['img_berita']['type'];
$tmp_imgberita = $_FILES['img_berita']['tmp_name'];
//upload img
if (!empty($tmp_imgberita)){
if ($type_imgberita != "image/jpeg" AND $type_imgberita != "image/jpg" AND $type_imgberita != "image/png"){
echo "<script>alert('Format yang digunakan jpeg|jpg|png');</script>";
redirect($this->redirect,'refresh');
}
else{
$img_berita = UploadImg($_FILES['img_berita'],'./assets/img_berita/','berita',500);
$data = array(
'judul_berita'=> $this->input->post('judul_berita'),
'id_kategori'=> $this->input->post('id_kategori'),
'isi_berita'=> $this->input->post('isi_berita'),
'tgl_berita'=> $this->input->post('tgl_berita'),
'jam_berita'=> $this->input->post('jam_berita'),
'kd_admin' => $this->session->userdata('kd_admin'),
'img_berita' => $img_berita
);
}
}
else{
$data = array(
'judul_berita'=> $this->input->post('judul_berita'),
'id_kategori'=> $this->input->post('id_kategori'),
'isi_berita'=> $this->input->post('isi_berita'),
'tgl_berita'=> $this->input->post('tgl_berita'),
'jam_berita'=> $this->input->post('jam_berita'),
'kd_admin' => $this->session->userdata('kd_admin'),
);
}
$this->M_berita->update($kd,$data);
redirect($this->redirect,'refresh');
}
public function delete(){
$kd = $this->uri->segment(3);
$data = array(
'kd_admin' => $kd
);
$this->M_berita->delete($data);
redirect($this->redirect,'refresh');
}
}
defined('BASEPATH') OR exit('No direct script access allowed');
class Berita extends CI_Controller{
private $view = "backend/v_berita/";
private $redirect = "Berita";
public function __construct()
{
parent::__construct();
$this->load->model('M_berita');
$this->load->model('M_kategori');
IsAdmin();
}
function index(){
$read = $this->M_berita->GetAll();
$data = array(
'judul' => "DATA ADMIN",
'sub' => "Lihat Admin",
'read'=> $read
);
//$this->load->view($this->view.'read', $data);
$this->template->load('backend/template',$this->view.'read', $data);
}
public function create(){
$data = array(
'judul' => "DATA ADMIN",
'sub' => "Tambah Admin",
'kategori' => $this->M_kategori->GetAll(),
'create' => ''
);
//$this->load->view($this->view.'create', $data);
$this->template->load('backend/template',$this->view.'create', $data);
}
public function save(){
//img_berita
$name_imgberita = $_FILES['img_berita']['name'];
$type_imgberita = $_FILES['img_berita']['type'];
$tmp_imgberita = $_FILES['img_berita']['tmp_name'];
//upload img
if (!empty($tmp_imgberita)){
if ($type_imgberita != "image/jpeg" AND $type_imgberita != "image/jpg" AND $type_imgberita != "image/png"){
echo "<script>alert('Format yang digunakan jpeg|jpg|png');</script>";
redirect($this->redirect,'refresh');
}
else{
$img_berita = UploadImg($_FILES['img_berita'],'./assets/img_berita/','berita',500);
$data = array(
'judul_berita'=> $this->input->post('judul_berita'),
'id_kategori'=> $this->input->post('id_kategori'),
'st_berita'=> 'Blokir',
'isi_berita'=> $this->input->post('isi_berita'),
'tgl_berita'=> date('Y-m-d'),
'jam_berita'=> date('H:i:s'),
'kd_admin' => $this->session->userdata('kd_admin'),
'img_berita'=> $img_berita
);
$this->M_berita->save($data);
redirect($this->redirect,'refresh');
}
}
}
public function edit(){
$kd = $this->uri->segment(3);
$data = array(
'judul' => "DATA ADMIN",
'sub' => "Ubah Admin",
'kategori' => $this->M_kategori->GetAll(),
'edit' => $this->M_berita->edit($kd)
);
//$this->load->view($this->view.'edit', $data);
$this->template->load('backend/template',$this->view.'edit', $data);
}
public function update(){
$kd = $this->uri->segment(3);
//img_berita
$name_imgberita = $_FILES['img_berita']['name'];
$type_imgberita = $_FILES['img_berita']['type'];
$tmp_imgberita = $_FILES['img_berita']['tmp_name'];
//upload img
if (!empty($tmp_imgberita)){
if ($type_imgberita != "image/jpeg" AND $type_imgberita != "image/jpg" AND $type_imgberita != "image/png"){
echo "<script>alert('Format yang digunakan jpeg|jpg|png');</script>";
redirect($this->redirect,'refresh');
}
else{
$img_berita = UploadImg($_FILES['img_berita'],'./assets/img_berita/','berita',500);
$data = array(
'judul_berita'=> $this->input->post('judul_berita'),
'id_kategori'=> $this->input->post('id_kategori'),
'isi_berita'=> $this->input->post('isi_berita'),
'tgl_berita'=> $this->input->post('tgl_berita'),
'jam_berita'=> $this->input->post('jam_berita'),
'kd_admin' => $this->session->userdata('kd_admin'),
'img_berita' => $img_berita
);
}
}
else{
$data = array(
'judul_berita'=> $this->input->post('judul_berita'),
'id_kategori'=> $this->input->post('id_kategori'),
'isi_berita'=> $this->input->post('isi_berita'),
'tgl_berita'=> $this->input->post('tgl_berita'),
'jam_berita'=> $this->input->post('jam_berita'),
'kd_admin' => $this->session->userdata('kd_admin'),
);
}
$this->M_berita->update($kd,$data);
redirect($this->redirect,'refresh');
}
public function delete(){
$kd = $this->uri->segment(3);
$data = array(
'kd_admin' => $kd
);
$this->M_berita->delete($data);
redirect($this->redirect,'refresh');
}
}
5. script pada Model M_berita.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class M_berita extends CI_Model{
private $table = 'berita';
private $pk = 'id_berita';
public function GetAll(){
$this->db->order_by ('tgl_berita', 'desc');
$this->db->order_by ('jam_berita', 'desc');
//$this->db->join('admin','berita.kd_admin=admin.kd_admin');
$this->db->join('kategori','berita.id_kategori=kategori.id_kategori');
return $this->db->get($this->table);
}
public function save($data){
return $this->db->insert($this->table, $data);
}
public function edit($kd){
$this->db->where($this->pk, $kd);
$this->db->join('kategori','berita.id_kategori=kategori.id_kategori');
return $this->db->get($this->table)->row_array();
}
public function update($kd,$data){
$this->db->where($this->pk, $kd);
return $this->db->update($this->table, $data);
}
public function delete($data){
$this->db->where($data);
return $this->db->delete($this->table);
}
}
defined('BASEPATH') OR exit('No direct script access allowed');
class M_berita extends CI_Model{
private $table = 'berita';
private $pk = 'id_berita';
public function GetAll(){
$this->db->order_by ('tgl_berita', 'desc');
$this->db->order_by ('jam_berita', 'desc');
//$this->db->join('admin','berita.kd_admin=admin.kd_admin');
$this->db->join('kategori','berita.id_kategori=kategori.id_kategori');
return $this->db->get($this->table);
}
public function save($data){
return $this->db->insert($this->table, $data);
}
public function edit($kd){
$this->db->where($this->pk, $kd);
$this->db->join('kategori','berita.id_kategori=kategori.id_kategori');
return $this->db->get($this->table)->row_array();
}
public function update($kd,$data){
$this->db->where($this->pk, $kd);
return $this->db->update($this->table, $data);
}
public function delete($data){
$this->db->where($data);
return $this->db->delete($this->table);
}
}
6. script view pada control Berita yakni read.php
<a href="<?php echo site_url('Berita/create') ?>" title="Tombol untuk menambah data">
<button type="button" name="button">Tambah</button>
</a><br>
<table border="1" width="50%">
<tr>
<th>No</th>
<th>Kategori</th>
<th>Status</th>
<th>Judul</th>
<th>Tgl</th>
<th>Jam</th>
<th>Aksi</th>
</tr>
<?php
$no=1;
foreach ($read->result_array() as $row) {
?>
<tr>
<td><?php echo $no ?></td>
<td><?php echo $row['nama_kategori'] ?></td>
<td><?php echo $row['st_berita'] ?></td>
<td><?php echo $row['judul_berita'] ?></td>
<td><?php echo $row['tgl_berita'] ?></td>
<td><?php echo $row['jam_berita'] ?></td>
<td>
<a href="<?php echo site_url('Berita/edit/'.$row['id_berita'])?>" title="Tombol untuk ubah data">Ubah</a> |
<a href="<?php echo site_url('Berita/delete/'.$row['id_berita'])?>" onclick="javascript: return confirm('Yakin Mau dihapus <?php echo $row['judul_berita'];?>')" title="Tombol untuk hapus data">Hapus</a>
</td>
</tr>
<?php
$no++;
}
?>
</table>
<button type="button" name="button">Tambah</button>
</a><br>
<table border="1" width="50%">
<tr>
<th>No</th>
<th>Kategori</th>
<th>Status</th>
<th>Judul</th>
<th>Tgl</th>
<th>Jam</th>
<th>Aksi</th>
</tr>
<?php
$no=1;
foreach ($read->result_array() as $row) {
?>
<tr>
<td><?php echo $no ?></td>
<td><?php echo $row['nama_kategori'] ?></td>
<td><?php echo $row['st_berita'] ?></td>
<td><?php echo $row['judul_berita'] ?></td>
<td><?php echo $row['tgl_berita'] ?></td>
<td><?php echo $row['jam_berita'] ?></td>
<td>
<a href="<?php echo site_url('Berita/edit/'.$row['id_berita'])?>" title="Tombol untuk ubah data">Ubah</a> |
<a href="<?php echo site_url('Berita/delete/'.$row['id_berita'])?>" onclick="javascript: return confirm('Yakin Mau dihapus <?php echo $row['judul_berita'];?>')" title="Tombol untuk hapus data">Hapus</a>
</td>
</tr>
<?php
$no++;
}
?>
</table>
7. script view pada control Berita yakni create.php
<form method="post" action="<?php echo site_url('Berita/save') ?>" enctype="multipart/form-data">
<label>Judul Berita</label><br>
<input type="text" name="judul_berita" class="" placeholder="Masukan Judul Berita" value="" required oninvalid="this.setCustomValidity('Judul Berita Harus Di Isi')" oninput="setCustomValidity('')"><p></p>
<label>Kategori</label><br>
<select name="id_kategori" class="" required>
<option value="" selected>- Pilih kategori -</option>
<?php
foreach ($kategori->result_array() as $r) {
?>
<option value="<?php echo $r['id_kategori'] ?>"><?php echo $r['id_kategori']; echo' - '; echo $r['nama_kategori']; ?></option>
<?php } ?>
</select><p></p>
<label>Isi Berita</label><br>
<textarea name="isi_berita" class="" id="" placeholder="isi Berita" rows="10" cols="80" required></textarea><p></p>
<label>Foto Berita</label><br>
<input type="file" name="img_berita" required><p></p>
<button class="" type="submit">Simpan </button>
<a href="<?php echo site_url('berita') ?>"><button class="" type="button">Batal</button></a>
</form>
<label>Judul Berita</label><br>
<input type="text" name="judul_berita" class="" placeholder="Masukan Judul Berita" value="" required oninvalid="this.setCustomValidity('Judul Berita Harus Di Isi')" oninput="setCustomValidity('')"><p></p>
<label>Kategori</label><br>
<select name="id_kategori" class="" required>
<option value="" selected>- Pilih kategori -</option>
<?php
foreach ($kategori->result_array() as $r) {
?>
<option value="<?php echo $r['id_kategori'] ?>"><?php echo $r['id_kategori']; echo' - '; echo $r['nama_kategori']; ?></option>
<?php } ?>
</select><p></p>
<label>Isi Berita</label><br>
<textarea name="isi_berita" class="" id="" placeholder="isi Berita" rows="10" cols="80" required></textarea><p></p>
<label>Foto Berita</label><br>
<input type="file" name="img_berita" required><p></p>
<button class="" type="submit">Simpan </button>
<a href="<?php echo site_url('berita') ?>"><button class="" type="button">Batal</button></a>
</form>
8. script view pada control Berita yakni edit.php
<form method="post" action="<?php echo site_url('Berita/update/'.$edit['id_berita']) ?>" enctype="multipart/form-data">
<label>Judul Berita</label><br>
<input type="text" name="judul_berita" class="" placeholder="Masukan Judul Berita" value="<?php echo $edit['judul_berita'] ?>" required oninvalid="this.setCustomValidity('Judul Berita Harus Di Isi')" oninput="setCustomValidity('')"><p></p>
<label>Tanggal</label><br>
<input type="date" name="tgl_berita" class="" value="<?php echo $edit['tgl_berita'] ?>" required oninvalid="this.setCustomValidity('Judul Berita Harus Di Isi')" oninput="setCustomValidity('')"><p></p>
<label>Jam</label><br>
<input type="time" name="jam_berita" class="" value="<?php echo $edit['jam_berita'] ?>" required oninvalid="this.setCustomValidity('Judul Berita Harus Di Isi')" oninput="setCustomValidity('')"><p></p>
<label>kategori</label><br>
<select name="id_kategori" class="" required>
<option value="<?php echo $edit['id_kategori'] ?>"><?php echo $edit['nama_kategori'] ?></option>
<?php
foreach ($kategori->result_array() as $r) {
?>
<option value="<?php echo $r['id_kategori'] ?>"><?php echo $r['nama_kategori']; ?></option>
<?php } ?>
</select><p></p>
<label>Isi Berita</label><br>
<textarea name="isi_berita" class="" id="" placeholder="isi Berita" rows="20" cols="80" required><?php echo $edit['isi_berita'] ?></textarea>
<p></p>
<img src="<?php echo base_url('assets/img_berita/'.$edit['img_berita']) ?>" width="30%"><br>
<label>Ganti Foto</label><br>
<input type="file" name="img_berita"><p></p>
<button class="" type="submit">Perbaharui</button>
<a href="<?php echo site_url('Berita') ?>"><button class="" type="button">Batal</button></a>
</form>
<label>Judul Berita</label><br>
<input type="text" name="judul_berita" class="" placeholder="Masukan Judul Berita" value="<?php echo $edit['judul_berita'] ?>" required oninvalid="this.setCustomValidity('Judul Berita Harus Di Isi')" oninput="setCustomValidity('')"><p></p>
<label>Tanggal</label><br>
<input type="date" name="tgl_berita" class="" value="<?php echo $edit['tgl_berita'] ?>" required oninvalid="this.setCustomValidity('Judul Berita Harus Di Isi')" oninput="setCustomValidity('')"><p></p>
<label>Jam</label><br>
<input type="time" name="jam_berita" class="" value="<?php echo $edit['jam_berita'] ?>" required oninvalid="this.setCustomValidity('Judul Berita Harus Di Isi')" oninput="setCustomValidity('')"><p></p>
<label>kategori</label><br>
<select name="id_kategori" class="" required>
<option value="<?php echo $edit['id_kategori'] ?>"><?php echo $edit['nama_kategori'] ?></option>
<?php
foreach ($kategori->result_array() as $r) {
?>
<option value="<?php echo $r['id_kategori'] ?>"><?php echo $r['nama_kategori']; ?></option>
<?php } ?>
</select><p></p>
<label>Isi Berita</label><br>
<textarea name="isi_berita" class="" id="" placeholder="isi Berita" rows="20" cols="80" required><?php echo $edit['isi_berita'] ?></textarea>
<p></p>
<img src="<?php echo base_url('assets/img_berita/'.$edit['img_berita']) ?>" width="30%"><br>
<label>Ganti Foto</label><br>
<input type="file" name="img_berita"><p></p>
<button class="" type="submit">Perbaharui</button>
<a href="<?php echo site_url('Berita') ?>"><button class="" type="button">Batal</button></a>
</form>
Tidak ada komentar:
Posting Komentar