Membuat Fungsi Input, Update, Delete dan Pagination Class

Setelah memahami cara membuat sebuah fungsi di framework CodeIgniter. Walaupun agak ribet juga programming dengan framewok CI ini. Saya akan mencoba menjelaskannya

1. Buat program di file controller/news.php


<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News extends CI_Controller {

public function index()
 {
 $data = array('title'=>'Test Title', 'heading'=>'Test Heading', 'message'=>'Test Message');

 $this->load->view('view_header');
 $this->load->view('view_news_show', $data);
 $this->load->view('view_footer');
 }

 public function show()
 {
 $data = $this->ModelNews->getAllNews();
 $this->load->view('news/view_show_page', $data); //pagination
 }

 public function delete($id="")
 {
 $this->ModelNews->deleteByID($id);
 redirect('news/show');
 }

 public function vupdate($id="")
 {
 $this->load->library('form_validation');

 $this->form_validation->set_rules('title', 'Title', 'trim|required');
 $this->form_validation->set_rules('content', 'Content', 'trim|required');

if($this->form_validation->run() == FALSE)
 {
 $data['n'] = $this->ModelNews->getDetailNews($id);
 $this->load->view('news/view_update_page', $data);
 }
 else
 {
 $id = $this->input->post('id');
 $title = $this->input->post('title');
 $content = $this->input->post('content');

 $this->ModelNews->getEditNews($id, $title, $content);
 redirect('news/show');
 }

 }

 public function input()
 {
 $this->load->library('form_validation');

 $this->form_validation->set_rules('title', 'Title', 'trim|required');
 $this->form_validation->set_rules('content', 'Content', 'trim|required');

if($this->form_validation->run() == FALSE)
 {
 $this->load->view('news/view_input');
 }
 else
 {
 $title = $this->input->post('title');
 $content = $this->input->post('content');

 $this->ModelNews->getInputNews($title, $content);
 redirect('news/show');
 }
 }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

2. Buat program di file model/modelnews.php

</pre>
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class ModelNews extends CI_Model {

function __construct()
 {
 parent::__construct();
 }

 function getAllNews(){

 $string_query = "SELECT * FROM news order by id DESC";
 $query = $this->db->query($string_query);

 $config['base_url'] = base_url().'index.php/news/show/';
 $config['total_rows'] = $query->num_rows();
 $config['per_page'] = '3';

 $num = $config['per_page'];
 $offset = $this->uri->segment(3);
 $offset = ( ! is_numeric($offset) || $offset < 1) ? 0 : $offset;

 if(empty($offset))
 {
 $offset=0;
 }

 $this->pagination->initialize($config);

 $data['query'] = $this->db->query($string_query." limit $offset,$num");
 $data['base'] = $this->config->item('base_url');

 return $data;
 }

 function deleteByID($id){
 $q="DELETE FROM news WHERE id='$id'";
 return $this->db->query($q);
 }

 function getDetailNews($id){
 $q="SELECT * FROM news WHERE id='$id'";
 return $this->db->query($q);
 }

 function getInputNews($title, $content){
 $q="INSERT INTO news SET title='$title', content='$content'";
 return $this->db->query($q);
 }

 function getEditNews($id, $title, $content){
 $q="UPDATE news SET title='$title', content='$content' WHERE id='$id'";
 return $this->db->query($q);
 }

}

3. Buat program di file view/news/view_show_page.php . Yang berfungsi untuk menampilkan program ke layar


<html>
<head>
<title>News</title>
</head>
<body>

<p>List News | <a href="<?=base_url();?>news/input">Input</a></p>
<table width="500">
<thead>
<tr></pre>
<th>ID</th>
 <th>Title</th>
 <th>Content</th>
 <th>Created</th>
 <th>Aksi</th>
 </tr>
 </thead>
 <tbody>
 <?php
 foreach($query->result() as $row)
 {
 echo
 "<tr>
 <td>$row->id</td>
 <td>$row->title</td>
 <td>$row->content</td>
 <td>$row->create</td>
 ";
 ?>
 <td>
 <a href="<?=base_url();?>news/vupdate/<?=$row->id?>">Edit</a>
 <a href="<?=base_url();?>news/delete/<?=$row->id?>">Hapus</a>
 </td>
 <?php
 echo "
 </tr>";
 }
 ?>
 </tbody>
 </table>
 <p><?=$this->pagination->create_links();?></p>
 </body>
 </html>
<pre>

Jadi, kalau programming php dengan framework CodeIgniter harus ada kesesuaian dengan model pemrograman melalui Konsep MVC (Model, View, COntroller)
Berikut ini adalah hasil printscreen programnya




About these ads

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Logout / Ubah )

Twitter picture

You are commenting using your Twitter account. Logout / Ubah )

Facebook photo

You are commenting using your Facebook account. Logout / Ubah )

Google+ photo

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s