create (crud) - andiwre.itmaranatha.organdiwre.itmaranatha.org/download/pwl/9-ci_db.pdf ·...

11
2012/11/27 1 12-05-12 Andi Wahju R E [email protected] 12-05-12 PWL Create (Crud) Create (Crud) Di file models, tambahkan fungsi: <?php function tambah_data($data) { //$data berisi array nilai yg diinput $this->db->insert('nama_table', $data); return; } ?>

Upload: phamnguyet

Post on 15-Feb-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Create (Crud) - andiwre.itmaranatha.organdiwre.itmaranatha.org/download/pwl/9-ci_db.pdf · 2012/11/27 1 12-05-12 AndiWahjuR E awreman@gmail.com 12-05-12 PWL Create (Crud) • Di file

2012/11/27

1

12-05-12

Andi Wahju R E

[email protected]

12-05-12

PWL

Create (Crud)Create (Crud)

• Di file models, tambahkan fungsi:

<?php

function tambah_data($data)

{

//$data berisi array nilai yg diinput

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

return;

}

?>

Page 2: Create (Crud) - andiwre.itmaranatha.organdiwre.itmaranatha.org/download/pwl/9-ci_db.pdf · 2012/11/27 1 12-05-12 AndiWahjuR E awreman@gmail.com 12-05-12 PWL Create (Crud) • Di file

2012/11/27

2

12-05-12

PWL

Create (Crud)Create (Crud)

• Untuk inisialisasi form (di file views atau file models):

//Untuk Input Type Text

$namauser = array('name' => 'emp_name',

'id' => 'emp_name',

'maxlength' => '25'

);

//Untuk Select Box

$jobuser = array();

$jobuser[0] = '--Select Job--';

$this->db->select('job_id, job_title');

$query = $this->db->get('jobs');

if ($query->num_rows() > 0)

{

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

{

$jobuser[$row->job_id] = $row->job_title;

}

}

12-05-12

PWL

Create (Crud) Create (Crud) (cont’d)(cont’d)

• Untuk pemanggilan form (di file views):

<?php

echo form_fieldset('nama_legend');

// form_open('nama_controller/nama_fungsi');

echo form_open('Latihan/tambahdata');

echo form_input($namauser);

echo form_dropdown('jobs', $jobuser);

echo form_submit('submit', 'Submit');

//beberapa contoh input type lainnya

echo form_password('$pwd');

echo form_hidden();

$js = 'onClick="myfungsi()"';

echo form_button('click', 'Simpan', $js);

echo form_close();

echo form_fieldset_close();

?>

Page 3: Create (Crud) - andiwre.itmaranatha.organdiwre.itmaranatha.org/download/pwl/9-ci_db.pdf · 2012/11/27 1 12-05-12 AndiWahjuR E awreman@gmail.com 12-05-12 PWL Create (Crud) • Di file

2012/11/27

3

12-05-12

PWL

Create (Crud) Create (Crud) (cont’d)(cont’d)

• Di file controller:

function tambahdata()

{

//array yg menampung nilai yg diinput

$data = array('name' => $this->input->post('emp_name'),

'job_id' => $this->input->post('jobs'));

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

$this->modellatihan->tambah_data($data);

$this->index();

}

12-05-12

PWL

Update (Update (crUdcrUd))

• Pemanggilan fungsi update di file views

<?php

//untuk melihat terlebih dulu nilai awal dr data yg dipilih

echo anchor('latihan/lihat_det/'.$row->employee_id, 'Edit');

?>

Page 4: Create (Crud) - andiwre.itmaranatha.organdiwre.itmaranatha.org/download/pwl/9-ci_db.pdf · 2012/11/27 1 12-05-12 AndiWahjuR E awreman@gmail.com 12-05-12 PWL Create (Crud) • Di file

2012/11/27

4

12-05-12

PWL

Update (Update (crUdcrUd) ) (cont’d)(cont’d)

• Di file controllers (untuk mengambil data detail dari yg

dipilih):

function lihat_det($emp_id)

{

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

//inisialisasi form untuk halaman update

$data = $this->modellatihan->inisialisasi();

if((int)$emp_id > 0){

//mengambil data yg ada di DB untuk emp_id yg dipilih

$dt = $this->modellatihan->view_db($emp_id);

//form yg sudah diinisialisasi diisi value defaultnya

$data['uiduser']['value'] = dt['employee_id'];

$data['unamauser']['value'] = $dt['name'];

//utk select box, valuenya ditampung

$data['val_job'] = $dt['job_id'];

}

//mengirimkan data dr DB yg sudah ditampung di $data ke

//tampilan update di file views

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

}

12-05-12

PWL

Update (Update (crUdcrUd) ) (cont’d)(cont’d)

• Di file models (untuk mengambil data dr emp_id tertentu):

<?php

function view_db($emp_id)

{

$query = "SELECT * FROM employees WHERE employee_id= ?";

$fetch = $this->db->query($query, $emp_id);

if($fetch->num_rows() > 0)

{

return $fetch->row_array();

}

}

?>

Page 5: Create (Crud) - andiwre.itmaranatha.organdiwre.itmaranatha.org/download/pwl/9-ci_db.pdf · 2012/11/27 1 12-05-12 AndiWahjuR E awreman@gmail.com 12-05-12 PWL Create (Crud) • Di file

2012/11/27

5

12-05-12

PWL

Update (Update (crUdcrUd) ) (cont’d)(cont’d)

• Di file models (untuk inisialisasi form awal):

function inisialisasi(){

//untuk text box hidden

$dt['uiduser'] = array('name' => 'id_user',

'id' => 'id_user',

'maxlength' => '25',

'type' => 'hidden');

$dt['unamauser'] = array('name' => 'emp_name',

'id' => 'emp_name',

'maxlength' => '25’);

$dt['ujobs'] = array();

$dt['ujobs']['0'] = '--Select Job--';

$this->db->select('job_id, job_title');

$query = $this->db->get('jobs');

if($query->num_rows() > 0){

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

$dt['ujobs'][$row->job_id] = $row->job_title;

}

}

//krn inisialisasi dng fungsi, harus ada nilai yg dikembalikan

return $dt;

}

12-05-12

PWL

Update (Update (crUdcrUd) ) (cont’d)(cont’d)

• Untuk pemanggilan form di file views (updatelatihan.php):

<?php

echo form_fieldset('nama_legend');

// form_open('nama_controller/nama_fungsi');

echo form_open('Latihan/ubahdata');

//untuk menyimpan emp_id yg dipilih, tdk terlihat krn type=hidden

echo form_input($uiduser);

echo form_input($unamauser);

echo form_dropdown('ujobs', $ujobs, $val_job);

echo form_submit('submit', 'Submit');

echo form_close();

echo form_fieldset_close();

?>

Page 6: Create (Crud) - andiwre.itmaranatha.organdiwre.itmaranatha.org/download/pwl/9-ci_db.pdf · 2012/11/27 1 12-05-12 AndiWahjuR E awreman@gmail.com 12-05-12 PWL Create (Crud) • Di file

2012/11/27

6

12-05-12

PWL

Update (Update (crUdcrUd) ) (cont’d)(cont’d)

• Di file controllers untuk menampung data baru dan

mengirimkan ke halaman models untuk update data lama:

function ubahdata()

{

//menampung nilai yg akan diupdate

$data = array('name' => $this->input->post('emp_name'),

'job_id' => $this->input->post('ujobs'));

//untuk mengambil emp_id dr data yg ingin diubah

$emp_id = $this->input->post('id_user');

//mengirimkan nilai yg baru ke halaman models

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

$this->modellatihan->edit_data($data, $emp_id);

$this->index();

}

12-05-12

PWL

Update (Update (crUdcrUd) ) (cont’d)(cont’d)

• Di file models:

function edit_data($data, $emp_id)

{

$this->db->where('employee_id', $emp_id);

$this->db->update('employees', $data);

return;

}

Page 7: Create (Crud) - andiwre.itmaranatha.organdiwre.itmaranatha.org/download/pwl/9-ci_db.pdf · 2012/11/27 1 12-05-12 AndiWahjuR E awreman@gmail.com 12-05-12 PWL Create (Crud) • Di file

2012/11/27

7

12-05-12

PWL

Delete (Delete (cruDcruD))• Pemanggilan fungsi delete di halaman views

• Fungsi hapus di halaman controllers:

<?php echo anchor('latihan/hapus/'.$row->employee_id, 'Del'); ?>

function hapus($emp_id)

{

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

$this->modellatihan->del_db($emp_id);

$this->index();

}

12-05-12

PWL

Delete (Delete (cruDcruD) ) ((con’tcon’t))

• Di file models untuk menghapus data:

<?php

function del_db($id)

{

$dt = array('employee_id' => $id);

$this->db->delete('employees', $dt);

}

?>

Page 8: Create (Crud) - andiwre.itmaranatha.organdiwre.itmaranatha.org/download/pwl/9-ci_db.pdf · 2012/11/27 1 12-05-12 AndiWahjuR E awreman@gmail.com 12-05-12 PWL Create (Crud) • Di file

2012/11/27

8

12-05-12

PWL

SessionSession

• Di file views (membuat tampilan login):

<?php

$uname = array('name' => 'uname',

'id' => 'uname',

'maxlength' => '25');

$pwd = array('name' => 'pwd',

'id' => 'pwd',

'maxlength' => '25');

// form_open('nama_controllers/nama_fungsi');

echo form_open('Login/logins');

echo form_input($uname);

echo form_password($pwd);

echo form_submit('submit', 'Login');

echo form_close();

?>

12-05-12

PWL

Session Session (cont’d)(cont’d)

• Di file controllers Login (memberi nilai ke variable session):

function logins(){

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

$hasil = $this->modellatihan->validasi();

if($hasil){

$det = $this->modellatihan->det_data();

//membuat variable session

$ses = array('username' => $this->input->post('uname'),

'login' => true,

'emp_id' => $det['employee_id'],

'name' => $det['name'],

'role' => $det['role']);

$this->session->set_userdata($ses);

//pindah ke controller user di function hal_member

redirect('user/hal_member');

}

else{

//pindah ke controllers latihan

redirect('latihan');

}

}

Page 9: Create (Crud) - andiwre.itmaranatha.organdiwre.itmaranatha.org/download/pwl/9-ci_db.pdf · 2012/11/27 1 12-05-12 AndiWahjuR E awreman@gmail.com 12-05-12 PWL Create (Crud) • Di file

2012/11/27

9

12-05-12

PWL

Session Session (cont’d)(cont’d)

• Di file models modellatihan (cek data login ke DB):

function validasi()

{

$this->db->where('username', $this->input->post('uname'));

$this->db->where('password', md5($this->input->post('pwd')));

$fetch = $this->db->get('user');

if($fetch->num_rows() == 1)

{

return true;

}

}

12-05-12

PWL

Session Session (cont’d)(cont’d)

• Di file models modellatihan (mengambil data detail dari yg

login):

function det_data()

{

$this->db->select('e.employee_id AS employee_id,

e.name AS name, u.role AS role');

$this->db->from('user u');

$this->db->join('employees e', 'u.employee_id = e.employee_id');

$this->db->where('u.username', $this->input->post('uname'));

$this->db->where('u.password', md5($this->input->post('pwd')));

$fetch = $this->db->get();

if($fetch->num_rows() == 1)

{

return $fetch->row_array();

}

}

Page 10: Create (Crud) - andiwre.itmaranatha.organdiwre.itmaranatha.org/download/pwl/9-ci_db.pdf · 2012/11/27 1 12-05-12 AndiWahjuR E awreman@gmail.com 12-05-12 PWL Create (Crud) • Di file

2012/11/27

10

12-05-12

PWL

Session Session (cont’d)(cont’d)

• Di file controllers User (mengambil nilai variable session yg

telah diinisialisasi ketika awal login dan mengirimkan ke

halaman views):

function hal_member()

{

//menampung data session ke dalam array $data

//$this->session->userdata('nama_var_session')

$data['uname'] = $this->session->userdata('username');

$data['emp_id'] = $this->session->userdata('emp_id');

$data['name'] = $this->session->userdata('name');

$data['role'] = $this->session->userdata('role');

//pindah ke halaman views hal_user.php

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

}

12-05-12

PWL

Session Session (cont’d)(cont’d)

• Di file views hal_user.php (halaman setelah login):

<html>

<body>

Welcome <?php echo ucwords($name); ?>.<br />

//link untuk logout

// anchor('nama_controller/nama_fungsi', 'Tulisan yg tampil');

<?php echo anchor('logout/index', 'Logout'); ?>

</body>

</html>

Page 11: Create (Crud) - andiwre.itmaranatha.organdiwre.itmaranatha.org/download/pwl/9-ci_db.pdf · 2012/11/27 1 12-05-12 AndiWahjuR E awreman@gmail.com 12-05-12 PWL Create (Crud) • Di file

2012/11/27

11

12-05-12

PWL

Session Session (cont’d)(cont’d)

• Di file controller Logout (keluar dr halaman setelah login

dan menghapus nilai variable session):

class Logout extends CI_Controller

{

function index()

{

$this->session->unset_userdata('username');

$this->session->unset_userdata('login');

$this->session->unset_userdata('emp_id');

$this->session->unset_userdata('name');

$this->session->unset_userdata('role');

redirect('latihan');

}

}