13 April 2010

[Tutorial] add, update, delete pada codeigniter

sekarang begitu banyak framework untuk mempermudah dalam pemprograman.. (mempermudah bagi yg tau)..  :D
saya akan memberikan sedikit tutorial untuk add, update, delete..

1. Install Codeigniter.
   
dapat dilihat http://codeigniter.com/user_guide/installation/index.html ...
(misal saya merename dg nama "UMB")

2. Setting config/autoload seperti ini.

libraries yg dipanggil.
$autoload['libraries'] = array('database', 'template', 'encrypt', 'form_validation', 'session');

helper yg dibutuhkan.
$autoload['helper'] = array('url','form','text','date','typography','indotgl');

3. Create database dan tabel..

silahkan buat database dg nama sesuai keinginan anda/setting pada autoload.
CREATE TABLE IF NOT EXISTS `UmbWhiteList` (
  `UmbWhiteListPK` int(11) NOT NULL auto_increment,
  `Msisdn` varchar(20) NOT NULL,
  `Imsi` varchar(20) NOT NULL,
  `IsActive` tinyint(1) NOT NULL default '1',
  PRIMARY KEY  (`UmbWhiteListPK`),
  UNIQUE KEY `Msisdn` (`Msisdn`,`Imsi`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;



4. Buat controller dg nama UmbWL.php.

lalu isi nya :
class UmbWL extends Controller{
function __construct(){
parent::Controller();
$this->load->model("MUmbWL","",TRUE);
$this->load->library("pagination");
$this->load->library("table");
}

function listUmbWL(){
    $data['base_url'] = base_url().'index.php/UmbWL/listUmbWL/';
       $data['total_rows'] = $this->db->count_all('UmbWhiteList');
       $data['per_page'] = '2';
       $this->pagination->initialize($data);
$data["no"] = $this->uri->segment(3);
$data["title"] = "Daftar UMB White List Pertamina";
$data['next_link'] = 'selanjutnya';
$data['prev_link'] = 'sebelumnya';
$data["browse_UmbWL"] = $this->MUmbWL->select($data["per_page"],$this->uri->segment(3));

$this->load->view("listUmbWL",$data);
}

function inputUmbWL(){

$data = array();

$data["Msisdn"] = array (
"name" => "Msisdn",
"size" => "20",
"maxlength" => "50",
);

$data["Imsi"] = array (
"name" => "Imsi",
"size" => "20",
"maxlength" => "50",
);

$this->load->view("UmbWL_create",$data);
}

function insert(){

$data = array(
"Msisdn" => $this->input->post("Msisdn"),
"Imsi" => $this->input->post("Imsi"),
"IsActive" => $this->input->post("IsActive"),
);

$this->load->model("MUmbWL");
$this->MUmbWL->insert_UmbWL($data);
redirect('UmbWL/listUmbWL','refresh');
}

function editUmbWL($UmbWhiteListPK){
$this->load->model('MUmbWL');
$data = array();

$data["Msisdn"] = array (
"name" => "Msisdn",
"size" => "20",
"maxlength" => "50",
);

$data["Imsi"] = array (
"name" => "Imsi",
"size" => "20",
"maxlength" => "50",
);

if((int)$UmbWhiteListPK > 0){
  $query = $this->MUmbWL->edit($UmbWhiteListPK);
  $data['UmbWhiteListPK']['value'] = $query['UmbWhiteListPK'];
  $data['Msisdn']['value'] = $query['Msisdn'];
  $data['Imsi']['value'] = $query['Imsi'];
  $data['IsActive']['value'] = $query['IsActive'];
}          
$this->load->view('editUmbWL',$data);  
}

function edit(){
$this->load->model('MUmbWL');

$this->MUmbWL->entry_edit();    
redirect('UmbWL/listUmbWL','refresh');
}


function hapus($UmbWhiteListPK){
    $this->MUmbWL->deleteUmbWL($UmbWhiteListPK);
    redirect('UmbWL/listUmbWL','refresh');
}

}
?>



5. Buat Model dgn nama MUmbWL.php

dan isinya :

Class MUmbWL extends Model{
 function __construct(){
  parent::model();
 }

 function select($limit1,$limit2){
  $data = array();
  $this->db->select("*");
  $this->db->from("UmbWhiteList");
  $this->db->limit($limit1,$limit2);
  $hasil = $this->db->get();
  if($hasil->num_rows() > 0){
   $data = $hasil->result();
  }
  $hasil->free_result();
  return $data;
 }

function insert_UmbWL($data=array()){
    $this->db->insert("UmbWhiteList",$data);
}

function edit($ProductIDPK){
//$this->load->database();
$query = $this->db->getwhere('UmbWhiteList',array('UmbWhiteListPK' => $ProductIDPK));
return $query->row_array();        
 }

function entry_edit(){
   //$this->load->database();
$data = array(
        'Msisdn' => $this->input->post('Msisdn'),
        'Imsi' => $this->input->post('Imsi'),
        'IsActive' => $this->input->post('IsActive'),
);
$this->db->where('UmbWhiteListPK',$this->input->post('UmbWhiteListPK'));
$this->db->update('UmbWhiteList',$data);
}

function deleteUmbWL($UmbWhiteListPK){
     $this->db->where('UmbWhiteListPK', $UmbWhiteListPK);
    $this->db->delete('UmbWhiteList');  
}
}
?>

6. Membuat tampilannya.

    6.1. Tampilan lihat.
           buat file pada view dg nama listUmbWL.php lalu codenya :

<style type="text/css">
.style14 {font-weight: bold; color: #FFFFFF; }
.style10 {
    font-family: Tahoma;
    font-size: 12px;
}
.style11 {
    font-family: Tahoma;
    font-size: 20px;
    color: #009999;
}
</style>
<table width=613 border=0 cellspacing=3 cellpadding=3 align='center' class=style10>
  <tr>
    <td colspan="8" bgcolor="#666699"><div align="center" class="style14">:: DATA UMB WHITE LIST :: </div></td>
  </tr>
  <tr bgcolor=#666699 class="style14">
    <td width="30" height="39" align='center' bgcolor="#666699"><div align='center'>No</div></td>
    <td width="190"><div align='center'>MSISDN</div></td>
    <td width="70"><div align='center'>IMSI</div></td>
    <td width="60"><div align='center'>Active</div></td>
    <td width="73" colspan="3"><div align='center'>Action</div></td>
  </tr>
  <?php
$i = $no+1;
foreach($browse_UmbWL as $rec_agg){ ?>
<tr bgcolor="#CCCCCC" class="style10">
<td align="center">
<? echo $i++; ?>
</td>
<td align="center">
<? echo $rec_agg->Msisdn; ?>
</td>
<td align="center">
<? echo $rec_agg->Imsi; ?>
</td>
<td align="center">
<?
if (($rec_agg->IsActive)=="1"){
echo "Active";
}
else
{
echo "InActive";
}
//echo $rec_agg->IsActive;
?>
</td>
<td align="center" width="25">
<? echo anchor('UmbWL/editUmbWL/'.$rec_agg->UmbWhiteListPK,'edit'); ?>
</td>
<td align="center" width="25">
<? echo anchor('UmbWL/hapus/'.$rec_agg->UmbWhiteListPK,'hapus'); ?>
</td>
<td align="center" width="23"><div align='center'>Detail</div></td>
  </tr>
  <?
}
?>
</table>

<?php
echo "<p align='center'>".$this->pagination->create_links()."</p>";

$this->load->view("pertamina_view");
?>



tampilan





    6.2. Tampilan tambah (create/add).
           buat file pada view dg nama UmbWL_create.php lalu codenya :
<html>

<body>

<style type="text/css">
.style4 {font-size: 12px; color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif;}
.style1 {font-size: 14px; color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif;}

</style>
<?php echo form_open("UmbWL/insert");?>

<table width="500" border="0" align="center">
          <tr bgcolor="#666699">
            <td colspan="2"><div align="center" class="style1"><strong>::
                TAMBAH UMB WHITE LIST :: </strong></div></td>
          </tr>
          <tr>
            <td width="166" bgcolor="#666699"><span class="style4">MSISDN</span></td>
            <td width="324" bgcolor="#CCCCCC"><p>
                <label>
                <? echo form_input($Msisdn); ?>
                <font color="#FF0000">*</font> </label>
            </td>
          </tr>
          <tr>
            <td bgcolor="#666699"><span class="style4">IMSI</span></td>
            <td bgcolor="#CCCCCC"><label>
              <? echo form_input($Imsi); ?>
              </label></td>
          </tr>
          <tr>
            <td bgcolor="#666699"><span class="style4">Keterangan</span></td>
            <td bgcolor="#CCCCCC"><label>
              <?
                  $options = array('1' => 'active', '0' => 'inactive');
                echo form_dropdown('IsActive',$options)  ?>
             </label></td>
          </tr>
        
          <tr>
            <td bgcolor="#666699"><span class="style4">&nbsp;</span></td>
            <td bgcolor="#CCCCCC"><div align="left">
                <label>
                <?
                echo form_submit("sbm_submit","Submit");
                echo form_close();
                ?>
                </label>
              </div></td>
          </tr>
          <tr>
            <td colspan="2" bgcolor="#666699">&nbsp;</td>
          </tr>
          <tr>
            <td colspan="2"><strong><a href="" class="style5">Kembali</a></strong></td>
          </tr>
        </table>
</body>
</html>

tampilan :

     6.2. Tampilan ubah (edit/update).
           buat file pada view dg nama UmbWL_create.php lalu codenya :

<html>

<body>

<style type="text/css">
.style4 {font-size: 12px; color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif;}
.style1 {font-size: 14px; color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif;}

-->
</style>
<?php echo form_open("UmbWL/edit");
echo form_input('UmbWhiteListPK',$UmbWhiteListPK['value']);
?>

<table width="500" border="0" align="center">
          <tr bgcolor="#666699">
            <td colspan="2"><div align="center" class="style1"><strong>::
                EDIT UMB WHITE LIST :: </strong></div></td>
          </tr>
          <tr>
            <td width="166" bgcolor="#666699"><span class="style4">MSISDN</span></td>
            <td width="324" bgcolor="#CCCCCC"><p>
                <label>
                <? echo form_input($Msisdn); ?>
                <font color="#FF0000">*</font> </label>
            </td>
          </tr>
          <tr>
            <td bgcolor="#666699"><span class="style4">IMSI</span></td>
            <td bgcolor="#CCCCCC"><label>
              <? echo form_input($Imsi); ?>
              </label></td>
          </tr>
          <tr>
            <td bgcolor="#666699"><span class="style4">Keterangan</span></td>
            <td bgcolor="#CCCCCC"><label>
              <?
                  $options = array('1' => 'active', '0' => 'inactive');
                echo form_dropdown('IsActive',$options, $IsActive)  ?>
             </label></td>
          </tr>
        
          <tr>
            <td bgcolor="#666699"><span class="style4">&nbsp;</span></td>
            <td bgcolor="#CCCCCC"><div align="left">
                <label>
                <?
                echo form_submit("sbm_submit","Submit");
                echo form_close();
                ?>
                </label>
              </div></td>
          </tr>
          <tr>
            <td colspan="2" bgcolor="#666699">&nbsp;</td>
          </tr>
          <tr>
            <td colspan="2"><strong><a href="" class="style5">Kembali</a></strong></td>
          </tr>
        </table>
</body>

</html>

Tampilannya :
 


7 KoMenTaR:

Unknown mengatakan...

Nice info.. tp msh blm paham gan.. :D

Ibnu Qoyyim Al Farizy mengatakan...

semua dari belajar... :D

ephie mengatakan...

mangstabbbb.... [:-bd]

adi mengatakan...

ada pesan error seperti ini tu gmn mas??

An Error Was Encountered

Unable to load the requested file: helpers/indotgl_helper.php

mohon bantuannya...!

adi mengatakan...

ada pesan error seperti ini gmn ??
mohon bantuanya mas..!
An Error Was Encountered

Unable to load the requested file: helpers/indotgl_helper.php

adi mengatakan...

ada error message seperti ini mas..!

An Error Was Encountered

Unable to load the requested file: helpers/indotgl_helper.php

mohon bantuanya,.,.,!!

Ibnu Qoyyim Al Farizy mengatakan...

hehe,, mohon maaf oom baru jawab...
itu dikarenakan tidak adanya file indotgl_helper di tempat oom.. jadi hapus ajah source nya pada settingan config/autoload pada bagian helper...

terima kasih atas kunjungannya oom...

QR Code

My Headlines


Dengan meninggalkan komentar, saya sangat berterima kasih sekali karena dapat menjadikan blog ini berkembang.. terima kasih