Posted in CodeIgniter
<h1>Tambah Berita</h1> <form id="biodata-form" action="simpan_berita" method="post" enctype="multipart/form-data"> <table id="tabel-biodata"> <tr> <td>Judul</td> <td><input type="text" name="judul_berita" class="biodata" value="" maxlength="1024"/></td> </tr> <tr> <td></td> <td><input type="hidden" name="uploader" class="biodata" value="<?php echo $this->session->userdata('username');?>" maxlength="1024"/></td> </tr> <tr> <td>Kategori</td> <td> <?php $attributes = 'class = "biodata"'; $pilih = ''; echo form_dropdown("id_kategori", $opt_kategori, $pilih, $attributes); ?> </td> </tr> <tr> <td>Status</td> <td> <select name="status" class="biodata"> <?php if ($status==1){?> <option value="1" selected="selected">Aktif</option> <option value="0" >Nonaktif</option> <?php }else{?> <option value="1" >Aktif</option> <option value="0" selected="selected">Nonaktif</option> <?php }?> </select> </td> </tr> <tr> <td></td> <td style="text-align: right;">Isi Artikel</td> </tr> <tr> <td colspan="2"><textarea name="isi_berita" class="biodata" value="" style="height: 500px; width: 600px"></textarea></td> </tr> <tr> <td></td> <td style="text-align: right;"><br /><input type="submit" value="Simpan" class="button"/></td> </tr> </table> </form>Selanjutnya dari form submit akan menuju ke bagian controller, yaitu: berita_admin dengan fungsi simpan_berita
<?php class Berita_admin extends CI_Controller{ function __Construct() { parent ::__construct(); $this->is_logged_in(); } function berita() { $total = $this->berita_model->hitung_berita(); $data['judul'] = 'SIMATIK › Pengaturan Berita'; $data['main_content'] = 'admin/berita'; $data['berita'] = $this->berita_model->get_berita(); $this->load->view('admin_template', $data); } function simpan_berita() { $judul_berita = $this->input->post('judul_berita'); $id_kategori = $this->input->post('id_kategori'); $isi_berita = $this->input->post('isi_berita'); $uploader = $this->input->post('uploader'); $status = $this->input->post('status'); $this->berita_model->simpan_berita($judul_berita, $id_kategori, $isi_berita, $uploader, $status); redirect('berita_admin/berita'); } }Di dalam fungsi simpan_berita, model simpan_berita yang ada di berita_model.php akan diakses, jika berhasil, maka controller akan melakukan redirect ke halaman akhir dan memberikan notifikasi bahwa berita berhasil disimpan.
<?php class Berita_model extends CI_Model{ function get_berita() { $array_keys_values = $this->db->query("SELECT k.nama_kategori, b.* FROM kategori k, berita b WHERE k.id_kategori = b.id_kategori ORDER BY tanggal DESC"); return $array_keys_values; } function simpan_berita($judul_berita, $id_kategori, $isi_berita, $uploader, $status) { $data = array( 'judul_berita' => $judul_berita, 'id_kategori' => $id_kategori, 'isi_berita' => $isi_berita, 'uploader' => $uploader, 'status' => $status ); $this->db->insert('berita', $data); } }Semoga bermanfaat.