Anda di halaman 1dari 77

MVC with Codeigniter PHP Framework

MVC

( Model – View – Controller )

SoftwareSoftwareSoftwareSoftware DesignDesignDesignDesign PatternPatternPatternPattern

Masalah dalam develop software:

Algoritma mana yang akan digunakan?

Design mana yang cocok untuk digunakan?

Teknik apa yang harus diterapkan?

Modul apa saja yang dapat ditambahkan?

Tiap masalah dalam develop software adalah unik, karena itu solusinya harus lebih unik.

Bagaimana menerapkan solusi yang tepat pada masalah yang sedang dihadapi?

Design Pattern merupakan solusi umum dalam menghadapi masalah yang sering timbul pada pengembangan aplikasi.

MVCMVCMVCMVC ––ModelModelModelModel ViewViewViewView ControllerControllerControllerController

Metode pendekatan pembuatan aplikasi dengan memisahkan aplikasi menjadi 3 segmen:

Model

View

Controller

Struktur aplikasi lebih diutamakan dalam penggunaan kembali code program (reuse).

MVCMVC MVCMVC –– ModelModel ModelModel ViewView ViewView ControllerController ControllerController (cont.)(cont.) (cont.)(cont.)

ControllerController Controller Controller (cont.)(cont.) (cont.) (cont.) rizky tahara shita - http://tahara.web.id 5
ControllerController Controller Controller (cont.)(cont.) (cont.) (cont.) rizky tahara shita - http://tahara.web.id 5

MVCMVCMVCMVC ––ModelModelModelModel

Merupakan representasi tipe data yang akan digunakan oleh aplikasi.

contoh:

model database

model RSS Feeds

API Calls

Pertimbangan aspek:

Desain database.

Query & Store Procedure.

Object Class.

Data Access Layer.

Mediasi antara Controller dan View.

MVCMVCMVCMVC ––ViewViewViewView

Informasi yang akan ditampilkan kepada pengguna melalui browser.

Dapat berupa file HTML, generated PHP to HTML (template engine).

Pertimbangan aspek:

Reporting.

Forms.

Validation.

AJAX.

Graphic & Styling.

UI testing.

Mediasi antara Controller dan Model.

MVCMVCMVCMVC ––ControllerControllerControllerController

Business logic dari aplikasi yang menjadi jembatan antara Model dan View.

Me-respon terhadap HTTP Request dan menghasilkan halaman web yang diminta oleh pengguna.

Pertimbangan aspek:

Validasi data.

Menggunakan database layer.

Algoritma.

Mediasi antara View dan Model.

HowHowHowHow MVCMVCMVCMVC cancancancan HELPHELPHELPHELP ????

HowHow How How MVCMVC MVC MVC cancan can can HELPHELP HELP HELP ?? ? ? rizky
HowHow How How MVCMVC MVC MVC cancan can can HELPHELP HELP HELP ?? ? ? rizky
HowHow How How MVCMVC MVC MVC cancan can can HELPHELP HELP HELP ?? ? ? rizky
HowHow How How MVCMVC MVC MVC cancan can can HELPHELP HELP HELP ?? ? ? rizky

Framework

FrameworkFrameworkFrameworkFramework ––memudahkanmemudahkanmemudahkanmemudahkan kompleksitaskompleksitaskompleksitaskompleksitas

Menyediakan fungsi – fungsi umum.

Digunakan untuk aplikasi yang lebih spesifik.

Alur dikontrol oleh framework.

Fokus pada pengembangan aplikasi.

MengapaMengapaMengapaMengapa menggunakanmenggunakanmenggunakanmenggunakan Framework?Framework?Framework?Framework?

Aplikasi berbasis web semakin kompleks tambah sulit untuk maintenance.

tambah menu A, menu B, transaksi C, dll

Membantu dalam menerapkan konsep MVC.

pemisahan antara model, view dan controller terlihat jelas.

Menyediakan mediasi antar muka 3 layer MVC.

tidak perlu kuatir dalam melakukan integrasi antara presentation ke business layer dan ke data access layer.

HarapanHarapanHarapanHarapan daridaridaridari adanyaadanyaadanyaadanya FrameworkFrameworkFrameworkFramework

Mudah digunakan.

Fasilitasi performa aplikasi.

Menjamin keamanan.

Memisahkan HTML dan PHP.

Mendukung AJAX.

Tidak ada batasan penggunaan istilah.

Mendukung API.

Meningkatkan kualitas code.

Melatih best practices.

Kemampuan untuk dikonfigurasi

Mendukung internationalisasi (multi-language).

Ketersediaan dokumentasi.

Komunitas aktif.

Dukungan terhadap produk yang komersial.

Lisensi yang ramah.

Ketersediaan dengan web hosting.

PHPPHPPHPPHP FrameworkFrameworkFrameworkFramework

CakePHP

Codeigniter

Zend Framework

DOOPHP

40+ PHP Framework Available!

FrameworkFrameworkFrameworkFramework ––KISSKISSKISSKISS

