Membuat CRUD dengan Code Igniter

Setelah menginstall Codeigniter beberapa waktu yang lalu, kali ini kita akan belajar beberapa cara menggunakan dasar" Code Igniter, yaitu untuk fungsi Create, Read, Update, dan Delete atau CRUD. Mari kita langsung mulai!

Disini kita buat dulu file controller bernama crud.php untuk keempat fungsi tersebut, codenya akan terlihat seperti ini


<?php

class Crud extends CI_Controller{

  function __construct(){
    parent::__construct();
    $this->load->model('m_data');
    $this->load->helper('url');
  }

  function index(){
    $data['phonebook'] = $this->m_data->tampil_data()->result();
    $this->load->view('v_tampil', $data);
  }

  function tambah(){
    $this->load->view('v_input');
  }

  function tambah_aksi(){
    $nama_up = $this->input->post('nama_in');
    $alamat_up = $this->input->post('alamat_in');
    $email_up = $this->input->post('email_in');
    $hp_up = $this->input->post('hp_in');
    $kelas_up = $this->input->post('kelas_in');

    $data = array(
      'nama' => $nama_up,
      'alamat' => $alamat_up,
      'email' => $email_up,
      'hp' => $hp_up,
      'kelas' => $kelas_up
    );
    $this->m_data->input_data($data,'phonebook');
    redirect('http://localhost:56789/codeig/index.php/crud');
  }

  function hapus($id){
    $where = array('Id' => $id);
    $this->m_data->hapus_data($where,'phonebook');
    redirect('http://localhost:56789/codeig/index.php/crud');
  }

  function edit($id){
    $where = array('Id' => $id);
    $data['phonebook'] = $this->m_data->edit_data($where,'phonebook')->result();
    $this->load->view('v_edit',$data);
  }

  function update(){
       $id = $this->input->post('id');
       $nama = $this->input->post('nama_up');
       $alamat = $this->input->post('alamat_up');
       $email = $this->input->post('email_up');
     $hp = $this->input->post('hp_up');
     $kelas = $this->input->post('kelas_up');
        $data = array(
               'Nama' => $nama,
              'Alamat' => $alamat,
                'Email' => $email,
            'HP' => $hp,
            'Kelas' => $kelas
          );

            $where = array(
                  'Id' => $id
            );

             $this->m_data->update_data($where,$data,'phonebook');
            redirect('http://localhost:56789/codeig/index.php/crud');
  }
}

 ?>


Kemudian, baru kita buat file modelnya bernama m_data.php


<?php

class m_data extends CI_Model{

  function __construct(){
    parent::__construct();
    $this->load->database();
  }

  function tampil_data(){
    return $this->db->get('phonebook');
  }

  function input_data($data,$table){
    $this->db->insert($table,$data);
  }

  function hapus_data($where,$table){
    $this->db->where($where);
    $this->db->delete($table);
  }

  function edit_data($where,$table){
    return $this->db->get_where($table,$where);
  }

  function update_data($where,$data,$table){
    $this->db->where($where);
    $this->db->update($table,$data);
  }
}

?>
<?php

class m_data extends CI_Model{

  function __construct(){
    parent::__construct();
    $this->load->database();
  }

  function tampil_data(){
    return $this->db->get('phonebook');
  }

  function input_data($data,$table){
    $this->db->insert($table,$data);
  }

  function hapus_data($where,$table){
    $this->db->where($where);
    $this->db->delete($table);
  }

  function edit_data($where,$table){
    return $this->db->get_where($table,$where);
  }

  function update_data($where,$data,$table){
    $this->db->where($where);
    $this->db->update($table,$data);
  }
}

?>
<?php

class m_data extends CI_Model{

  function __construct(){
    parent::__construct();
    $this->load->database();
  }

  function tampil_data(){
    return $this->db->get('phonebook');
  }

  function input_data($data,$table){
    $this->db->insert($table,$data);
  }

  function hapus_data($where,$table){
    $this->db->where($where);
    $this->db->delete($table);
  }

  function edit_data($where,$table){
    return $this->db->get_where($table,$where);
  }

  function update_data($where,$data,$table){
    $this->db->where($where);
    $this->db->update($table,$data);
  }
}

?>

Lalu, baru kita buat file viewnya satu persatu. Pertama yaitu file v_tampil.php seperti dibawah


