Pagination Codeigniter 3

Jika dalam satu halaman web memiliki banyak artikel, tentu akan membuat halaman tersebut sangat panjang kebawah. Sehingga membuat tampilan web menjadi tidak bagus dan menarik. Oleh sebab itu disini kita akan membuat pagination sederhana yang akan membatasi tampilan artikel pada halaman home.

  • Tambahkan Code Dibawah Pada Akhir File Routes.php Yang Ada Pada Folder Config :
$route['home/(:any)'] = 'home/index';
  • Buat Controller Seperti Dibawah :
function __construct()
{
parent::__construct();
$this->load->model('M_home');
$this->load->helper('url');
$this->load->library('pagination');
}

function index()
{
$config = array();
$config["base_url"] = base_url().'home';
$config["total_rows"] = $this->M_home->totalPosts();
$config["per_page"] = 2;
$config["uri_segment"] = 2;

$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$config['full_tag_open'] = "<ul class='pagination pagination-sm' style='background-color:black;'>";
$config['full_tag_close'] = "</ul>";
$config['num_tag_open'] = "<li>";
$config['num_tag_close'] = "</li>";
$config['cur_tag_open'] = "<li class='disable'><li class='active'><a href='#'>";
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
$config['next_tag_open'] = "<li>";
$config['next_tag_close'] = "</li>";
$config['prev_tag_open'] = "<li>";
$config['prev_tag_close'] = "</li>";
$config['first_tag_open'] = "<li>";
$config['first_tag_close'] = "</li>";
$config['last_tag_open'] = "<li>";
$config['last_tag_close'] = "</li>";

$this->pagination->initialize($config);
$page = $this->uri->segment(2);
$data["articles"] = $this->M_home->getPosts($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();

$this->load->view('V_home', $data);
}
  • Code Pada Model :
function getPosts($limit, $offset)
{
$this->db->select("*");
$this->db->from('tb_articles');
$this->db->limit($limit, $offset);
$query = $this->db->get();
return $query->result();
}

function totalPosts()
{
return $this->db->count_all_results('tb_articles');
}
  • Code Pada View Tambahkan <?php echo $links; ?> Pada Bagian Bawah Artikel :
<main class="col-md-8">

<?php foreach($articles as $row): ?>

<article class="post post-1">
<header class="entry-header">
<h1 class="entry-title">
<a href="<?php echo site_url('home/article_read/'.$row->article_slug) ?>"><?php echo character_limiter($row->article_subject, 55) ?></a>
</h1>
<div class="entry-meta">
<span class="post-category"><a href="#"><?php echo $row->category_name ?></a></span>
<span class="post-date"><a href="#"><time class="entry-date" datetime="2012-11-09T23:15:57+00:00">Posted on <?php $dob = $row->article_date; echo date("F, d Y", strtotime($dob)); ?></time></a></span>
<span class="post-author"><a href="#"><?php echo $row->user_name ?></a></span>
<span class="comments-link"><a href="#"><?php echo $row->article_read ?> Reads</a></span>
</div>
</header>
<div class="entry-content clearfix">
<p><?php echo character_limiter($row->article_description, 210) ?></p>
<div class="read-more cl-effect-14">
<a href="<?php echo site_url('home/article_read/'.$row->article_slug) ?>" class="more-link">Continue reading <span class="meta-nav">?</span></a>
</div>
</div>
</article>

<?php endforeach; ?>

<div class="post">
<?php echo $links; ?>
</div>

</main>

Pembuatan pagination selesai. Untuk merubah total artikel yang ditampilkan dalam 1 page/halaman cukup mengganti nilai ( $config["per_page"] = 2; ).


Total 0 comment with 0 comment reply