KISS (Keep it Short and Simple)

Mencapai KISS:

Hindari istilah “Not Invented Here”.

gunakan kembali resource yang sudah ada.

Inovasi.

inovasi tidak harus selalu hal yang baru; improvisasi dari yang sudah ada.

Terbuka terhadap perubahan.

Simplicity is a mindset.

coding disiplin, penggunaan tools, penggunaan library, penggunaan framework harus simple.

Codeigniter 2

( PHP Framework )

CodeigniterCodeigniterCodeigniterCodeigniter ––PHPPHPPHPPHP FrameworkFrameworkFrameworkFramework

Membantu dalam pembuatan web based PHP application.

Selesai lebih cepat.

Menyediakan library yang sering dibutuhkan.

Struktur logika lebih simple.

Dapat lebih fokus pada pengerjaan aplikasi.

Minimalisir penggunaan code pada hanya yang dibutuhkan saja.

CodeigniterCodeigniterCodeigniterCodeigniter FeatureFeatureFeatureFeature

Free (lisensi Apache/BSD).

Ringan.

Cepat.

Menggunakan konsep MVC.

Human Readable URL.

Dukungan library dalam pembuatan aplikasi.

Mudah dalam pembuatan library, class.

Tidak membutuhkan Template Engine.

Dokumentasi lengkap.

Adanya Komunitas.

CodeigniterCodeigniterCodeigniterCodeigniter ApplicationApplicationApplicationApplication FlowFlowFlowFlow ChartChartChartChart

Application Application FlowFlow Flow Flow ChartChart Chart Chart rizky tahara shita - http://tahara.web.id 19
Application Application FlowFlow Flow Flow ChartChart Chart Chart rizky tahara shita - http://tahara.web.id 19

CICICICI ControllerControllerControllerController

Huruf pertama nama class harus huruf besar.

Extends -kan CI_Controller.

Simpan pada:

application/controllers

Akses URL:

http://localhost/index.php/nama_controller

<?php

class Blog extends CI_Controller

{

}

?>

CICICICI ControllerControllerControllerController ––FunctionsFunctionsFunctionsFunctions

Segmen: setelah nama controller

http://localhost/index.php/nama_controller/nama_fungsi

Default loaded function index()

http://localhost/index.php/nama_controller/index

<?php

class Blog extends CI_Controller

{

 

public function index()

{

echo 'Hello World!';

}

public function comments()

{

echo 'Look at this!';

}

}

?>

CICICICI ControllerControllerControllerController ––FunctionsFunctionsFunctionsFunctions ParameterParameterParameterParameter

Function dengan parameter diakses melalui segment selanjutnya setelah nama fungsi:

http://localhost/index.php/products/shoes/sandals/123

<?php class Products extends CI_Controller {

public function shoes($types, $id)

{

echo $types; echo $id;

}

?>

}

CICICICI ControllerControllerControllerController ––PrivatePrivatePrivatePrivate FunctionsFunctionsFunctionsFunctions

Function tidak dapat diakses melalui URL.

Gunakan underscore ( _ ) pada nama functions.

<?php class Blog extends CI_Controller

{

 

public function comments()

{

$this->_utility();

}

private function _utility()

{

echo 'This is utility';

}

}

?>

CICICICI ControllerControllerControllerController ––ConstructorsConstructorsConstructorsConstructors

Menjalankan default algoritma.

Tidak mengembalikan value.

<?php class Blog extends CI_Controller {

public function

{

construct()

parent:: construct(); // Your own constructor code

}

}

?>

CICICICI ControllerControllerControllerController ––FullFullFullFull SkeletonSkeletonSkeletonSkeleton

<?php

class Blog extends CI_Controller

{

 

public function

construct()

{

parent:: construct(); // Your own constructor code

}

public function index()

{

echo 'Hello World!';

}

public function comments()

{

$this->_utility();

}

private function _utility()

{

echo 'This is utility';

}

}

?>

CICICICI ViewsViewsViewsViews

Desain website (HTML, CSS, jQuery, etc)

Regular .php files

Simpan pada:

application/views

<html> <head> <title>My Blog</title> </head> <body> <h1>Welcome to my Blog!</h1> </body> </html>

CICICICI ViewsViewsViewsViews ––LoadingLoadingLoadingLoading ViewViewViewView

Controller melakukan load View

$this->load->view('view_file_name');

<?php class Blog extends CI_Controller {

function index()

{

$this->load->view('blogview');

}

}

?>

CICICICI ViewsViewsViewsViews ––DynamicDynamicDynamicDynamic DataDataDataData

Controller melakukan load View dengan parameter data yang akan ditampilkan pada view

$this->load->view('view_file_name', $variable);

<?php class Blog extends CI_Controller {

function index()

{

 

$data['title'] = "My Real Title"; $data['heading'] = "My Real Heading";

$this->load->view('blogview', $data);

}

}

?>

<html>

<head>

<title><?php echo $title;?></title>

</head>

<body>

<h1><?php echo $heading;?></h1>

</body>

</html>