<!DOCTYPE html>
<html>
  <head>
    <title>| Coba Coba CRUD |</title>
  </head>
  <body>
    <center>
      <h1>| Coba Coba CRUD |</h1>
      <h3>View Data</h3>
    </center>
    <table style="margin:30px auto;" border="2">
      <tr>
        <th>No.</th>
        <th>Name</th>
        <th>Address</th>
        <th>Email</th>
        <th>HP</th>
        <th>Class</th>
        <th>Action</th>
      </tr>
      <?php
      $no = 1;
      foreach ($phonebook as $p) {
       ?>
       <tr>
         <td><?php echo $no++ ?></td>
         <td><?php echo $p->Nama ?></td>
         <td><?php echo $p->Alamat ?></td>
         <td><?php echo $p->Email ?></td>
         <td><?php echo $p->HP ?></td>
         <td><?php echo $p->Kelas ?></td>
         <td><?php echo anchor('http://localhost:56789/codeig/index.php/crud/edit/'.$p->Id, 'Edit'); ?>
           <?php echo anchor('http://localhost:56789/codeig/index.php/crud/hapus/'.$p->Id, 'Delete'); ?>
         </td>
       </tr>
       <?php }
        ?>
      </table>
      <center>
        <h4><?php echo anchor('http://localhost:56789/codeig/index.php/crud/tambah/','Insert Data'); ?></h4>
      </center>
    </body>
  </html>

Tampilannya akan seperti ini


Lalu kita buat file v_input.php untuk fungsi Create


<!DOCTYPE html>
<html>
<head>
    <title>| Coba Coba CRUD | </title>
</head>
<body>
    <center>
        <h1>| Coba Coba CRUD |</h1>
        <h3>Insert data</h3>
    </center>
    <form action="<?php echo 'http://localhost:56789/codeig/index.php/crud/tambah_aksi'; ?>" method="post">
        <table style="margin:20px auto;">
            <tr>
                <td>Name</td>
                <td><input type="text" name="nama_in"></td>
            </tr>
            <tr>
                <td>Address</td>
                <td><input type="text" name="alamat_in"></td>
            </tr>
            <tr>
                <td>Email</td>
                <td><input type="text" name="email_in"></td>
            </tr>
      <tr>
                <td>HP</td>
                <td><input type="text" name="hp_in"></td>
            </tr>
      <tr>
                <td>Class</td>
                <td><input type="text" name="kelas_in"></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Insert"></td>
            </tr>
        </table>
    </form>
</body>
</html>


Tampilannya akan menjadi seperti ini


Kemudian kita buat file v_edit.php untuk fungsi Update



<!DOCTYPE html>
<html>
  <head>
    <title>|Coba Coba CRUD|</title>
  </head>
  <body>
    <center>
      <h1>| Coba Coba CRUD |</h1>
      <h3> Update Data </h3>
    </center>
    <?php foreach($phonebook as $p) { ?>
      <form action="<?php echo 'http://localhost:56789/codeig/index.php/crud/update'; ?>" method="post">
        <table style="margin: 20px auto;">
          <tr>
            <td>Name</td>
            <td>
              <input type="hidden" name="id" value="<?php echo $p->Id ?>">
              <input type="text" name="nama_up" value="<?php echo $p->Nama ?>">
            </td>
          </tr>
          <tr>
            <td>Address</td>
            <td><input type="text" name="alamat_up" value="<?php echo $p->Alamat ?>"></td>
          </tr>
          <tr>
            <td>Email</td>
            <td><input type="text" name="email_up" value="<?php echo $p->Email ?>"></td>
          </tr>
          <tr>
            <td>HP</td>
            <td><input type="text" name="hp_up" value="<?php echo $p->HP ?>"></td>
          </tr>
          <tr>
            <td>Class</td>
            <td><input type="text" name="kelas_up" value="<?php echo $p->Kelas ?>"></td>
          </tr>
          <tr>
            <td></td>
            <td><input type="submit" value="Save"></td>
          </tr>
        </table>
      </form>
      <?php } ?>
    </body>
</html>


Tampilannya akan menjadi seperti berikut



Nah, untuk kali ini cukup segitu saja ilmu nya. Silahkan berbagi pada teman - teman anda. Sekian Terima kasih.  Byeee

Belajar dengan CodeIgniter

Langkah - Langkah Instalasi:

 1. Download CodeIgniter 3.0 di https://www.codeigniter.com/download
 2. Extract filenya
 3. Masukkan hasil extract ke dalam folder C://xampp/htdocs

Langkah Inisiasi Awal Code Igniter:

1. Ubah base_url pada file application/config/config.php di folder codeigniter anda menjadi lokasi folder anda
2. Buka file dalam application/config/database.php di folder codeigniter dan ubah menjadi di bawah
 $db['default'] = array(  
      'dsn'     => '',  
      'hostname' => 'localhost',          //BIARKAN BILA MEMANG ADA DI LOCALHOST  
      'username' => 'root',                         //MASUKKAN USER PADA PHPMYADMIN ANDA  
      'password' => '',                                   //MASUKKAN PASSWORD USER BILA ADA  
      'database' => 'ci_test',               //MASUKKAN NAMA DATABASE ANDA  
      'dbdriver' => 'mysqli',                    //MASUKKAN JENIS SOFTWARE DATABASE ANDA  
      'dbprefix' => '',  
      'pconnect' => FALSE,  
      'db_debug' => (ENVIRONMENT !== 'production'),  
      'cache_on' => FALSE,  
      'cachedir' => '',  
      'char_set' => 'utf8',  
      'dbcollat' => 'utf8_general_ci',  
      'swap_pre' => '',  
      'encrypt' => FALSE,  
      'compress' => FALSE,  
      'stricton' => FALSE,  
      'failover' => array(),  
      'save_queries' => TRUE  
 );  

Membuat Hello World

1. Buat file view pada folder view dengan nama hello_world.php



 <?php  
 defined('BASEPATH') OR exit('No direct script access allowed');  
 ?><!DOCTYPE html>  
 <html lang="en">  
 <head>  
      <meta charset="utf-8">  
      <title>Welcome codeig</title>  
      <style type="text/css">  
      ::selection { background-color: #E13300; color: white; }  
      ::-moz-selection { background-color: #E13300; color: white; }  
      body {  
           background-color: #fff;  
           margin: 40px;  
           font: 13px/20px normal Helvetica, Arial, sans-serif;  
           color: #4F5155;  
      }  
      a {  
           color: #003399;  
           background-color: transparent;  
           font-weight: normal;  
      }  
      h1 {  
           color: #444;  
           background-color: transparent;  
           border-bottom: 1px solid #D0D0D0;  
           font-size: 19px;  
           font-weight: normal;  
           margin: 0 0 14px 0;  
           padding: 14px 15px 10px 15px;  
      }  
      code {  
           font-family: Consolas, Monaco, Courier New, Courier, monospace;  
           font-size: 12px;  
           background-color: #f9f9f9;  
           border: 1px solid #D0D0D0;  
           color: #002166;  
           display: block;  
           margin: 14px 0 14px 0;  
           padding: 12px 10px 12px 10px;  
      }  
      #body {  
           margin: 0 15px 0 15px;  
      }  
      p.footer {  
           text-align: right;  
           font-size: 11px;  
           border-top: 1px solid #D0D0D0;  
           line-height: 32px;  
           padding: 0 10px 0 10px;  
           margin: 20px 0 0 0;  
      }  
      #container {  
           margin: 10px;  
           border: 1px solid #D0D0D0;  
           box-shadow: 0 0 8px #D0D0D0;  
      }  
      </style>  
 </head>  
 <body>  
 <div id="container">  
      <h1>Hello World</h1>  
   <p>Welcome to my very own Welcome Page!!</p>  
      <p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds. <?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p>  
 </div>  
 </body>    
 </html>  
2. Masukkan kode berikut pada file controller

      public function index()  
      {  
           $this->load->view('hello_world');  
      }  

3. Coba di web browser akan muncul tampilan begini

Membuat PhoneBook dari Database

1. Membuat file Controller pada folder controller dengan nama tampil.php

 <?php  
  class Tampil extends CI_Controller{  
   function __construct(){  
    parent::__construct();  
    $this->load->helper(array('url', 'form'));  
    $this->load->model('m_tampil');  
   }  
   function lihat(){  
    $data['data_buku'] = $this->m_tampil->m_lihat();  
    $this->load->view('v_lihat', $data);  
   }  
  }  
 ?>  

2. Buat file Model pada folder model dengan nama m_tampil.php dengan 'phonebook' sebagai nama tabel yang telah kita buat pada phpmyadmin


 <?php  
  class M_tampil extends CI_Model{  
   function __construct(){  
    parent::__construct();  
    $this->load->database();  
   }  
   function m_lihat(){  
    $lihat = $this->db->get('phonebook');  
    return $lihat->result();  
   }  
  }  
  ?>  

3. Buat file Viewnya pada folder view dengan nama v_lihat.php


 <!DOCTYPE html>  
 <html>  
  <head>  
   <title>PhoneBook</title>  
  </head>  
  <body>  
   <h1>My Phonebook</h1>  
   <table border="1">  
    <tr>  
     <th>Nama</th>  
     <th>Alamat</th>  
     <th>Email</th>  
     <th>HP</th>  
     <th>Kelas</th>  
    </tr>  
    <?php foreach ($data_buku as $lihat){  
     ?>  
    <tr>  
     <td><?php echo $lihat->Nama; ?></td>  
     <td><?php echo $lihat->Alamat; ?></td>  
     <td><?php echo $lihat->Email; ?></td>  
     <td><?php echo $lihat->HP; ?></td>  
     <td><?php echo $lihat->Kelas; ?></td>  
    </tr>  
    <?php  
    }  
    ?>  
   </table>  
  </body>  
  </html>  

