我正在使用 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' => '»',
'last_tag_open' => '<li>',
'last_tag_close' => '</li>',
'full_tag_open' => '<ul class="pagination">',
'full_tag_close' => '</ul>',
'next_link' => '›',
'next_tag_open' => '<li>',
'next_tag_close' => '</li>',
'prev_link' => '‹',
'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 变量添加到视图中并将其添加到计数中。我怎样才能做到这一点?
