Codeigniter 3 pagination: add the $offset variable to the view

pagination in codeigniter step by step
dynamic pagination in codeigniter
codeigniter pagination example
codeigniter pagination with query string
custom pagination codeigniter
codeigniter pagination with search
$this- pagination- create_links() not working
simple pagination in codeigniter

I am working on a basic blog application in Codeigniter 3.1.8.

I have an Admin dashboard displaying the Posts, Categories, etc, in tables. These tables are paginated. The posts are numbered.

In the controller I have:

public function index() {
    $this->load->library('pagination');
    $config = [
        'base_url' => base_url("/dashboard/posts"),
        'page_query_string' => TRUE,
        'query_string_segment' => 'page',
        'display_pages' => TRUE,
        'use_page_numbers' => TRUE,
        'per_page' => 10,
        'total_rows' => $this->Posts_model->get_num_rows(),
        'uri_segment' => 3,
        'first_link' => '«',
        'first_tag_open' =>  '<li>',
        'first_tag_close' => '</li>',
        'last_link' => '&raquo;',
        'last_tag_open' =>  '<li>',
        'last_tag_close' => '</li>',
        'full_tag_open' =>  '<ul class="pagination">',
        'full_tag_close' => '</ul>',
        'next_link' =>  '&rsaquo;',
        'next_tag_open' =>  '<li>',
        'next_tag_close' => '</li>',
        'prev_link' => '&lsaquo;',
        'prev_tag_open' =>  '<li>',
        'prev_tag_close' => '</li>',
        'num_tag_open' =>   '<li>',
        'num_tag_close' =>  '</li>',
        'cur_tag_open' =>   '<li class="active"><span>',
        'cur_tag_close' =>  '</span></li>'
    ];
    if (!isset($_GET[$config['query_string_segment']]) || $_GET[$config['query_string_segment']] < 1) {
        $_GET[$config['query_string_segment']] = 1;
    }
    $limit = $config['per_page'];
    $offset = ($this->input->get($config['query_string_segment']) - 1) * $limit;
    $this->pagination->initialize($config);
    $data['posts'] = $this->Posts_model->get_posts($limit, $offset);    

    $this->load->view('partials/header', $data);
    $this->load->view('dashboard/dindex');
    $this->load->view('partials/footer');
}

The view looks like this:

<table class="table table-striped table-sm border-0">
  <thead>
    <tr>
      <th>#</th>
      <th>Title</th>
      <th>Publication date</th>
      <th class="text-center">Actions</th>
    </tr>
  </thead>
  <tbody>
    <?php foreach ($posts as $index => $post): ?>
    <tr>
      <td><?php echo $index + 1; ?></td>
      <td><?php echo $post->title; ?></td>
      <td><?php echo nice_date($post->created_at, 'D, M d, Y'); ?></td>
      <td class="text-center">
        <div class="btn-group btn-group-sm" role="group">
          <a href="<?php echo base_url('posts/post/post/') . $post->id; ?>" class="btn btn-success"><i class="fa fa-eye"></i> View</a>
          <a href="<?php echo base_url('posts/edit/') . $post->id; ?>" class="btn btn-success"><i class="fa fa-pencil-square-o"></i> Edit</a>
          <a href="<?php echo base_url('posts/delete/') . $post->id; ?>" id="delete_post" class="btn btn-success"><i class="fa fa-trash"></i> Delete</a>
        </div>
      </td>
    </tr>
    <?php endforeach ?>
  </tbody>
</table>
<div class="card-footer bg-white py-1">
  <?php $this->load->view("partials/pagination");?>
</div>

The posts are displayed and paginated correctly. The problem is that whatever the page, the post count shows only 1 to 10:

I guess I have to add the $offset variable from the controller to the view and the add it to the count. How can I do that?

Pass offset value from controller and receive it in view

 public function index() {
    $this->load->library('pagination');
    $config = [
        'base_url' => base_url("/dashboard/posts"),
        'page_query_string' => TRUE,
        'query_string_segment' => 'page',
        'display_pages' => TRUE,
        'use_page_numbers' => TRUE,
        'per_page' => 10,
        'total_rows' => $this->Posts_model->get_num_rows(),
        'uri_segment' => 3,
        'first_link' => '&laquo;',
        'first_tag_open' =>  '<li>',
        'first_tag_close' => '</li>',
        'last_link' => '&raquo;',
        'last_tag_open' =>  '<li>',
        'last_tag_close' => '</li>',
        'full_tag_open' =>  '<ul class="pagination">',
        'full_tag_close' => '</ul>',
        'next_link' =>  '&rsaquo;',
        'next_tag_open' =>  '<li>',
        'next_tag_close' => '</li>',
        'prev_link' => '&lsaquo;',
        'prev_tag_open' =>  '<li>',
        'prev_tag_close' => '</li>',
        'num_tag_open' =>   '<li>',
        'num_tag_close' =>  '</li>',
        'cur_tag_open' =>   '<li class="active"><span>',
        'cur_tag_close' =>  '</span></li>'
    ];
    if (!isset($_GET[$config['query_string_segment']]) || $_GET[$config['query_string_segment']] < 1) {
        $_GET[$config['query_string_segment']] = 1;
    }
    $limit = $config['per_page'];
    $offset = ($this->input->get($config['query_string_segment']) - 1) * $limit;
    $this->pagination->initialize($config);
    $data['posts'] = $this->Posts_model->get_posts($limit, $offset);    
    $data['offset'] = $offset;    //add this line

    $this->load->view('partials/header', $data);
    $this->load->view('dashboard/dindex');
    $this->load->view('partials/footer');
}

