Simple MVC CI and MySql

Malam ini sepi sekali, Natal malam gak ada kerjaan, jadi mari kita nge-date dengan lapi dan internet. 😀
Iseng-iseng membuat postingan tentang Codeigniter. Oke langsung saja, jadi ceritanya saya akan membuat sebuah framework ini bekerja untuk menampilkan data yang terpusat di MySql.

1. Configurasi dulu Codeigniternya. Jadi ini adalah configurasi standarnya aja. Bisa googling kok kalok mau yang lebih detail. :p

//autoload.php
$autoload[‘libraries’] = array(‘database’);

//config.php
$config[‘base_url’] = ‘localhost/payable/’;

//routes.php
$route[‘default_controller’] = “c_payable”;

//database.php
$db[‘default’][‘hostname’] = ‘localhost’;
$db[‘default’][‘username’] = ‘root’;
$db[‘default’][‘password’] = ”;
$db[‘default’][‘database’] = ‘payable’;
$db[‘default’][‘dbdriver’] = ‘mysql’;
$db[‘default’][‘dbprefix’] = ”;
$db[‘default’][‘pconnect’] = TRUE;
$db[‘default’][‘db_debug’] = TRUE;
$db[‘default’][‘cache_on’] = FALSE;
$db[‘default’][‘cachedir’] = ”;
$db[‘default’][‘char_set’] = ‘utf8’;
$db[‘default’][‘dbcollat’] = ‘utf8_general_ci’;
$db[‘default’][‘swap_pre’] = ”;
$db[‘default’][‘autoinit’] = TRUE;
$db[‘default’][‘stricton’] = FALSE;

2. Buat sebuh database dan tabel2nya juga. Jadi saya buat database bernama “Payable” 

CREATE DATABASE `payable`
CHARACTER SET ‘latin1’
COLLATE ‘latin1_swedish_ci’;
USE `payable`;

CREATE TABLE `payable` (
`pay_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`pay_date` date DEFAULT NULL,
`pay_remarks` varchar(100) DEFAULT NULL,
`vnd_id` int(11) DEFAULT NULL,
PRIMARY KEY (`pay_id`),
UNIQUE KEY `pay_id` (`pay_id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

CREATE TABLE `payable_detail` (
`pay_det_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`pay_id` int(11) DEFAULT NULL,
`pay_det_total` date DEFAULT NULL,
`pay_det_remarks` varchar(100) DEFAULT NULL,
PRIMARY KEY (`pay_det_id`),
UNIQUE KEY `pay_det_id` (`pay_det_id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

Ini bagian insert datanya

INSERT INTO `payable` (`pay_id`, `pay_date`, `pay_remarks`, `vnd_id`) VALUES
(1,’2013-12-18′,’However you moved some of it to the VIEWto make the foreach loop. Is it mandatory for the foreach lo’,1),
(2,’2013-12-18′,’However you moved some of it to the VIEWto make the foreach loop. Is it mandatory for the foreach lo’,2),
(3,’2013-12-18′,’However you moved some of it to the VIEWto make the foreach loop. Is it mandatory for the foreach lo’,1),
(4,’2013-12-18′,’However you moved some of it to the VIEWto make the foreach loop. Is it mandatory for the foreach lo’,2);

INSERT INTO `payable_detail` (`pay_det_id`, `pay_id`, `pay_det_total`, `pay_det_remarks`) VALUES
(1,1,’2050-00-00′,’However you moved some of it to the VIEWto make the foreach loop. Is it mandatory for the foreach lo’),
(2,1,’2060-00-00′,’However you moved some of it to the VIEWto make the foreach loop. Is it mandatory for the foreach lo’),
(3,2,’1970-00-00′,’However you moved some of it to the VIEWto make the foreach loop. Is it mandatory for the foreach lo’),
(4,2,’1980-00-00′,’However you moved some of it to the VIEWto make the foreach loop. Is it mandatory for the foreach lo’);

3. Buka Dreamweaver, kalo gak ada, boleh pakek Notepad atau yang lain.

File m_payable.php adalah file yang akan mengatur agar aplikasi terhubung dengan database MySql.
Semua query disimpan pada sebuah fungsi bernama “select” dan disimpan pada variable $query.

1:  <?php  
2:  class M_payable extends CI_Model {  
3:    function select()  
4:    {  
5:      $query = $this->db->query("  
6:            select   
7:            p.*,  
8:            det.*  
9:            from payable as p  
10:           join payable_detail det on p.pay_id = det.pay_id  
11:           ");  
12:           return $query;  
13:    }  
14:  }  
15:  ?>  

File v_payable.php adalah file yang akan menampilkan output dari kombinasi semua aplikasi, disini bisa disimpan desain dan tampilan pendukung layaknya sebuah Website biasa.

1:  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
2:  <html>  
3:  <head>  
4:  <title>Untitled Document</title>  
5:  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  
6:  </head>  
7:  <body>  
8:  <?php  
9:  foreach ($query->result_array() as $row)  
10:  {  
11:       ?><li><? echo $row['pay_id']  
12:       .", ". $row['pay_date']  
13:       .", ". $row['pay_remarks'];  
14:  }  
15:  ?>  
16:  </body>  
17:  </html>  

File c_payable adalah sebuah file controller, file inilah yang mengatur semua perjalanan aplikasi. Oke coba perhatikan filenya berikut ini.

1:  <?php  
2:  class C_payable extends CI_Controller {  
3:       function index()  
4:       {  
5:       $this->load->model('M_payable');  
6:       $data['query'] = $this->M_payable->select();  
7:       $this->load->view('v_payable',$data);  
8:       }  
9:  }  
10: ?>  

Berikut ini adalah hasil outpunya. Buka browser anda. Ketik alamat http://localhost/payable/index.php/C_payable atau http://localhost/payable (link pertama adalah jika anda tidak seting routes.php diatas, potongan terakhir adalah nama controller harus diawali oleh huruf besar)

blog

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s