kali ini saya akan membagikan sedikit tutorial menggunakan Codeigniter dalam Pemrograman web
langsung saja
- pertama-tama install terlebih dahulu CodeIgniter. kalau saya CodeIgniter-3.0.4.
kemudian ekstrak file dan simpan pada direktori xampp/htdocs.
- buka file config.php di direktori xampp/htdocs/CI/application/config
- edit bagian base_url
- lalu buka file database.php pada direktori xampp/htdocs/CI/application/config lalu edit menggunakan Sublme Text
- ubah username dan password serta database seuai dengan yang telah kita buat tadi
database.php
Membuat Hello Word
membuat tampilan pembuka, HELLO WORD
View
- buat file baru pada direktori xampp/htdocs/CI/application/view- beni nama Hello_word.php dan coba ketikkan kodenya seperti dibawah ini
Hello_word.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: Pink; }
::-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: grey;
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 WORD**</h1>
<>
<p>Selamat Datang</p>
<p class="footer">punya Elva<strong>Teknik Informatika ITS</strong> Surabaya. <?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p>
</div>
</body>
</html>
Controller
-untuk mencoba tampilan sederhana pada html, tambahkan kode berikut pada file welcome.php di direktori xampp/htdocs/CI/application/controller
- tambahkan kode dibawah ini pada file tersebut
- untuk melihat hasilnya pada ketikkkan localhost/CI/
maka akan muncul
Membuat Tampilan PhoneBook
- jangan lupa nyalakan xampp terlebih dahulu- kemudian buat database baru pada PhpMyAdmin dengan nama pweb.
- lalu buat tabel baru dengan nama Phonebook
- Tambahkan atribut Nama, Alamat, No.telp, Email, Kelas
<?php
class Blog extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
}
?>
- lalu kemudian tambah kode baru dalam file yang sama untuk membuat tabel pada CI- coba ketikkan code berikut
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class tes extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see https://codeigniter.com/user_guide/general/urls.html
*/
function __construct(){
parent::__construct();
$this->load->helper(array('url','form'));
$this->load->model('ctrl');
}
function ndelok()
{
$data['pb'] = $this->ctrl->index();
$this->load->view('tesview', $data);
}
}
?>
- untuk mengambil database pweb yang kitabuat tadi kita harus masuk pada direktori xampp/htdocs/CI/application/models lalu buat file baru misal take.php
-ketikkan kode berikut
preview.php
<?php
/**
*
*/
class table_model extends CI_Model{
function __construct()
{
parent::__construct();
$this->load->database();
}
function index(){
$ndelok = $this->db->get('mhs');
return $ndelok->result();
}
}
?>
- untuk mengambil database pweb yang kitabuat tadi kita harus masuk pada direktori xampp/htdocs/CI/application/view lalu buat file baru misal preview.php
-ketikkan kode berikut
- kemudia untuk menampilkan data tabel html, buka localhost pada browser dengan syntax localhost/CodeIgniter-3.0.4/index.php/nama_controller/nama_view.
ketikkan localhost/CodeIgniter-3.0.4/index.php/ctrl/preview
maka akan muncul tampilan di prowser