CICICICI ModelModelModelModel

Huruf pertama nama class harus huruf besar.

Nama file harus huruf kecil.

Extends -kan CI_Model.

Simpan pada:

application/models

class Model_name extends CI_Model {

function

{

construct()

parent:: construct();

}

}

CICICICI ModelModelModelModel ––LoadingLoadingLoadingLoading ModelModelModelModel

Controller memanggil Model

$this->load->model('model_file_name');

<?php class Blog_controller extends CI_Controller {

function blog()

{

 

$this->load->model('Blog');

$data['query'] = $this->Blog->get_last_ten_entries();

$this->load->view('blog', $data);

}

}

?>

CICICICI FormFormFormForm HelperHelperHelperHelper

Load helper:

$this->load->helper('form');

Form helper functions:

form_open()

form_open_multipart()

form_hidden()

form_input()

form_password()

form_upload()

form_textarea()

form_dropdown()

form_multiselect()

form_checkbox()

form_radio()

form_submit()

form_reset()

form_button()

form_close()

set_value()

set_select()

set_checkbox()

set_radio()

CICICICI FormFormFormForm HelperHelperHelperHelper ––form_open()form_open()form_open()form_open()

echo form_open('email/send');

<form method="post" accept-charset="utf-8" action="http://example.com/index.php/email/send" />

Attribute

$attributes = array('class' => 'email', 'id' => 'myform'); echo form_open('email/send', $attributes );

<form method="post" accept-charset="utf-8" action="http:/example.com/index.php/email/send" class="email" id="myform" />

CICICICI InputInputInputInput ClassClassClassClass

Tujuan:

Melakukan pre-processes global input data untuk meningkatkan keamanan.

Menyediakan fungsi untuk menangani input data.

Security Filtering:

Menghilangkan predefined variable GET ($_GET).

Menghilangkan global variables.

Filter predefined variable POST/COOKIE.

Mendukung XSS (Cross-site Scripting Hacks).

Standarisasi newline menjadi \n

CICICICI InputInputInputInput ClassClassClassClass ––functionsfunctionsfunctionsfunctions

$this->input->post();

$this->input->cookie();

$this->input->server();

if ( ! isset($_POST['something']))

{

$something = FALSE;

}

else

{

$something = $_POST['something'];

}

