0% found this document useful (0 votes)
40 views7 pages

Code Igniter Pertemuan 7

The document discusses Code Igniter and performing CRUD (create, read, update, delete) operations on a database table. It includes creating controllers and models to insert, update, delete data. It also covers creating views to display data in a table and create forms for adding and editing data. Autoloading is configured to load the database library and model automatically. Views are created to display the data table, input form for adding new data, and edit form for updating existing data. The CRUD functions are tested by accessing the controller functions through a URL.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views7 pages

Code Igniter Pertemuan 7

The document discusses Code Igniter and performing CRUD (create, read, update, delete) operations on a database table. It includes creating controllers and models to insert, update, delete data. It also covers creating views to display data in a table and create forms for adding and editing data. Autoloading is configured to load the database library and model automatically. Views are created to display the data table, input form for adding new data, and edit form for updating existing data. The CRUD functions are tested by accessing the controller functions through a URL.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Code Igniter pertemuan 7

Insert, Update, Delete

Buat file didalam controller dengan nama Mhs1a.php


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

class Mhs1a extends CI_Controller {

public function insert()


{
// echo "Ini function insert";
$res=$this->Model_mhs1->InsertData('mahasiswa',array(
"nim"=>"33333",
"nama"=>"Hisyam",
"alamat"=>"Tangerang",
));
if ($res>=1)
{echo"<h2>berhasil insert data<hr /></h2>";

$data=$this->db->query('select * from mahasiswa');


foreach($data->result_array()as $d){
echo"Nim=",$d['nim'],"<br />" ;
echo"Nama=",$d['nama'],"<br />" ;
echo"alamat=",$d['alamat'],"<hr />" ;}}

else
{echo "gagal input data";}
}
public function update()
{
$res=$this->Model_mhs1->UpdateData('mahasiswa',array(
"alamat"=>"Yogyakarta",
),array('nim'=>"11111"));
if($res>=1){
echo"update sukses";
}else{
echo"update data gagal";}
}
public function delete(){
$res=$this->Model_mhs1->DeleteData('mahasiswa',array(
'nim'=>"11111"));
if($res>=1){
echo"delete sukses";
}else{
echo"delete data gagal";
}
}
}

========================================================================

Buat file didalam models dengan nama Model_mhs1.php


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

class Model_mhs1 extends CI_Model {

public function GetMahasiswa($where="")


{
$data=$this->db->query('select * from mahasiswa',$where);
return $data->result_array();
}

public function InsertData($TabelName,$data)


{
$res=$this->db->insert($TabelName,$data);
return $res;
}
public function UpdateData($tableName,$data,$where){
$res=$this->db->update($tableName,$data,$where);
return $res;
}
public function DeleteData($tableName,$where){
$res=$this->db->delete($tableName,$where);
return $res;
}
}
Pembuatan Form
Buat file didalam controller dengan nama mhs2.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Mhs2 extends CI_Controller {

public function index()


{
$data=$this->Model_mhs1->GetMahasiswa();
$this->load->view('tabel',array('data'=>$data));
// echo "tambah data";
}

public function add_data(){


// echo "Tambah data";
$this->load->view('form_add');
}
public function do_insert(){
$nim=$_POST['nim'];
$nama=$_POST['nama'];
$alamat=$_POST['alamat'];
$data_insert=array(
'nim'=>$nim,
'nama'=>$nama,
'alamat'=>$alamat
);
$res= $this->db->insert('mahasiswa',$data_insert);
if ($res>=1){
echo"insert data ok ";
}else{
echo "insert gagal";
}

}
public function do_delete($nim){
// echo"delete nim:",$nim ;
$where=array('nim'=>$nim);
$res=$this->Model_mhs1->DeleteData('mahasiswa',$where);
$data=$this->Model_mhs1->GetMahasiswa();
$this->load->view('tabel',array('data'=>$data));
}
public function do_update(){
// echo "<pre>";
// print_r($_POST);
// echo "</pre>";
$nim=$_POST['nim'];
$nama=$_POST['nama'];
$alamat=$_POST['alamat'];
$data_update=array(
'nama'=>$nama,
'alamat'=>$alamat
);
$where=array('nim'=>$nim);
$res= $this->Model_mhs1->UpdateData('mahasiswa',$data_update,$where);
if ($res>=1){
redirect('mhs2/index');
//$data=$this->Model_mhs1->GetMahasiswa();
// $this->load->view('tabel',array('data'=>$data));
}else{
echo "update gagal";
}
}
public function edit_data($nim){
$mhs =$this->Model_mhs1->GetMahasiswa("where nim=,'$nim'");
$data=array(
"nim"=>$mhs[0]['nim'],
"nama"=>$mhs[0]['nama'],
"alamat"=>$mhs[0]['alamat'],
);
$this->load->view('form_edit',$data);
}
}
Buat file di dalam view dengan nama table.php
<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
<title>Title here!</title>
</head>
<body>
<table border="1">
<tr style="background:gray;">
<th>No Induk</th>
<th>Nama</th>
<th>Alamat</th>
<th>Action</th>
</tr>

<?php foreach($data as $d){?>


<tr>
<td><?php echo $d['nim'];?></td>
<td><?php echo $d['nama'];?></td>
<td><?php echo $d['alamat'];?></td>
<td align="center">
<a href="<?php echo base_url().'index.php/mhs2/edit_data/',$d['nim'];?>">Edit</a>
<a href="<?php echo base_url().'index.php/mhs2/do_delete/',$d['nim'];?>">Delete</a>
</td>
</tr>
<?php } ?>
</table>
<a href="<?php echo base_url().'index.php/mhs2/add_data';?>">Tambah Data</a>
</body>
</html>
Buat file di dalam view dengan nama form_add.php
<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
<title>Title here!</title>
</head>
<body>
<form method="POST" action="<?php echo base_url()."index.php/mhs2/do_insert";?>">
<table>
<tr>
<td>No Induk</td><td><input type="text" name="nim"/></td>
</tr>

<tr>
<td>Nama</td><td><input type="text" name="nama"/></td>
</tr>
<tr>
<td>alamat</td><td><textarea name="alamat"></textarea></td>
</tr>
<tr>
<td><input type="submit" name="simpan"value="Simpan"/></td>
</tr>
</table>
</body>
</html>

Ubah autoload didalam application >> confiq >>autoload


view
Buat file di dalam form_edit.php
dengan nama
<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
<title>Title here!</title>
</head>
<body>
<form method="POST" action="<?php echo base_url().'index.php/mhs2/do_update';?>">
<table>
<tr>
<td>No Induk</td><td><input type="text" name="nim" value='<?php echo $nim;?>'
readonly /></td>
</tr>

<tr>
<td>Nama</td><td><input type="text" name="nama" value='<?php echo $nama;?
>'/></td>
</tr>
<tr>
<td>alamat</td><td><textarea name="alamat"><?php echo $alamat ?></textarea></td>
</tr>
<tr>
<td><input type="submit" name="simpan"value="Simpan"/></td>
</tr>
</table>
</body>
</html>

Jalankan
http://localhost/latihan1/index.php/mhs2/index

You might also like