Silahkan Melihat Tutorial di website kami dengan nyaman ENJOY YOUR LIFE ☕

How To create Pagination in codeigniter

How To create Pagination in codeigniter



Here I am Create Pagination Example in Codeigniter.

The Model


create model Model/news_model.php

provide a count of all the records in the News table, and retrieve a list of news from the table

The record_count() method returns the number of records and is needed because one of the options in the $config array for the pagination library is $config["total_rows"]

The get_news() method retrieves a list of all the records from the News table.
there are two arguments $start, $limit.
The arguments will be set in the controller.



<?php class News_model extends CI_model{
   
    public function __contruct(){
     
       $this->load->database();
    }
   
    public function record_count() {
       return $this->db->count_all("news");
    }
   
   
    public function get_news($limit, $start){
     
     
     
     
     
    
          $this->db->limit($limit, $start);
     
           $query = $this->db->get("news");
     
               if ($query->num_rows() > 0) {
                   foreach ($query->result() as $row) {
                       $data[] = $row;
                   }
                   return $data;
              /*$query = $this->db->get('news');
              return $query->result_array();*/
          }
   
   
  
   
    }
}


The Controller


create controller controllers/News.php

Here We can Load model and pagination library and also added helper of
url.

check method in index.


<?php class News extends CI_Controller {
   
   
    public function  __construct(){
     
      parent::__construct();
      $this->load->model('news_model');
        $this->load->helper("url");
         $this->load->library("pagination");
    }
   
    public function index(){
       $this->load->helper("url");
     
$config = array();      
$config['base_url'] = base_url().'index.php/news/';
$config['total_rows'] = $this->news_model->record_count();
$config['per_page'] = 2;
$config["uri_segment"] = 2;

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


$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$data["results"] = $this->news_model->get_news($config["per_page"], $page);
 $data["links"] = $this->pagination->create_links();
    
    
       //$data['news'] = $this->news_model->get_news();
     
       $data['title'] = 'News archive';

   $this->load->view('templates/header', $data);
   $this->load->view('news/index', $data);
   $this->load->view('templates/footer');
    }

}?>

The View

create file  views/index.php
<?php
//echo "<pre>";
//print_r($results);

foreach($results as $result){?>
       <h2><?php  echo $result->title; ?></h2>
       <div class="main">
       <?php  echo $result->text; ?>
     
       <p><a href="view/<?php   echo $result->slug; ?>">View article</a></p>
    </div>
<?php }
echo $links; ?>

in last config/routes.php changes.

$route['news/(:num)'] = 'news/index';

set this in your routes.php









0 komentar:

Post a Comment

How To create Pagination in codeigniter