CodeIgniter Forums
Pagination, how to start with it?[SOLVED] - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Pagination, how to start with it?[SOLVED] (/thread-67276.html)



Pagination, how to start with it?[SOLVED] - kirasiris - 02-03-2017

Hello guys, I'm wondering how to do the pagination, as I followed the user guide I was expecting it to work as it says, but it did not. Any suggestion is goign to be very helpful, thank you.

Can somebody explain to me what's the right way to use the pagination class?

This is my controller(News, function blog):

PHP Code:
        public function blog()
 
       {
 
               $data['blog'] = $this->blog_model->get_blog();
                
$data['categorias'] = $this->categorias_model->get_categorias();
                
$data['title'] = 'Blog';
        
                
$this->load->view('templates/head',$data);
                
$this->load->view('templates/navbar',$data);
                
$this->load->view('news/blog/index'$data);
                
$this->load->view('templates/footer',$data);
                
                
// Pagination for Blog //

                
$this->load->library('pagination');
                
                
$config['base_url'] = site_url('/blog/');
                
$config['total_rows'] = 200;
                
$config['per_page'] = 1;
                
$config['uri_segment'] = 3;
                
$config['num_links'] = 2;
                
$config['use_page_numbers'] = TRUE;
                
$config['page_query_string'] = TRUE;
                
                
$this->pagination->initialize($config);
                
                echo 
$this->pagination->create_links();

                

        } 

This is my view (Blog)
Code:
<div class="container" id="main">
<div class="row">
<!-- Article -->
<div class="col-md-8 animated fadeIn">
<?php foreach($blog as $blog) : ?>
<div class="card text-xs-left"><!-- FIRST ARTICLE DEMO -->
<div class="card-header" id="article-header">
<h4><a href="<?php echo site_url('/blog/'.$blog['slug']); ?>"><?php echo ucfirst($blog['titulo']); ?></a></h4>
</div>
<img src="<?php echo $blog['imagen']; ?>" width="750" heihgt="350" class="img-fluid">
<!--<div class="card-body">
<p> <?phpp // echo word_limiter($post['contenido'],5); ?></p>
</div>-->
<div class="card-footer" id="article-footer">
<div class="row">
<div class="col-lg-12 col-md-9 col-sm-8">
<i class="fa fa-calendar" aria-hidden="true"></i> <?php echo ucfirst($blog['fecha']); ?>

<i class="fa fa-folder" aria-hidden="true"></i> <?php echo $blog['categoria_id']; ?>

<a href="<?php echo site_url('/blog/'.$blog['slug']); ?>" class="pull-right">Read more »</a>
</div>
</div>
</div>
</div>
<?php endforeach ?>
<!-- /Article -->
<?php echo $pagination ?>

This is my pagination file(inside config folder):

PHP Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Pagination Config
 * 
 * Just applying codeigniter's standard pagination config with twitter 
 * bootstrap stylings
 * 
 * @license        http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
 * @author        Mike Funk
 * @link        http://codeigniter.com/user_guide/libraries/pagination.html
 * @email        [email protected]
 * 
 * @file        pagination.php
 * @version        1.3.1
 * @date        03/12/2012
 * 
 * Copyright (c) 2011
 */
 
// --------------------------------------------------------------------------

// $config['base_url'] = '';
$config['per_page'] = 2;
$config['uri_segment'] = 3;
$config['num_links'] = 2;
$config['page_query_string'] = TRUE;
// $config['use_page_numbers'] = TRUE;
$config['query_string_segment'] = 'pages';

$config['full_tag_open'] = '<div class="pagination"><ul>';
$config['full_tag_close'] = '</ul></div><!--pagination-->';

$config['first_link'] = '&laquo; First';
$config['first_tag_open'] = '<li class="prev page">';
$config['first_tag_close'] = '</li>';

$config['last_link'] = 'Last &raquo;';
$config['last_tag_open'] = '<li class="next page">';
$config['last_tag_close'] = '</li>';

$config['next_link'] = 'Next &rarr;';
$config['next_tag_open'] = '<li class="next page">';
$config['next_tag_close'] = '</li>';

$config['prev_link'] = '&larr; 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';

// --------------------------------------------------------------------------

/* End of file pagination.php */
/* Location: ./bookymark/application/config/pagination.php */ 


I attached an image on this posrt as well. For some reason the pagination works in the url as "http://localhost/demo6/blog/?pages=2" and so on (this is good), but when I put it in the view it send an error:

A PHP Error was encountered

Severity: Notice
Message: Undefined variable: pagination
Filename: blog/index.php
Line Number: 35
Backtrace:
File: C:\xampp2\htdocs\demo6\application\views\news\blog\index.php
Line: 35
Function: _error_handler

File: C:\xampp2\htdocs\demo6\application\controllers\News.php
Line: 66
Function: view

File: C:\xampp2\htdocs\demo6\index.php
Line: 315
Function: require_once



RE: Pagination, how to start with it? - drub4n - 02-04-2017

There is no variable pagination sent to your view, you should do :

in controller :

Code:
$this->data['pagination'] = $this->pagination->create_links();
$this->load->view('news\blog\index.php',$this->data);



And then $pagination will be accessible from within your view.

Hope it helps