Kategori
PHP

Create a Pagination in CodeIgniter

Case Study : Create a pagination in CodeIgniter
Requirements : Webserver Package, already installed. CodeIgniter bundle.

If you aren’t familiar with the term “pagination”, pagination refers to the link which is for navigation from one page to another.just like the picture below,

Pagination will be quite easy in CI, this tutorial will give the example how to make a pagination in CodeIgniter’s application.

Okay then, prepare for a cup of tea 🙂

Step 1 : CI Configuration

  1. Open the config.php file located in the system-application-config
  2. Change base_url, adjust according where your ci folder are. FOr example: your ci folder located in www/ci folder
    so change the line $config['base_url']="http://example.com/";
    with
    $config['base_url']="http://localhost/ci";
  3. Setting the database. Open file database.php located in the same folder as config.php. Change hostname, username, password, and the database name according to your mysql setting. For example :
    $db['default']['hostname'] = "localhost";
    $db['default']['username'] = "root";
    $db['default']['password'] = "";
    $db['default']['database'] = "db_tutorial";

Step 2 : Prepare the Database

  1. Make a database named db_tutorial.
  2. Prepare a table named tb_book (for table’s structure, see picture below)
  3. Insert some sample data, for example insert some sample data just like the picture below
  4. Ok, we’re done with database

Step 3 : Create the Controller

  1. Type the following script
    <?php
    class Buku_con extends Controller {
    public function Buku_con()
    {
    parent::__construct();
    $this->load->model('buku_model');
    $this->load->library('pagination'); //call pagination library
    }
    function getBuku() {
    //count the total rows of tb_book
    $this->db->select('*');
    $this->db->from('tb_book');
    $getData = $this->db->get('');
    $a = $getData->num_rows();
    $config['base_url'] = base_url().'index.php/Buku_con/getBuku/'; //set the base url for pagination
    $config['total_rows'] = $a; //total rows
    $config['per_page'] = '2'; //the number of per page for pagination
    $config['uri_segment'] = 3; //see from base_url. 3 for this case
    $config['full_tag_open'] = '<p>';
    $config['full_tag_close'] = '</p>';
    $this->pagination->initialize($config); //initialize pagination
    $data['title'] = 'menampilkan isi buku';
    $data['detail'] = $this->buku_model->getBuku($config['per_page'],$this->uri->segment(3));
    $this->load->view('buku_view', $data);
    }
    }
    ?>
  2. Save with the name buku_con.php in the system-application-controllers folder
  3. Explanation : function getBuku() will get the content of tb_book via model. In this controller, pagination initialized and coordinated like in the script above (see the script’s comment).

Step 4 : Create the Model

  1. Type the following script,
    <?php
    class Buku_model extends Model {
    function Buku_model()
    {
    parent::Model();
    }
    function getBuku($perPage,$uri) { //to get all data in tb_book
    $this->db->select('*');
    $this->db->from('tb_book');
    $this->db->order_by('id','DESC');
    $getData = $this->db->get('', $perPage, $uri);
    if($getData->num_rows() > 0)
    return $getData->result_array();
    else
    return null;
    }
    }
    ?>
  2. Save with the name buku_model.php in the system-application-models folder

