Sabtu, Januari 21

File pertama CI mode CRUD

model_barang.php

<?php
class Model_barang extends CI_Model{
       
    /** Begin Proses CRUD pada tabel Suplyer **/
    //mengambil semua data pada tabel suplyer
    function get_suply(){
        $query = $this->db->get('suplyer');
          
        if ($query->num_rows() > 0){
                   
        foreach($query->result() as $bar){
             $data[] = $bar;
        }
        return $data;
        }
    }
   
    //fungsi insert ke tabel suplyer
    function insert_suply(){
        //mengambil referensi dari form update
        $suply = $_POST['suply'];
        $ket = $_POST['ket'];
               
        //proses array 
        $data = array (
            'nama_suplyer' => $suply,
            'ket' => $ket
        );
              
        $this->db->insert('suplyer',$data);
    }
   
    //mengambil semua data pada tabel barang dengan referensi id
    function suply_update(){
        $id = $this->uri->rsegment(3); //mengambil nilai segmen ke-3
        $sqlquery = 'SELECT * FROM suplyer WHERE id_suplyer =?';       
        $query = $this->db->query($sqlquery, $id);       
   
        if ($query->num_rows() > 0){
             foreach($query->result() as $bar){
                 $data[] = $bar;
             }
        return $data;
        }
    }
   
    //update data tabel pada tabel suplyer
    function pro_ups(){
        //mengambil referensi dari form update
        $id = $_POST['id'];
        $suply = $_POST['suply'];
        $ket = $_POST['ket'];
       
        //proses array 
        $data = array (
            'nama_suplyer' => $suply,
            'ket' => $ket
        );
       
        //update proses
        $this->db->where('id_suplyer', $id);
        $this->db->update('suplyer', $data);   
    }
   
    //menghaus baris dengan referensi id
    function hapus_suply(){
        $id = $this->uri->rsegment(3); //mengambil nilai segmen ke-3
   
        $this->db->where('id_suplyer', $id);
        $this->db->delete('suplyer');
    }
    /** End Proses CRUD pada tabel Suplyer **/
   
    /** Begin Proses CRUD pada tabel Barang **/
    //mengambil semua data pada tabel barang
    function get_barang(){
        $query = $this->db->get('barang');
        if ($query->num_rows() > 0){
                 
        foreach($query->result() as $bar){
            $data[] = $bar;
        }
        return $data;
        }
    }
   
    //fungsi insert ke tabel suplyer
    function insert_barang(){
         //mengambil referensi dari form update
        $suply = $_POST['suply'];
        $id_suply = $_POST['id'];
        $sn = $_POST['sn'];
        $jenis = $_POST['jenis'];
        $jumlah = $_POST['jumlah'];
       
        //proses array 
        $data = array (
            'nama_suplyer' => $suply,
            'id_suplyer' => $id_suplyer,
            'sn' => $sn ,
            'jumlah' => $jumlah,
            'jenis' => $jenis
        );
              
        $this->db->insert('barang',$data);
    }
   
    function lihat_barang(){
        $id = $this->uri->rsegment(3); //mengambil nilai segmen ke-3
        echo $id;
        /**
        $sqlquery = 'SELECT * FROM suplyer WHERE id_suplyer =?';       
        $query = $this->db->query($sqlquery, $id);       
        if ($query->num_rows() > 0){
           
           foreach($query->result() as $bar){
              $data[] = $bar;
           }
        return $data;
        }
        **/
    }
    /** End Proses CRUD pada tabel barang **/       
}
?>


barang.php

<?php
class Barang extends CI_Controller{
     function barang(){
        parent::__construct();
     
        $this->load->model('model_barang');
       
    }
   
    function index(){
        $data['title'] = 'Daftar Suplyer';
        $data['head'] = 'Daftar Suplyer';
       
        //memanggil fungsi dari model_barang.php
        $data['baris'] = $this->model_barang->get_suply();
        $this->load->view('suplyer', $data);
   }
   
    /** Begin Proses tabel Suplyer **/
    function insert_suply(){
        $this->model_barang->insert_suply();
        redirect('../barang');
    }
   
    function suply_update(){
        $data['title'] = 'Data Suplyer';
        $data['head'] = 'Ubah Data Suplyer';
                       
        //memanggil fungsi dari model_barang.php
        $data['baris'] = $this->model_barang->suply_update();
       
        $this->load->view('edit_suplyer', $data);
    }
   
    function ups(){
        $this->model_barang->pro_ups();
       
        redirect('../barang');

    }

    function hapus_suply(){
       
        $this->model_barang->hapus_suply();
        redirect('../barang');
    }
    /** End proses tabel suplyer **/
   
    /** Begin proses tabel barang **/
   
    //Pilih Suplyer
    function finsert_barang(){
        $data['title'] = 'Data barang';
        $data['head'] = 'Insert Data Barang';
       
        //memanggil fungsi dari model_barang.php
        $data['baris'] = $this->model_barang->suply_update();
       
        $this->load->view('stok_barang', $data);
    }
   
    //input proses
    function view_barang(){
        echo "OK";
    }
   
    /** End proses tabel barang **/
   
}

?>

suplyer.php

<html
<head>
    <title><?=$title?></title>
</head>

<body>
    <div align="center">
        <h3><?=$head?></h3>
        <div>
            <?php echo form_open('../barang/insert_suply');?>
            <label for="sn">Nama Suplyer :</label>
            <input type="text" name="suply" id="suply" maxlength="100" />
           
            <label for="nama_barang">Keterangan :</label>
            <input type="text" name="ket" id="ket" />
           
            <input type="submit" value="Simpan">   
           
            <?php form_close()?>
        </div>
<hr>
        <div>
            <?php
                if ($baris > 0){ 
            ?>
            <!-- begin tabel Suplyer -->
            <table border="1" cellpadding="2">
                <tr>
                    <th>Nama Suplyer</th>
                    <th>Keterangan</th>
                    <th>Ubah</th>
                    <th>Hapus</th>
                </tr>
       
            <?php foreach ($baris as $bar) { ?>
                <tr>
                    <td><?=anchor('../barang/finsert_barang/'.$bar->id_suplyer,$bar->nama_suplyer);?></td>
                    <td><?=$bar->ket?></td>
                    <td><?=anchor('../barang/suply_update/'.$bar->id_suplyer,'Update');?></td>
                    <td><?=anchor('../barang/hapus_suply/'.$bar->id_suplyer,'Hapus');?></td>
               </tr>
                   
            <?php    } }?>
            </table>
            <!-- end tabel suplyer -->
        </div>
    </div>
</body>
</html>


edit_suplyer.php

<html
<head>
    <title><?=$title?></title>
</head>

<body>
    <h3><?=$head?></h3>
    <?php foreach ($baris as $bar) { ?>
       <div>
            <?php echo form_open('../barang/ups');?>
           
            <input type="hidden" name="id" value="<?=$bar->id_suplyer?>" />
           
            <label for="sn">Nama Suplyer :</label>
            <input type="text" name="suply" id="suply" maxlength="100" />
           
            <label for="nama_barang">Keterangan :</label>
            <input type="text" name="ket" id="ket" />
           
            <input type="submit" value="Simpan">   
           
            <?php form_close()?>
       </div>
   <?php    } ?>
</body>
</html>