Kamis, 14 April 2016

Starting CodeIgniter

1. INSTALLATION
 a. Download CodeIgniter 3.0

 b. Ekstrak file di folder htdocs di XAMPP
c. Ubah base_url pada file application/config/config.php di folder codeigniter anda menjadi lokasi folder anda
d. 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' => 'cobaci',               //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  
 );  

 Selesai! CodeIgniter Anda sekarang dapat digunakan

2. MAKING A "HELLO WORLD"

Kali ini kita akan mencoba menampilkan sebuah pesan hello world menggunakan code igniter. Langkah-langkahnya sebagai berikut
a. Buatlah file hello_world.php di folder views
b. Isi sebagai berikut :

<?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>  

c. Kemudian pada file welcome.php di folder controllers, isi sebagai berikut :

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

d. Hasilnya dapat dibuka melalui browser

 3. MAKE A "PHONEBOOK DATABASE"
Kali ini kita akan menampilkan suatu tabel yang berisi list nomor telepon yang diambil dari database

a. Buatlah file tampil.php di folder Controllers, isikan sebagai berikut

 <?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);  
   }  
  }  
 ?>  

b. Buatlah file m_tampil.php di folder Models, isikan sebagai berikut

<?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();  
   }  
  }  
  ?>  

c. Buatlah file v_lihat.php di folder Views


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

d. Buka melalui browser, maka akan ditampilkan sebagai berikut :

Tidak ada komentar:

Posting Komentar