4.Coba cek dan akan muncul seperti ini

Terima Kasih Wassalam.. :D

UTS P. Web

1. Berdasarkan tugas pembuatan web yang telah dilakukan

  • Web yang dikerjakan : stickmypride.com

  • Biaya Web :
          - Domain : Rp 115.000
          - Hosting : Rp 394.800 per tahun
  • Langkah - langkah yang telah dilakukan :
          a. Menyewa hosting sekaligus domain
          b. Menginstall CMS Wordpress pada Hosting
          c. Mengupload beberapa stiker
          d. Menambahkan beberapa kategori

  • Fitur - fitur pada Web
         a. Sign Up


       b. Log In

      c. Melihat stiker sesuai kategori




2. Buatlah program pendek untuk monitoring praktiku, sebagai berikut :
    a. Tabel User
   
    
    b. Tabel Praktikum
     
    
    c. Koneksi.php
         <?php
$dbserver = 'localhost';
$dbuser   = 'root';
$dbpass   = '';
$dbname   = 'baru';

$connect = new mysqli($dbserver, $dbuser, $dbpass, $dbname);

if ($connect->connect_error) {
    trigger_error('Database connection failed: '  . $conn->connect_error, E_USER_ERROR);
}
?>

   d. Login.php
      
    
<?php
    include 'koneksi.php';
?>
    
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>UTS Pweb</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

  </head>

<div class="col-sm-3" style="background-color:lightgreen; padding-top:10px">
    <form action="login_user.php" method="POST">
      <div class="contact-bottom">
        <label>Username : </label>
        <input name="nama_user" type="text" placeholder="Username" />
        <br>
        <label>Password : </label>
        <input name="password_user" type="password" placeholder="Password" />
        <div class="submit-btn">
          <input type="Submit" value="Login">
        </div>
      </div>
    </form>
</div>

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>


     d. Menampilkan data
<?php 
  include'koneksi.php';
$i=1;

echo '<table border="1" style="background-color:orange"><thead><td> No. </td><td>Kode</td><td>Mata Praktikum</td><td> Jumlah Praktikan</td></thead><tbody>';
echo '<h1>Data Mata Praktikum dan Jumlah Praktikan</h1>';
if($resultc = $connect->query("SELECT * FROM `praktikum`")){
                while($rowc = $resultc->fetch_assoc()) {
                      echo '<tr>';
                       echo '<td>', $i++,'</td>';
                       echo '<td>', $rowc['kode_mk'],'</td>';
                       echo '<td>', $rowc['nama_mk'],'</td>';
                       echo '<td>', $rowc['jumlah_praktikan'],'</td>';
                    echo '</tr>';
                    }
                $resultc->free();


}

echo '</tbody></table>';
echo '<div style="text-align:center"><button > Tambah Data MK</button></div>';
?>




Cara pake PHP di Web

Halo halo....
Akhirnya saya menggunakan blog ini lagi...  Maaf kalo lama nggak post lagi....
Kali ini saya akan menunjukkan cara cara dasar menggunakan server side programming pada web. Kali ini adalah bahasa PHP.
Berikut cara menyambungkan web dengan database

conn.php

<?php

  $host = 'localhost';
  $database = 'baru';
  $username = 'root';
  $password = '';

  $connection = mysqli_connect($host, $username, $password, $database);

  if(!$connection){
    echo "GAGAL";
  }

  echo "SUKSES";
  ?>

Sebelum mencoba kode diatas, kita mulai dulu service Apache dan MySQL pada XAMPP


Jika kita berhasil koneksi, maka akan muncul seperti ini

Inilah salah satu contoh kode php yang menggunakan array

<html>
 <head>
  <title>Pemrograman PHP menggunakan array</title>
 </head>
 <body>
  <?php
   $nama[]="Setiawan";
   $nama[]="Hari";

   echo $nama[1], $nama[0];
   echo "<br>";

   $kampus[1]="ITS";
   $kampus[2]="Surabaya";

   echo "Kampusku adalah $kampus[1] $kampus[2]";
  ?>
 </body>
</html>


Tampilannya akan menjadi berikut


Terima kasih atas perhatiannya.... Bye bye....