In view

  <tbody>
    <?php foreach ($posts as $index => $post): ?>
    <tr>
      <td><?php echo $offset ++; ?></td>
      <td><?php echo $post->title; ?></td>
      <td><?php echo nice_date($post->created_at, 'D, M d, Y'); ?></td>
      <td class="text-center">
        <div class="btn-group btn-group-sm" role="group">
          <a href="<?php echo base_url('posts/post/post/') . $post->id; ?>" class="btn btn-success"><i class="fa fa-eye"></i> View</a>
          <a href="<?php echo base_url('posts/edit/') . $post->id; ?>" class="btn btn-success"><i class="fa fa-pencil-square-o"></i> Edit</a>
          <a href="<?php echo base_url('posts/delete/') . $post->id; ?>" id="delete_post" class="btn btn-success"><i class="fa fa-trash"></i> Delete</a>
        </div>
      </td>
    </tr>
    <?php endforeach ?>
  </tbody>

Pagination Class, Customizing the Pagination; Adding Enclosing Markup; Customizing the The $​config array contains your configuration variables. The following is a list of all the preferences you can pass to the initialization function to tailor the display. URI Segments as well as query string arguments, which until 3.0 was not  CodeIgniter’s Pagination class is very easy to use, and it is 100% customizable, either dynamically or via stored preferences. Example Notes. Setting preferences in a config file. Customizing the Pagination. Adding Enclosing Markup. Customizing the First Link. Customizing the Last Link. Customizing the “Next” Link.

There are another alternative way to do it.

  1. Prepare your pagination data data your mentioned code

  2. Then you can use create_links() from pagination class method like this $this->pagination->create_links();. Check this link for a working example.

  3. Save it to any variable & pass to your view like $data['pagination'] = $this->pagination->create_links();

  4. print it inside your view where you want to show pagination.

How to make paging from the basics, I put default pagination configs in application/config/pagination.php : $config['​num_links'] = 3; //per_page is the variable of pagination library, if you want you can in view: PHP Code: <?php echo $pagination; //to show pagination get_posts($filter, $offset, $limit) will filter posts according to data in  CodeIgniter’s Pagination class is very easy to use, and it is 100% customizable, either dynamically or via stored preferences. Example Notes. Setting preferences in a config file. Customizing the Pagination. Adding Enclosing Markup. Customizing the First Link. Customizing the Last Link. Customizing the “Next” Link.

I solved it this way:

In the controller I added $data['offset'] = $offset;.

In the view I have:

<?php foreach ($posts as $index => $post): ?>
<tr>
  <td><?php $count = $index + 1; echo $count + $offset; ?></td>
  <td><?php echo $post->title; ?></td>
  <td><?php echo nice_date($post->created_at, 'D, M d, Y'); ?></td>
  <td class="text-center">
    <div class="btn-group btn-group-sm" role="group">
      <a href="<?php echo base_url('posts/post/post/') . $post->id; ?>" class="btn btn-secondary"><i class="fa fa-eye"></i> View</a>
      <a href="<?php echo base_url('posts/edit/') . $post->id; ?>" class="btn btn-secondary"><i class="fa fa-pencil-square-o"></i> Edit</a>
      <a href="<?php echo base_url('posts/delete/') . $post->id; ?>" id="delete_post" class="btn btn-secondary"><i class="fa fa-trash"></i> Delete</a>
    </div>
  </td>
</tr>
<?php endforeach ?>

Pagination, CodeIgniter provides a very simple, but flexible pagination library that is simple to theme, works with So, we grab that and assign it to the $pager variable in the view. the page 3 link can be generated, along with the other links, as follows:. How to Add Codeigniter Pagination Codeigniter Pagination: In Day 5 Delete Operation we finished all CodeIgniter CRUD operations. Which shows all database records for post table, update existing posts, delete post & create new post.

Getting pagination to work with POSTED variables, it doesn't give that crappy cached POSTed variables message. 3) Handle incoming POSTed form data which sends an offset download the view file here; (Remove view_ from title, rename as .php and put in view folder) CodeIgniter Pagination Database Model ; CodeIgniter Pagination Routes ; CodeIgniter Pagination Controller ; Database configuration. We will start with creating the database and inserting some dummy records in it. It will be a single table database with 50 records on it. Run the following script against MySQL to create the database authors table.

Pagination, offset issue | General Discussion, Customizing the Pagination; Adding Enclosing Markup; Customizing the The $​config array contains your configuration variables. The following is a list of all the preferences you can pass to the initialization function to tailor the display. URI Segments as well as query string arguments, which until 3.0 was not  As a robust framework, CodeIgniter provides very efficient way to handle it with its integrated support. In this tutorial, we will see through an example, how we can implement CodeIgniter pagination and enhance it further as per our need.

Codeigniter pagination help, Chat with fellow EECMS users in the 'Pagination, offset issue' //load the view $​this->load->view('books_view',$data); } } /* End of file books.php */ /* Location: . 1cookie1. 3 posts. Hi. I am also new to codeigniter but If you try of the rows $​start = $limit * $page - $limit; // do not put $limit*($page - 1) // if for  CodeIgniter 3 has a 2MB download, including the user guide. CodeIgniter 4 is a 1.2MB download, plus 6MB for the user guide. Much of the CodeIgniter configuration is done by convention, for instance putting models in a "models" folder. There are still a number of configuration options available