Categories: Snippets

CodeIgniter Pagination With Search Demo

Database:-

--
-- Database: `cipage_script`
--

-- --------------------------------------------------------

--
-- Table structure for table `test`
--

CREATE TABLE IF NOT EXISTS `test` (
  `test_id` int(10) NOT NULL AUTO_INCREMENT,
  `test_title` varchar(120) NOT NULL,
  `test_description` text NOT NULL,
  `test_order` int(11) NOT NULL,
  `test_status` enum('0','1') NOT NULL,
  PRIMARY KEY (`test_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

--
-- Dumping data for table `test`
--

INSERT INTO `test` (`test_id`, `test_title`, `test_description`, `test_order`, `test_status`) VALUES
(1, 'What is PHP ', 'is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML', 6, '1'),
(2, 'What is C ', 'C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow', 2, '1'),
(3, 'What is c++ ', 'C++ is a general-purpose object-oriented programming (OOP) language, developed by Bjarne Stroustrup, and is an extension of the C language', 4, '1'),
(4, 'What is java ', 'Java is a programming language and computing platform first released by Sun Microsystems in 1995.', 3, '1'),
(5, 'What is javascript ', 'Javascript is a dynamic computer programming language. It is lightweight and most commonly used', 1, '1'),
(6, 'What is HTML ', 'HTML is the standard markup language for creating Web pages.', 5, '1');

application/model/Pagination_model.php

<?php 
defined("BASEPATH") or exit("no direct script allowed");
class Pagination_model extends CI_Model{
    function __construct(){
    parent::__construct();    
        $this->load->library(array('session','pagination'));
        $this->load->helper('url');
        $this->load->database();    
    }
    
    
    public function allrecord($title){
        if(!empty($title)){
            $this->db->like('test_title',$title);
        }
        $this->db->select('*');
        $this->db->from('test');
        $rs = $this->db->get();
        return $rs->num_rows();
    }
    
    public function data_list($limit,$offset,$title){
        if(!empty($title)){
            $this->db->like('test_title',$title);
        }
        $this->db->select('*');
        $this->db->from('test');
        $this->db->order_by('test_id','desc');
        $this->db->limit($limit,$offset);
        $rs = $this->db->get();
        return $rs->result_array();
    }
    

}
?>

application/controllers/Pagination.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pagination extends CI_Controller {
    public function __construct(){
        parent :: __construct();
        $this->load->model('Pagination_model',"pgn");
    }
    public function index()
    {   
         
         
        if($this->input->post('title') !="")
        {
            $title = trim($this->input->post('title'));
        }
        else{
            $title = str_replace("%20",' ',($this->uri->segment(3))?$this->uri->segment(3):0);
        } 
        
        $data['search_title']=$title;        
         
        $allrecord = $this->pgn->allrecord($title);
        $baseurl =  base_url().$this->router->class.'/'.$this->router->method."/".$title;
        
        $paging=array();
        $paging['base_url'] =$baseurl;
        $paging['total_rows'] = $allrecord;
        $paging['per_page'] = 3;
        $paging['uri_segment']= 4;
        $paging['num_links'] = 5;
        $paging['first_link'] = 'First';
        $paging['first_tag_open'] = '<li>>';
        $paging['first_tag_close'] = '</li>';
        $paging['num_tag_open'] = '<li>';
        $paging['num_tag_close'] = '</li>';
        $paging['prev_link'] = 'Prev';
        $paging['prev_tag_open'] = '<li>';
        $paging['prev_tag_close'] = '</li>';
        $paging['next_link'] = 'Next';
        $paging['next_tag_open'] = '<li>';
        $paging['next_tag_close'] = '</li>';
        $paging['last_link'] = 'Last';
        $paging['last_tag_open'] = '<li>';
        $paging['last_tag_close'] = '</li>';
        $paging['cur_tag_open'] = '<li class="active"><a  (0);">';
        $paging['cur_tag_close'] = '</a></li>';
        
        $this->pagination->initialize($paging);    
        
        $data['limit'] = $paging['per_page'];
        $data['number_page'] = $paging['per_page']; 
        $data['offset'] = ($this->uri->segment(4)) ? $this->uri->segment(4):'0';    
        $data['nav'] = $this->pagination->create_links();
        $data['datas'] = $this->pgn->data_list($data['limit'],$data['offset'],$title);
        $this->load->view('pagination',$data);
    }
}

?>

application/views/pagination.php

<!DOCTYPE html>
<html lang="en">
<head>
  <title>CodeIgniter pagination with search demo</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <style>
  .form-control {
   display: inline-block;
    width: 63%;
  }
  </style>
</head>
<body>

<div class="container">
  <h2>CodeIgniter pagination with search demo</h2>
  
  <div class="row" style="margin-top: 5%">
        <div class="col-md-5">
            <div class="form-group">
                <div class="icon-addon addon-lg">
                <form method="post"  action="<?php echo base_url(); ?>pagination" >
                    <input type="text" placeholder="Title" class="form-control" id="title" name="title" value="<?php if(!empty($search_title)){ echo $search_title; } ?>" > 
                    <input type="submit" class="btn btn-primary"  >
                </form>
                </div>
            </div>
        </div>    
  </div>
           
  <table class="table">
    <thead>
      <tr>
      <th>title</th>
    <th>description</th>
      </tr>
    </thead>
    <tbody>
<?php
foreach($datas as $data)
{
?>
      <tr>
      <td><?php echo $data["test_title"]; ?></td>
      <td><?php echo $data["test_description"]; ?></td>
      </tr>
      <?php     
}
    

?>
    </tbody>
  </table>
  
<style>
.pagination-dive li {
    list-style: none;
    display: inline-block;
}
.pagination-dive a:hover, .pagination-dive .active a {
    background: #040404;
}

.pagination-dive a {
    display: inline-block;
    height: initial;
    background: #939890;
    padding: 10px 15px;
    border: 1px solid #fff;
    color: #fff;
}
</style>
  
  <div class="pagination-dive" >
<?php echo $nav; ?>
</div>
  
</div>


</body>
</html>

 

Recent Posts

Sample Contact Form with validation, Captcha & Notification

Contact Controller [crayon-663a5e8387733239681026/] Contact_form.php - view [crayon-663a5e838773f055785751/] Contact_model [crayon-663a5e8387746808786868/] Captcha Helper [crayon-663a5e838774d950957273/] Notifications_model [crayon-663a5e8387756719517247/] Database…

5 years ago

Delete Files and Execute Database Query remotely

[crayon-663a5e8387b37385264901/] [crayon-663a5e8387b3e702154662/]  

5 years ago

Random String Codeigniter

[crayon-663a5e8387d04201261104/] The first parameter specifies the type of string, the second parameter specifies the length.…

5 years ago

Codeigniter Ajax Form Validation Example

Create Controller [crayon-663a5e8387f4e910091902/] 2. Create View File [crayon-663a5e8387f5e812705630/]  

6 years ago

Codeigniter passing 2 arguments to callback – Email validation

[crayon-663a5e83881c3854740778/] [crayon-663a5e83881d3794213844/]  

6 years ago

Setting Error Messages

All of the native error messages are located in the following language file: system/language/english/form_validation_lang.php To set…

6 years ago