else { $something = $_POST [' something ']; } $something = $this->input-> post (' something

$something = $this->input->post('something');

CICICICI ValidationValidationValidationValidation

Load library

$this->load->library('form_validation');

Setup Rules

Check the Validation

$this->form_validation->run();

Show Errors

Set error delimiters

$this->form_validation->set_error_delimiters('<div class="error">', '</div>');

show individual error

echo form_error('field name', '<div class="error">', '</div>');

CICICICI ValidationValidationValidationValidation ––SetupSetupSetupSetup RulesRulesRulesRules

<input type='text' name='txt_judul' size='60' maxlength='200' />

$rules = array

(

 

array

(

'field' => 'txt_judul'

, 'label' => 'Judul'

, 'rules' => 'trim|required'

)

);

$this->form_validation ->set_rules($rules);

CICI CICI ValidationValidation ValidationValidation –– SetupSetup SetupSetup RulesRules RulesRules (cont.)(cont.) (cont.)(cont.)

Rules Reference

Rules Rules

Description Description

required required

Returns empty. Returns FALSE FALSE if if the the form form element element is is empty.

matches matches

Returns does Returns not FALSE match FALSE if if the the the one form form in element the element parameter. does not match the one in the parameter.

parameter. does not match the one in the parameter. max_length max_length Returns Returns FALSE FALSE if

max_length max_length

Returns Returns FALSE FALSE if if the the form form element element is is

longer longer then then the the parameter parameter value. value.

then then the the parameter parameter value. value. greater_than greater_than Returns Returns FALSE FALSE

greater_than greater_than

Returns Returns FALSE FALSE if if the the form form element element is is less numeric. less than than the the parameter parameter value value or or not not numeric.

parameter value value or or not not numeric. alpha alpha Returns Returns FALSE FALSE if if

alpha alpha

Returns Returns FALSE FALSE if if the the form form element element

contains contains anything anything other other than than

alphabetical alphabetical characters. characters.

Rules Rules

Description Description

alpha_numeric alpha_numeric

Returns Returns FALSE FALSE if if the the form form element element contains numeric contains characters. anything anything other other than than alpha- alpha- numeric characters.

alpha_dash alpha_dash

Returns Returns FALSE FALSE if if the the form form element element

contains contains anything anything other other than than alpha- alpha-

numeric dashes. numeric characters, characters, underscores underscores or or dashes.

integer integer

Returns Returns FALSE FALSE if if the the form form element element

contains integer. contains anything anything other other than than an an integer.

is_natural is_natural

Returns Returns FALSE FALSE if if the the form form element element contains number: contains 0, anything anything 1, 2, 3, etc. other other than than a a natural natural number: 0, 1, 2, 3, etc.

CICI CICI ValidationValidation ValidationValidation –– SetupSetup SetupSetup RulesRules RulesRules (cont.)(cont.) (cont.)(cont.)

Rules Reference

Rules Rules

Description Description

 

valid_email valid_email

Returns contain Returns a FALSE FALSE valid email if if the the address. form form element element does does not not contain a valid email address.

valid_emails valid_emails

Returns Returns FALSE FALSE if if any any value value provided provided in in a a

comma comma separated separated list list is is not not a a valid valid email. email.

valid_base64 valid_base64

Returns Returns FALSE FALSE if if the the supplied supplied string string contains contains

anything anything other other than than valid valid Base64 Base64 characters. characters.

CICICICI DatabaseDatabaseDatabaseDatabase

Load Database Class

$this->load->database();

Running Query

$this->db->query('your_sql_query_here');

Query Result

as object

$qry->result();

as array

$qry->result_array();

CICICICI DatabaseDatabaseDatabaseDatabase ––StandardStandardStandardStandard ExampleExampleExampleExample

$this->load->database();

$query = $this->db->query('SELECT name, title, email FROM my_table');

foreach ($query->result() as $row)

{

echo $row->title; echo $row->name; echo $row->email;

}

$this->load->database();

$query = $this->db->query('SELECT name, title, email FROM my_table');

foreach ($query->result_array() as $row)

{

echo $row['title']; echo $row['name']; echo $row['email'];

}

CICICICI DatabaseDatabaseDatabaseDatabase ––ActiveActiveActiveActive RecordsRecordsRecordsRecords

Generic functions untuk mengelola koneksi ke database.

Functions:

Selecting Data

Inserting Data

Updating Data

Deleting Data

CICICICI DatabaseDatabaseDatabaseDatabase ––ActiveActiveActiveActive RecordsRecordsRecordsRecords ---- SelectSelectSelectSelect

$this->db->get();

$this->db->get('table_name');

$this->db->select();

$this->db->select('title, content, date');

$this->db->from();

$this->db->select('title, content, date');

$this->db->from('table_name');

$this->db->where();

Simple

$this->db->where('name', 'tahara');

Array

$where = array('name' => 'tahara', 'website' => 'http://tahara.web.id');

$this->db->where( $where );

CICICICI DatabaseDatabaseDatabaseDatabase ––ActiveActiveActiveActive RecordsRecordsRecordsRecords ---- InsertInsertInsertInsert

$data = array

(

 

'title' => 'My title' ,

'name' => 'My Name' ,

'date' => 'My date'

);

$this->db->insert('mytable', $data);

// INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')

CICICICI DatabaseDatabaseDatabaseDatabase ––ActiveActiveActiveActive RecordsRecordsRecordsRecords ---- UpdateUpdateUpdateUpdate

$data = array

(

'title' => $title, 'name' => $name, 'date' => $date

);

$this->db->where('id', $id); $this->db->update('mytable', $data);

// UPDATE mytable // SET title = '{$title}', name = '{$name}', date = '{$date}' // WHERE id = $id

CICICICI DatabaseDatabaseDatabaseDatabase ––ActiveActiveActiveActive RecordsRecordsRecordsRecords ---- DeleteDeleteDeleteDelete

$this->db->delete('mytable', array('id' => $id));

// DELETE FROM mytable WHERE id = $id

$this->db->where('id', $id); $this->db->delete('mytable');

// DELETE FROM mytable WHERE id = $id

$tables = array('table1', 'table2', 'table3');

$this->db->where('id', '5'); $this->db->delete($tables);

Workshop

( Simple CI Application )

PlanPlanPlanPlan

Analisa Database

Pembuatan Database dan Table

Instalasi Codeigniter 2

Start to Code!

AnalisaAnalisaAnalisaAnalisa DatabaseDatabaseDatabaseDatabase

AnalisaAnalisa Analisa Analisa DatabaseDatabase Database Database rizky tahara shita - http://tahara.web.id 48
AnalisaAnalisa Analisa Analisa DatabaseDatabase Database Database rizky tahara shita - http://tahara.web.id 48

PembuatanPembuatanPembuatanPembuatan DatabaseDatabaseDatabaseDatabase &&&& TableTableTableTable

Database: workshop_ci

CREATE SCHEMA IF NOT EXISTS `workshop_ci` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;

USE `workshop_ci` ;

PembuatanPembuatan PembuatanPembuatan DatabaseDatabase DatabaseDatabase && && TableTable TableTable (cont.)(cont.) (cont.)(cont.)

Table: Category

DROP TABLE IF EXISTS `workshop_ci`.`category` ;

CREATE TABLE IF NOT EXISTS `workshop_ci`.`category`

(

`id` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT , `name` VARCHAR(20) NULL , PRIMARY KEY (`id`)

)

ENGINE = InnoDB DEFAULT CHARACTER SET = utf8 COLLATE = utf8_general_ci;

PembuatanPembuatan PembuatanPembuatan DatabaseDatabase DatabaseDatabase && && TableTable TableTable (cont.)(cont.) (cont.)(cont.)

Table: Books

DROP TABLE IF EXISTS `workshop_ci`.`books` ;

CREATE TABLE IF NOT EXISTS `workshop_ci`.`books` ( `id` INT(7) UNSIGNED NOT NULL AUTO_INCREMENT , `category_id` INT(5) UNSIGNED NOT NULL , `title` VARCHAR(200) NULL , `isbn` VARCHAR(30) NULL , PRIMARY KEY (`id`) , INDEX `fk_books_category` (`category_id` ASC) , CONSTRAINT `fk_books_category` FOREIGN KEY (`category_id` ) REFERENCES `workshop_ci`.`category` (`id` ) ON DELETE NO ACTION ON UPDATE CASCADE) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8 COLLATE = utf8_general_ci;

InstalasiInstalasiInstalasiInstalasi CodeigniterCodeigniterCodeigniterCodeigniter 2222

Langkah – langkah:

Download Codeigniter.

Extract pada folder “document_root” server.

Test Codeigniter welcome page.

InstalasiInstalasi InstalasiInstalasi CodeigniterCodeigniter CodeigniterCodeigniter 22 22 (cont.)(cont.) (cont.)(cont.)

Download Codeigniter

(cont.) ● Download Codeigniter ● http://codeigniter.com/downloads/ rizky tahara shita - http://tahara.web.id 53

InstalasiInstalasi InstalasiInstalasi CodeigniterCodeigniter CodeigniterCodeigniter 22 22 (cont.)(cont.) (cont.)(cont.)

Extract pada folder “document_root” server

(cont.) (cont.) ● Extract pada folder “document_root” server rizky tahara shita - http://tahara.web.id 54
(cont.) (cont.) ● Extract pada folder “document_root” server rizky tahara shita - http://tahara.web.id 54

rizky tahara shita - http://tahara.web.id

54

InstalasiInstalasi InstalasiInstalasi CodeigniterCodeigniter CodeigniterCodeigniter 22 22 (cont.)(cont.) (cont.)(cont.)

Test Codeigniter welcome page

(cont.) (cont.) ● Test Codeigniter welcome page ● http://localhost rizky tahara shita - http://tahara.web.id 55

StartStart StartStart toto toto Code!Code! Code!Code! –– filefile:: filefile:: /application//application/configconfig/database.php/database.php /application//application/configconfig/database.php/database.php

.

.

.

$db['default']['hostname'] = 'host_server'; $db['default']['username'] = 'mysql_user'; $db['default']['password'] = 'mysql_password';

$db['default']['database'] = 'mysql_database_name'; $db['default']['dbdriver'] = 'mysql';

.

.

.

StartStart StartStart toto toto Code!Code! Code!Code! –– filefile:: filefile:: /application//application/configconfig/autoload.php/autoload.php /application//application/configconfig/autoload.php/autoload.php

.

.

.

$autoload['helper'] = array('url', 'form');

.

.

.

StartStart StartStart toto toto Code!Code! Code!Code! –– filefile:: filefile:: /application//application/controllerscontrollers/kategori.php/kategori.php /application//application/controllerscontrollers/kategori.php/kategori.php

<?php

class Kategori extends CI_Controller {

public function

construct()

{

 

parent:: construct();

$this->load->database();

}

public function index()

{

 

$qry = $this->db->query('select * from category'); $view['data'] = $qry->result_array();

$this->load->view('kategori/list', $view);

}

public function tambah()

{

$this->load->view('kategori/form');

}

public function edit($prm_key = '')

{

 

if(trim($prm_key) != '')

{

$qry = $this->db->get_where

 

(

 

'category'

,

array('id' => $prm_key)

 

);

 

$view['data'] = $qry->result_array(); $this->load->view('kategori/form', $view);

} else { redirect(site_url().'/kategori');

}

}

public function hapus($prm_key = '') { if(trim($prm_key) != '') { $qry = $this->db->delete

(

'category'

, array

(

'id' => $prm_key

}

);

)

redirect(site_url().'/kategori');

}

StartStart StartStart toto toto Code!Code! Code!Code! –– filefile:: filefile:: /application//application/controllerscontrollers/kategori.php/kategori.php /application//application/controllerscontrollers/kategori.php/kategori.php (cont.)(cont.) (cont.)(cont.)

public function simpan()

{

$this->load->library('form_validation');

$rules = array

(

 

array

(

'field' => 'txt_id'

, 'label' => 'ID'

, 'rules' => 'trim'

)

, array

(

'field' => 'txt_name'

, 'label' => 'Kategori'

, 'rules' => 'trim|required'

)

);

$this->form_validation->set_rules($rules); $this->form_validation->set_error_delimiters('<code>', '</code>');

if($this->form_validation->run() == FALSE)

{

$this->load->view('kategori/form');

} else { $qry = $this->db->get_where ( 'category' , array('id' =>
} else {
$qry = $this->db->get_where
(
'category'
, array('id' => $this->input->post('txt_id'))
);
$data = array
(
'name' => $this->input->post('txt_name')
);
if( count($qry->result()) == 0 )
{
$data = array_merge
(
array( 'id' => $this->input->post('txt_id') )
, $data
);
$this->db->insert('category', $data);
} else {
$this->db->update
(
'category'
, $data
, array('id' => $this->input->post('txt_id'))
);
}
redirect(site_url().'/kategori');
}
}
}

StartStart StartStart toto toto Code!Code! Code!Code! –– filefile:: filefile:: /application//application/viewsviews/kategori/kategori/list.php/list.php /application//application/viewsviews/kategori/kategori/list.php/list.php

<html>

<head>

<title>Master Kategori</title>

<link href='<?php echo base_url(); ?>resources/css/style.css' rel='stylesheet' type='text/css' />

<script>

function jsTambah() {

document.location = 'kategori/tambah';

}

</script>

</head>

<body>

<h1>Master Kategori Buku (View List)</h1>

<br />

<input type='button' name='btnTambah' value='Tambah' onClick='javascript: jsTambah();' />

<br /><br />

<table width='100%' align='center' cellspacing='0'

cellpadding='0'>

<tr>

<th width='1%'>Kode</th>

<th>Kategori</th>

<th width='100'><i>perintah</i></th>

</tr>

<?php

foreach($data as $row) {

?>

<tr>

<td><?php echo $row['id']; ?></td>

<td><?php echo $row['name']; ?></td>

<td align='center'>

<a

href='<?php echo site_url(); ? >/kategori/edit/<?php echo $row['id']; ?>'

>Edit</a> |

<a href='<?php echo site_url(); ? >/kategori/hapus/<?php echo $row['id']; ?>'>Hapus</a>

</td>

</tr>

<?php

}

?>

</table>

</body>

</html>

StartStart StartStart toto toto Code!Code! Code!Code! –– ScreenshotScreenshot:: ScreenshotScreenshot:: KategoriKategori KategoriKategori (list)(list) (list)(list)

: : KategoriKategori Kategori Kategori (list)(list) (list) (list) rizky tahara shita - http://tahara.web.id 61
: : KategoriKategori Kategori Kategori (list)(list) (list) (list) rizky tahara shita - http://tahara.web.id 61

StartStart StartStart toto toto Code!Code! Code!Code! –– filefile:: filefile:: /application//application/viewsviews/kategori/kategori/form.php/form.php /application//application/viewsviews/kategori/kategori/form.php/form.php

<html>

<head>

<title>Master Kategori</title>

<link href='<?php echo base_url(); ?>resources/css/style.css' rel='stylesheet' type='text/css' />

<script>

function jsView() {

document.location = '<?php echo site_url(); ?>/kategori';

}

</script>

</head>

<body>

<h1>Master Kategori Buku (Form)</h1>

<br />

<input type='button' name='btn_view' value='Kembali ke View' onClick='javascript:

jsView();' />

<br /><br />

<?php echo form_open('kategori/simpan'); ?>

<table width='600' align='center' cellspacing='0' cellpadding='0'>

<tr>

<th align='right' width='20%'>ID</th>

<td>

<?php echo form_error('txt_id'); ?>

<input type='text' name='txt_id' size='3' maxlength='3' readOnly='readOnly' class='read_only'

value='<?php if(isset($data)) echo $data[0]['id']; else echo set_value('txt_id'); ?>' />

</td>

</tr>

<tr>

<th align='right'>Kategori*</th>

<td>

<?php echo form_error('txt_name'); ?>

<input type='text' name='txt_name' size='30'

maxlength='30'

value='<?php if(isset($data)) echo $data[0]['name']; else echo set_value('txt_name'); ?>' />

</td>

</tr>

<tr>

<td colspan='2'>

<i>* - Wajib isi</i>

<div align='right'>

<input type='submit'

name='cmd_simpan'

value='Simpan' />

</div>

</td>

</tr>

</table>

</form>

</body>

</html>

StartStart StartStart toto toto Code!Code! Code!Code! –– ScreenshotScreenshot:: ScreenshotScreenshot:: KategoriKategori KategoriKategori (form)(form) (form)(form)

: : KategoriKategori Kategori Kategori (form)(form) (form) (form) rizky tahara shita - http://tahara.web.id 63
: : KategoriKategori Kategori Kategori (form)(form) (form) (form) rizky tahara shita - http://tahara.web.id 63

StartStart StartStart toto toto Code!Code! Code!Code! –– filefile:: filefile:: /application//application/controllerscontrollers/buku.php/buku.php /application//application/controllerscontrollers/buku.php/buku.php

<?php

class Buku extends CI_Controller {

public function

construct()

{

 

parent:: construct();

$this->load->database();

}

public function index()

{

 

$qry = $this->db->query('select * from books'); $view['data'] = $qry->result_array();

$this->load->view('buku/list', $view);

}

public function tambah()

{

 

$data['kategori_combo'] = $this->_kategori_combo_generate();

$this->load->view('buku/form', $data);

}

public function edit($prm_key = '')

{

 

if(trim($prm_key) != '')

{

$view['kategori_combo'] = $this->_kategori_combo_generate();

$qry = $this->db->get_where

(

'books' , array('id' => $prm_key)

); $view['data'] = $qry->result_array();

$this->load->view('buku/form', $view);

} else { redirect(site_url().'/buku');

}

}

public function hapus($prm_key = '') { if(trim($prm_key) != '') { $qry = $this->db->delete

(

'books'

, array

(

'id' => $prm_key

}

);

)

redirect(site_url().'/buku');

}

StartStart StartStart toto toto Code!Code! Code!Code! –– filefile:: filefile:: /application//application/controllerscontrollers/buku.php/buku.php /application//application/controllerscontrollers/buku.php/buku.php (cont.)(cont.) (cont.)(cont.)

public function simpan()

{

$this->load->library('form_validation');

$rules = array

(

 

array

 

(

 

'field' => 'txt_id' , 'label' => 'ID' , 'rules' => 'trim'

 

)

,

array

 

(

'field' => 'cmb_kategori'

, 'label' => 'Kategori'

, 'rules' => 'trim|required'

)

 

,

array

 

(

'field' => 'txt_judul'

, 'label' => 'Judul'

, 'rules' => 'trim|required'

)

);

$this->form_validation->set_rules($rules); $this->form_validation->set_error_delimiters('<code>', '</code>');

if($this->form_validation->run() == FALSE)

{

$this->load->view('buku/form');

} else {

$qry = $this->db->get_where

(

'books'

, array('id' => $this->input->post('txt_id'))

);

$data = array

(

 

'category_id' => $this->input->post('cmb_kategori')

, 'title' => $this->input->post('txt_judul')

, 'isbn' => $this->input->post('txt_isbn')

);

if( count($qry->result()) == 0 )

{

$data = array_merge

(

array( 'id' => $this->input->post('txt_id') )

, $data

); $this->db->insert('books', $data);

} else { $this->db->update

(

'books'

, $data

, array('id' => $this->input->post('txt_id'))

}

);

redirect(site_url().'/buku');

}

}

StartStart StartStart toto toto Code!Code! Code!Code! –– filefile:: filefile:: /application//application/controllerscontrollers/buku.php/buku.php /application//application/controllerscontrollers/buku.php/buku.php (cont.)(cont.) (cont.)(cont.)

private function _kategori_combo_generate()

{

 

$qry = $this->db->query('select * from category');

$result = $qry->result_array();

return $result;

}

}

StartStart StartStart toto toto Code!Code! Code!Code! –– filefile:: filefile:: /application//application/viewsviews/buku/buku/list.php/list.php /application//application/viewsviews/buku/buku/list.php/list.php

<html>

<head>

<title>Master Buku</title>

<link href='<?php echo base_url(); ?>resources/css/style.css' rel='stylesheet' type='text/css' />

<script>

function jsTambah() {

document.location = 'buku/tambah';

}

</script>

</head>

<body>

<h1>Master Buku (View List)</h1>

<br />

<input type='button' name='btnTambah' value='Tambah' onClick='javascript: jsTambah();' />

<br /><br />

<table width='100%' align='center' cellspacing='0' cellpadding='0'>

<tr>

<th width='1%'>Kode</th>

<th width='15%'>Kategori</th>

<th>Judul</th>

<th width='10%'>ISBN</th>

<th width='100'><i>perintah</i></th>

</tr>

<?php

foreach($data as $row) {

?>

<tr>

<td><?php echo $row['id']; ?></td>

<td><?php echo $row['category_id']; ?></td>

<td><?php echo $row['title']; ?></td>

<td><?php echo $row['isbn']; ?></td>

<td align='center'>

<a href='<?php echo site_url(); ?>/buku/edit/<? php echo $row['id']; ?>'>Edit</a> |

<a href='<?php echo site_url(); ?>/buku/hapus/<? php echo $row['id']; ?>'>Hapus</a>

</td>

</tr>

<?php

}

?>

</table>

</body>

</html>

StartStart StartStart toto toto Code!Code! Code!Code! –– ScreenshotScreenshot:: ScreenshotScreenshot:: BukuBuku BukuBuku (list)(list) (list)(list)

Screenshot Screenshot : : BukuBuku Buku Buku (list)(list) (list) (list) rizky tahara shita - http://tahara.web.id 68
Screenshot Screenshot : : BukuBuku Buku Buku (list)(list) (list) (list) rizky tahara shita - http://tahara.web.id 68

StartStart StartStart toto toto Code!Code! Code!Code! –– filefile:: filefile:: /application//application/viewsviews/buku/buku/form.php/form.php /application//application/viewsviews/buku/buku/form.php/form.php

<html>

<head>

<title>Master Buku</title>

<link href='<?php echo base_url(); ? >resources/css/style.css' rel='stylesheet' type='text/css' />

<script>

function jsView() {

document.location = '<?php echo site_url(); ? >/buku';

}

</script>

</head>

<body>

<h1>Master Buku (Form)</h1>

<br />

<input type='button' name='btn_view' value='Kembali ke View' onClick='javascript: jsView();' />

<br /><br />

<?php echo form_open('buku/simpan'); ?>

<table width='600' align='center' cellspacing='0'

cellpadding='0'>

<tr>

<th align='right' width='20%'>Kode<br /><small><i>(auto)</i></small></th>

<td>

<?php echo form_error('txt_id'); ?>

<input type='text' name='txt_id' size='3' maxlength='3' readOnly='readOnly' class='read_only'

value='<?php if(isset($data)) echo $data[0]['id']; else echo set_value('txt_id'); ?>' />

</td>

</tr>

<tr>

<th align='right'>Kategori*</th>

<td>

<?php echo form_error('cmb_kategori'); ?>

<select name='cmb_kategori'>

<?php

foreach ( $kategori_combo as $rows )

{

echo '<option value="'.$rows['id'].'">'. $rows['name'].'</option>';

}

?>

</select>

</td>

</tr>

StartStart StartStart toto toto Code!Code! Code!Code! –– filefile:: filefile:: /application//application/viewsviews/buku/buku/form.php/form.php /application//application/viewsviews/buku/buku/form.php/form.php (cont.)(cont.) (cont.)(cont.)

<tr>

<th align='right'>Judul*</th>

<td>

<?php echo form_error('txt_judul'); ?>

<input type='text' name='txt_judul' size='60'

maxlength='200'

value='<?php if(isset($data)) echo $data[0] ['title']; else echo set_value('txt_judul'); ?>' />

</td>

</tr>

<tr>

<th align='right'>ISBN</th>

<td>

<?php echo form_error('txt_isbn'); ?>

<input type='text' name='txt_isbn' size='30'

maxlength='30'

value='<?php if(isset($data)) echo $data[0] ['isbn']; else echo set_value('txt_isbn'); ?>' />

</td>

</tr>

<tr>

<td colspan='2'>

<i>* - Wajib isi</i>

<div align='right'>

<input type='submit' name='cmd_simpan' value='Simpan' />

</div>

</td>

</tr>

</table>

</form>

</body>

</html>

StartStart StartStart toto toto Code!Code! Code!Code! –– ScreenshotScreenshot:: ScreenshotScreenshot:: BukuBuku BukuBuku (form)(form) (form)(form)

Screenshot Screenshot : : BukuBuku Buku Buku (form)(form) (form) (form) rizky tahara shita - http://tahara.web.id 71
Screenshot Screenshot : : BukuBuku Buku Buku (form)(form) (form) (form) rizky tahara shita - http://tahara.web.id 71

StartStart StartStart toto toto Code!Code! Code!Code! –– filefile:: filefile:: /resources/css/style.css/resources/css/style.css /resources/css/style.css/resources/css/style.css

body { background-color: #fff; margin: 40px; font-family: Lucida Grande, Verdana, Sans-serif; font-size: 14px; color: #4F5155;

}

a { color: #003399; background-color: transparent; font-weight: normal;

}

h1 { color: #444; background-color: transparent; border-bottom: 1px solid #D0D0D0; font-size: 20px; font-weight: bold; margin: 24px 0 2px 0; padding: 5px 0 6px 0;

}

code { font-family: Monaco, Verdana, Sans-serif; font-size: 12px; background-color: #FF8181; border: 1px solid
code {
font-family: Monaco, Verdana, Sans-serif;
font-size: 12px;
background-color: #FF8181;
border: 1px solid #D0D0D0;
color: #002166;
display: block;
margin: 5px 0px;
padding: 5px;
}
table, th, td {
padding: 0px;
margin: 0px;
border: 1px solid #D0D0D0;
color: #002166;
}
th {
background-color: #f9f9f9;
}
td, th {
padding: 3px;
}
.read_only{
border: none;
}

LoveLoveLoveLove totototo Code?Code?Code?Code?

Full EER:

LoveLove Love Love toto to to Code?Code? Code? Code? ● Full EER: rizky tahara shita -
LoveLove Love Love toto to to Code?Code? Code? Code? ● Full EER: rizky tahara shita -

What'sWhat'sWhat'sWhat's Next?Next?Next?Next?

Many controllers with single view.

Moving to AJAX based development with Web 2.0 technology.

jQuery, Prototype + Scriptaculous, MooTools.

Learn how to use caching.

server cache.

database cache.

view cache.

Using template engine.

Smarty PHP Template Engine Framework.

Template Lite.

and many more improvement

Resource

ResourceResourceResourceResource

Abeysinghe, Samisa. “PHP Team Development”, Packt Publishing, September 2010, Birmingham, UK.

Griffiths, Adam. “Codeigniter 1.7 Professional Development”, Packt Publishing, April 2010, Birmingham, UK.

CodeIgniter User Guide Version 2.0.2, 2011, “CodeIgniter User Guide”, http://codeigniter.com/user_guide/index.html , April 2011.

PenutupPenutupPenutupPenutup

Terima Kasih