简体   繁体   中英

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

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>

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.

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 ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM