Tutorial membuat pagination di codeigniter dengan menggunakan
Jamie Rumbelow’s MY_Model.
kode di Controller :
class
Posts
extends
CI_Controller
{
public
function
__construct()
{
parent::__construct();
$this
->load->helper(
'html'
);
$this
->load->helper(
'url'
);
$this
->load->model(
'post_model'
,
'post'
);
$this
->load->library(
"pagination"
);
$this
->load->database();
$limit
= 2;
$offset
=
$this
->uri->segment(3);
$config
[
'total_rows'
] =
$this
->post->count_all();
$config
[
'base_url'
] = site_url(
'/posts/index/'
);
$config
[
'per_page'
] =
$limit
;
$data
[
'posts'
] =
$this
->post->limit(
$limit
,
$offset
)->post->get_all();
$this
->pagination->initialize(
$config
);
$data
[
"pagination_links"
] =
$this
->pagination->create_links();
$this
->load->view(
'posts'
,
$data
);
}
}
kode di view:
<?php
foreach ($posts as $key => $value)
{
echo '<h1>'. $value->judul . '</h1>';
echo '<p>'. $value->isi_berita . '</p>';
}
echo $pagination_links;
?>
Untuk mempercantik tampilan link pagination dengan menggunakan Bootstrap adalah
membuat file pagination.php sinpan di folder Application/Config.
isi file pagination.php :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// --------------------------------------------------------------------------
// $config['base_url'] = 'blog';
$config['per_page'] = 2;
$config['uri_segment'] = 3;
// $config['page_query_string'] = TRUE;
$config['use_page_numbers'] = TRUE;
$config['query_string_segment'] = 'page';
$config['full_tag_open'] = '<div ><ul class="pagination">';
$config['full_tag_close'] = '</ul></div><!--pagination-->';
$config['first_link'] = '« First';
$config['first_tag_open'] = '<li class="prev page">';
$config['first_tag_close'] = '</li>';
$config['last_link'] = 'Last »';
$config['last_tag_open'] = '<li class="next page">';
$config['last_tag_close'] = '</li>';
$config['next_link'] = 'Next →';
$config['next_tag_open'] = '<li class="next page">';
$config['next_tag_close'] = '</li>';
$config['prev_link'] = '← Previous';
$config['prev_tag_open'] = '<li class="prev page">';
$config['prev_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li class="page">';
$config['num_tag_close'] = '</li>';
// $config['display_pages'] = FALSE;
//
$config['anchor_class'] = 'follow_link';
// --------------------------------------------------------------------------
No comments:
Post a Comment