Create Pagination using Codeigniter

Pagination.php        Controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Pagination extends CI_Controller {

function __construct()
{
parent:: __construct();
$this->load->model('pagination_model');
$this->load->library('pagination');
}

public function index()
{
$config=[
        'base_url' => base_url('Pagination/index'),
        'per_page' => 3,
        'total_rows' => $this->pagination_model->num_rows(),
        'full_tag_open' => "<ul class='pagination'>",
        'first_link' => false,
        'last_link' => false,
        'full_tag_close'=> "</ul>",
        'next_tag_open' => "<li>",
        'next_tag_close'=> "</li>",
        'prev_tag_open' => "<li>",
        'prev_tag_close'=> "</li>",
        'num_tag_open' => "<li>",
        'num_tag_close' => "<li>",
        'cur_tag_open' => "<li class='active'><a>",
        'cur_tag_close' => "</a></li>"
];

  $this->pagination->initialize($config);

  $data['dataList']=$this->pagination_model->dataList($config['per_page'],$this->uri->segment(3));
$this->load->view('include/header');
$this->load->view('pagination/pagination',$data);
$this->load->view('include/footer');
}
}

Pagination_model.php           Model

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Pagination_model extends CI_Model {

public function num_rows()
  {
    $query=$this->db->select()
            ->from('tbl_form')
            ->get();
           return $query->num_rows();
  }

public function dataList($limit,$offset)
  {
   $query=$this->db->select('')
            ->from('tbl_form')
            ->limit($limit,$offset)
            ->get();
           return $query->result();
  }
}

pagination.php            View

<div class="container-fluid text-center">    
  <div class="row content">
    <div class="col-sm-12 text-left"> 
      <table class="table table-striped table-bordered nowrap dataTable dt-responsive">
        <thead>
            <tr>
                <th>S.no</th>
                <th>Text</th>
                <th>Dropdown</th>
            </tr>
        </thead>
        <tbody>
        <?php if(count($dataList)): ?> 
        <?php foreach ($dataList as $key => $art): ?>
        <tr>
        <td><?php  echo $art->id; ?></td>
        <td><?php  echo $art->text; ?></td>
        <td><?php  echo $art->dropdown; ?></td>
        </tr>
        <?php endforeach; ?>
        <?php else: ?>
        <tr>
        <td colspan="3">Not data available</td>
        </tr>
        <?php endif; ?>
        </tbody>
    </table>
    <?php  echo $this->pagination->create_links();   ?>
    </div>
  </div>
</div>

Sql Script

CREATE TABLE IF NOT EXISTS `tbl_form` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `text` varchar(100) NOT NULL,
  `dropdown` varchar(10) NOT NULL,
  `radio` varchar(10) NOT NULL,
  `checkbox` varchar(100) NOT NULL,
  `singleFile` varchar(100) NOT NULL,
  `multiplefile` varchar(255) NOT NULL,
  `status` tinyint(1) NOT NULL,
  `cdate` datetime(6) NOT NULL,
  `udate` datetime(6) NOT NULL,
  PRIMARY KEY (`id`)
)

Comments

Popular posts from this blog

API

Encryption and Decryption By PHP

Seeder