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']="";
  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
    class Buku_con extends Controller {
    public function Buku_con()
    $this->load->library('pagination'); //call pagination library
    function getBuku() {
    //count the total rows of 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,
    class Buku_model extends Model {
    function Buku_model()
    function getBuku($perPage,$uri) { //to get all data in tb_book
    $getData = $this->db->get('', $perPage, $uri);
    if($getData->num_rows() > 0)
    return $getData->result_array();
    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">
    foreach($detail as $rows) {
    echo "<tr>";
    echo "
    <td>". $rows['id']."</td>
    <td>". $rows['title'] ."</td>
    <td>". $rows['author'] ."</td>
    "; } ?>
    <?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 :)

Hal yang juga menarik:

Hak Cipta

Semua skrip dan teknik dalam artikel di boleh digunakan sebagaimana kehendakmu tanpa perlu mencantumkan sumber. Kamu tidak boleh mengkopi seluruh artikel, dalam Bahasa Indonesia ataupun diterjemahkan ke dalam bahasa lain.

Related Articles:

Artikel terkait:


About the author

obviously, a girl. with sweet smile, off course. turning her 21 years trapped on Informatics departement and just started to having some crush with it lately. she uses wordpress. she loves php. she lo


Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

x( X-( B-) ;-) :wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :D :?: :-| :-x :-o :-P :-D :-? :-)) :-) :-( :-& :) :( :!: 8-O 8-) 8) (Русский) (yahoo) (worship) (woot) (wave) (unsure) (tongue) (thinking) (tears) (taser) (smileydance) (sleeping) (sick) (scenic) (rofl) (rock) (party) (panic) (okok) (nottalking) (ninja) (music) (muscle) (muhaha) (money) (mmm) (lonely) (lol) (lmao) (idiot) (hungry) (highfive) (heart_beat) (heart) (headspin) (hassle) (haha) (gym) (griltongue) (goodluck) (girlkiss) (funkydance) (fish_hit) (eyeroll) (evilsmirk) (evil_grin) (drinking) (doh) (devil) (dance) (cry) (cozy) (coffee) (brokenheart) (bringit) (blush) (bigeyes) (beer) (banana_rock) (banana_ninja) (banana_cool) (applause) (annoyed) (angry) (K) (: