Codeigniter Pagination - Web Development and Web Design Codes

Latest

Wednesday, February 14, 2018

Codeigniter Pagination

Codeigniter Pagination

Codeigniter Pagination
Codeigniter Pagination
Hi Friends Today we are Going to learn how to create codeigniter pagination with database..
Follow the below example to learn how to create codeigniter pagination with database..

=>Database and Table:
  Database Name: test
  Table Name : company

-- Table structure for table `company`
CREATE TABLE IF NOT EXISTS `company` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  PRIMARY KEY(id)
) ENGINE=MyISAM AUTO_INCREMENT=60 DEFAULT CHARSET=latin1;

-- Dumping data for table `company`
INSERT INTO `company` (`id`, `name`) VALUES
(NULL, 'Nokia Inc'),
(NULL, 'Samsung Inc'),
(NULL, 'Sony Inc'),
(NULL, 'DigiCraft'),
(NULL, 'Techsoft'),
(NULL, 'Newsoft'),
(NULL, 'MonoSoft'),
(NULL, 'Digisoft'),
(NULL, 'Noki Android'),
(NULL, 'Samsung Shipyard'),
(NULL, 'Bajaj Motor'),
(NULL, 'Tata Nano'),
(NULL, 'HAL Inc'),
(NULL, 'Airtel Inc'),
(NULL, 'Baidu Inc'),
(NULL, 'Holdings Inc'),
(NULL, 'Nokia Inc'),
(NULL, 'Samsung Inc'),
(NULL, 'ASUS Inc'),
(NULL, 'IBM'),
(NULL, 'Alibaba Inc'),
(NULL, 'Google Inc'),
(NULL, 'FileMaker Inc'),
(NULL, 'Facebook Inc'),
(NULL, 'EMC Corporation'),
(NULL, 'Ebay Inc'),
(NULL, 'Delphi Inc'),
(NULL, 'Dell Inc'),
(NULL, 'Bank of India'),
(NULL, 'Bank Of America'),
(NULL, 'Bally Technology Inc'),
(NULL, 'Bharathi Airtel'),
(NULL, 'Alphabet Inc');

Configuration:
  autoload.php
application/config/autoload.php
$autoload['helper'] = array('url');

  config.php
application/config/config.php
$config['base_url'] = 'http://localhost/pagination/';

 Database.php
$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
 'dsn' => '',
 'hostname' => 'localhost',
 'username' => 'root',
 'password' => '',
 'database' => 'test',
 'dbdriver' => 'mysqli',
 'dbprefix' => '',
 'pconnect' => FALSE,
 'db_debug' => (ENVIRONMENT !== 'production'),
 'cache_on' => FALSE,
 'cachedir' => '',
 'char_set' => 'utf8',
 'dbcollat' => 'utf8_general_ci',
 'swap_pre' => '',
 'encrypt' => FALSE,
 'compress' => FALSE,
 'stricton' => FALSE,
 'failover' => array(),
 'save_queries' => TRUE
);

Codes :
Controllers: blog.php


<?php
class Blog extends CI_Controller{
 public function index(){
  //Loading Pagination Library and Model
  $this->load->library('pagination');
  $this->load->Model('Pagination_model');
  //Count total rows
  $rows=$this->Pagination_model->record_count();
  $config['base_url']=base_url().'index.php/blog/index';
  $config['total_rows']=$rows;
  $config['per_page']=2;
  $config['prev_link']='Previous';
        $config['next_link']='Next';
  $config['display_pages'] =TRUE;
    $config['full_tag_open'] = '<div class="pagination">';
        $config['full_tag_close'] = '</div>';
        $config['cur_tag_open'] = '<a class="active" href="">';
        $config['cur_tag_close'] = '</a>';
  
  $this->pagination->initialize($config);
  $perpage=2;
  if ($this->uri->segment(3)) {
            $segment = ($this->uri->segment(3));
        } else {
            $segment=0;
        }
   
  $data['user'] = $this->Pagination_model->user($perpage,$segment);
  $this->load->view('index',$data);
 }
}

Models : pagination_model.php
<?php
class Pagination_model extends CI_Model{
 public function __construct(){
  parent::__construct();
  $this->load->database();
 }
 //Count all records of table user_error
 public function record_count(){
  return $this->db->count_all('company');
 }
 public function user($perpage,$segment){
        $query = $this->db->limit($perpage, $segment)->order_by('id','desc')->get('company');
        return $query->result_array();
 }
}

?>

Views: index.php

<style>
.pagination {
    display: inline-block;
}

.pagination a {
    color: black;
    float: left;
    padding: 4px 8px;
    text-decoration: none;
    border: 1px solid #ddd;
}

.pagination a.active {
    background-color: #4CAF50;
    color: white;
    border: 1px solid #4CAF50;
}

.pagination a:hover:not(.active) {background-color: #ddd;}

.pagination a:first-child {
    border-top-left-radius: 5px;
    border-bottom-left-radius: 5px;
}

.pagination a:last-child {
    border-top-right-radius: 5px;
    border-bottom-right-radius: 5px;
}

</style>
<h3 style="color:red">Codeigniter Pagination</h3>
<table border="1" cellspacing="0" width="400">
    <tr>
     <th>Serial</th>
     <th>ID</th>
  <th>Name</th>
 </tr>
<?php $i = $this->uri->segment(3); foreach ($user as $row){ $i++; ?>

     <tr>
      <td><?php echo $i;?></td>
      <td><?php echo $row['id'];?></td>
   <td><b><?php echo $row['name'];?></b></td>
  </tr>
 <?php  } ?>
</table>
<?php

echo $this->pagination->create_links();

?>

That's it guys how to create codeigniter pagination with database..Keep visiting for more codes...
Thank You..

No comments:

Post a Comment

Thank You for Your Comment

Note: Only a member of this blog may post a comment.