0% found this document useful (0 votes)
78 views

Code Igniter 3: Eko Halim Santoso

This document provides instructions for creating static and dynamic pages in CodeIgniter. It describes how to: 1. Create a Pages controller class to handle static pages like home and about. It loads common header and footer views. 2. Add routing configuration to route all requests through the Pages controller. 3. Create a News controller and News_model to retrieve and display news items from a database. The News_model contains methods to get all news or a single news item. 4. The News controller indexes all news and views a single news item, passing data to common views.

Uploaded by

Kamen Go
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Code Igniter 3: Eko Halim Santoso

This document provides instructions for creating static and dynamic pages in CodeIgniter. It describes how to: 1. Create a Pages controller class to handle static pages like home and about. It loads common header and footer views. 2. Add routing configuration to route all requests through the Pages controller. 3. Create a News controller and News_model to retrieve and display news items from a database. The News_model contains methods to get all news or a single news item. 4. The News controller indexes all news and views a single news item, passing data to common views.

Uploaded by

Kamen Go
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

CODE

IGNITER
3
EKO HALIM
SANTOSO
Kamenhalim.

DAFTAR ISI
HALAMAN STATIS........................................................................................................ 3
Halaman Statis........................................................................................................ 3
Menambahkan Logika ke Controller........................................................................4
Routing.................................................................................................................... 4

Halim Soft

Page 2

HALAMAN STATIS
Halaman Statis
Contoh pemanggilan
http://example.com/news/latest/10
menampilkan 10 item dari database.

Model Pattern.
http://example.com/[controller-class]/[controller-method]/[arguments]

buat file
application/controllers/Pages.php
<?php
class Pages extends CI_Controller {
public function view($page = 'home')
{
}
}

Skrip diatas digunakan unutk membuat class pages. Dengan menerima 1 argumen
bernama $page
buat header at application/views/templates/header.php
<html>
<head>

<title>CodeIgniter Tutorial</title>

</head>
<body>
<h1><?php echo $title; ?></h1>

Buat footer at application/views/templates/footer.php


</body>

<em>&copy; 2015</em>

</html>

Halim Soft

Page 3

Menambahkan Logika ke Controller


Template halaman statis akan berada di direktori
application/views/pages/
DI dalam directory tersebut, buat 2 file
home.php dan about.php
Lalu untuk meload kedua halaman tersebut..
public function view($page = 'home')
{
if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter

$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);

Routing
Buka file routing yang terletak di application/config/routes.php
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';

Halim Soft

Page 4

HALAMAN DINAMIS
Seting model
Query harus diletakan di model.
Buka application/models/ dan buat file file
News_model.php
<?php
class News_model extends CI_Model {

public function __construct()


{
$this->load->database();
}

Sebelumnya buat schema database.


News.sql
CREATE TABLE news (
id int(11) NOT NULL AUTO_INCREMENT,
title varchar(128) NOT NULL,
slug varchar(128) NOT NULL,
text text NOT NULL,
PRIMARY KEY (id),
KEY slug (slug)
);

Dapatkan semua post dari database dengan Query Builder yang terdapat di CI 3
Tambahkan di model
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}

Kita sudah mendapatkan 2 query.


Halim Soft

Page 5

Tampilkan News
Buat Controller baru di application/controllers/News.php.
<?php
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
$this->load->helper('url_helper');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
}

public function view($slug = NULL)


{
$data['news_item'] = $this->news_model->get_news($slug);
}

Lalu passing data ke view


public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}

Halim Soft

Page 6

You might also like