0

我正在使用 Codeigniter 3.1.8 开发一个基本的博客应用程序

我有一个管理仪表板,在表格中显示帖子、类别等。这些表是分页的。帖子有编号。

在控制器中我有:

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');
}

视图如下所示:

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

帖子正确显示和分页。问题是,无论页面如何,帖子计数仅显示 1 到 10:

在此处输入图像描述

我想我必须将控制器中的 $offset 变量添加到视图中并将其添加到计数中。我怎样才能做到这一点?

4

3 回答 3

1

offset从控制器传递值并在视图中接收它

 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');
}

在视图中

  <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>
于 2018-05-12T12:08:29.427 回答
0

还有另一种方法可以做到这一点。

  1. 准备您提到的代码的分页数据数据

  2. 然后你可以使用create_links()这样的分页类方法$this->pagination->create_links();。检查此链接以获取工作示例。

  3. 将其保存到任何变量并传递给您的视图,例如$data['pagination'] = $this->pagination->create_links();

  4. 在要显示分页的视图中打印它。

于 2018-05-12T12:15:10.680 回答
0

我是这样解决的:

在我添加的控制器$data['offset'] = $offset;中。

在我看来:

<?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 ?>
于 2018-05-12T12:16:30.203 回答