Step 5 : Create the View

  1. Type the following script,

    <h4>Book Data</h4>
    <?php if(count($detail) > 0) { ?>
    <table border="1">
    <tr>
    <th>ID</th>
    <th>Title</th>
    <th>Author</th>
    </tr>
    <?php
    foreach($detail as $rows) {
    echo "<tr>";
    echo "
    <td>". $rows['id']."</td>
    <td>". $rows['title'] ."</td>
    <td>". $rows['author'] ."</td>
    "; } ?>
    </table>
    <?php } ?>
    <div> <?php echo $this->pagination->create_links(); ?> </div>
  2. Save with the name buku_view.php in the system-application-views folder
  3. Explanation: $detail taken from controller, this variabel filled with the content of tb_book (the model’s result), and then the data showed in the array form.

Step 6 : Testing Code

  1. Go to http://localhost/namaFolderCIKamu/buku_con/getBuku
  2. You will see something like this,
  3. Click in the pagination link in to page 2, and you will see,


So easy, right? Just try it, and happy coding 🙂

Kategori
PHP

Membuat Pagination dalam CodeIgniter

Studi Kasus : Membuat Pagination dalam CodeIgniter
Kebutuhan : Webserver Package, already installed. CodeIgniter Bundle.
Jika anda tidak familiar dengan istilah “pagination”, pagination merujuk pada link yang berguna untuk navigasi dari page ke page, seperti gambar dibawah ini,

Pagination dalam CI sangat mudah digunakan, tutorial ini akan memberikan contoh membuat pagination dalam aplikasi CI.
Okay then, prepare for a cup of tea 🙂


Step 1 : Konfigurasi CI

  1. Buka file config.php yang berada dalam folder system-application-config-config.php
  2. Ubah base url, sesuaikan dengan lokasi dimana folder CI-mu berada (tempat ekstrak-an tadi). Contoh : Folder CI anda berada dalam folder www/ci
    maka ubah baris $config['base_url']="http://example.com/";
    dengan
    $config['base_url'] = "http://localhost/ci/";
  3. Setting database. Buka file database.php yang berada dalam folder yang sama dengan config.php. Ubah hostname, username, password, dan nama database. Sesuaikan dengan pengaturan mysql anda. Contoh :
    $db['default']['hostname'] = "localhost";
    $db['default']['username'] = "root";
    $db['default']['password'] = "";
    $db['default']['database'] = "db_tutorial";

Step 2 : Siapkan Database

  1. Buat database dengan nama db_tutorial (via phpmyadmin)
  2. Siapkan tabel dengan nama tb_book (untuk strukturnya, lihat gambar dibawah ini)
  3. Insertkan beberapa sample data, sebagai contoh insertkan data seperti tabel dibawah ini.
  4. Ok, we’re done with database !

Step 3 : Membuat Controller

  1. Ketikkan script berikut,
    <?php
    class Buku_con extends Controller {
    public function Buku_con()
    {
    parent::__construct();
    $this->load->model('buku_model');
    $this->load->library('pagination'); //call pagination library
    }
    function getBuku() {
    //count the total rows of tb_book
    $this->db->select('*');
    $this->db->from('tb_book');
    $getData = $this->db->get('');
    $a = $getData->num_rows();
    $config['base_url'] = base_url().'index.php/Buku_con/getBuku/'; //set the base url for pagination
    $config['total_rows'] = $a; //total rows
    $config['per_page'] = '2'; //the number of per page for pagination
    $config['uri_segment'] = 3; //see from base_url. 3 for this case
    $config['full_tag_open'] = '<p>';
    $config['full_tag_close'] = '</p>';
    $this->pagination->initialize($config); //initialize pagination
    $data['title'] = 'menampilkan isi buku';
    $data['detail'] = $this->buku_model->getBuku($config['per_page'],$this->uri->segment(3));
    $this->load->view('buku_view', $data);
    }
    }
    ?>
  2. Simpan dengan nama buku_con dalam folder system-application-controllers
  3. Penjelasan : function getBuku() akan mengambil isi dari tb_book via model. Dalam controller ini, pagination diinisialisasi dan diatur settingannya seperti pada script diatas (lihat komentar pada script untuk keterangannya).

Step 4 : Membuat Model

  1. Ketikkan script berikut,
    <?php
    class Buku_model extends Model {
    function Buku_model()
    {
    parent::Model();
    }
    function getBuku($perPage,$uri) { //to get all data in tb_book
    $this->db->select('*');
    $this->db->from('tb_book');
    $this->db->order_by('id','DESC');
    $getData = $this->db->get('', $perPage, $uri);
    if($getData->num_rows() > 0)
    return $getData->result_array();
    else
    return null;
    }
    }
    ?>
  2. Simpan dalam folder system-application-models, dengan nama buku_model.php

Step 5 : Membuat View

  1. Ketikkan script berikut,

    <h4>Book Data</h4>
    <?php if(count($detail) > 0) { ?>
    <table border="1">
    <tr>
    <th>ID</th>
    <th>Title</th>
    <th>Author</th>
    </tr>
    <?php
    foreach($detail as $rows) {
    echo "<tr>";
    echo "
    <td>". $rows['id']."</td>
    <td>". $rows['title'] ."</td>
    <td>". $rows['author'] ."</td>
    "; } ?>
    </table>
    <?php } ?>
    <div> <?php echo $this->pagination->create_links(); ?> </div>
  2. Simpan dalam folder system-application-views, dengan nama buku_view.php
  3. Penjelasan : $detail didapatkan dari controller, variabel ini berisi data dari tb_book (hasil dari model). Kemudian data ditampilkan dalam bentuk arrays.

Step 6 : Testing Code

  1. Buka browser, pergi ke http://localhost/namaFolderCIKamu/buku_con/getBuku
  2. Anda akan melihat tampilan seperti berikut,
  3. Klik pada link pagination ke halaman 2, maka anda akan melihat tampilan seperti dibawah


So easy right?
😀 Silahkan mencoba, and… happy coding 🙂