【发布时间】:2018-05-30 08:55:11
【问题描述】:
我重温了 CodeIgniter 的分页类。
由于文档缺乏详细信息,我在网上查看了许多教程。
所以我复制粘贴了教程并更改了一些相关细节以适应我拥有的随机表。
控制器:
public function sample_pagination($offset = 0){
$num_rows=$this->db->count_all("genre");
$config['base_url'] = base_url().'pages/sample_pagination/';
$config['total_rows'] = $num_rows;
$config['per_page'] = 5;
$config['num_links'] = $num_rows;
$config['use_page_numbers'] = TRUE;
$this->pagination->initialize($config);
$data['records']=$this->db->get('genre', $config['per_page'],$offset);// take record of the table
$header = array('genre_Id','name'); // create table header
$this->table->set_heading($header);// apply a heading with a header that was created
$this->load->view('pages/sample',$data); // load content view with data taken from the users table
}
观点:
<body>
<div id="container">
<h1>Welcome to CodeIgniter Pagination System!</h1>
<div id="body">
<?php echo $this->table->generate($records); ?>
<?php echo $this->pagination->create_links(); ?>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
</div>
</body>
它工作正常,我唯一的问题是,我拥有的数据库表类型有 14 个条目,视图上的结果因每页设置而异。
例如上面的例子,我将 per_page 设置为 5。它将显示分页的每页 5 行,带有 3 个分页链接
问题是细节只显示到表格的第 8 行,有 14 行,并且重复。
首页是
1 2 3 4 5
第二页是
3 4 5 6 7
第三页是
4 5 6 7 8
我指的是上面的genre_id。我的表流派将流派 ID 作为 PK,流派名称作为列。所以它只有 2 列。
无论如何,如果我将 Per_page 设置为 1,它会正确显示从genre_id 1 到 14 的所有表行。
我很困惑这是怎么回事?我自己做很容易学习,所以我尝试通过在线复制教程来做,这样我就可以看到它是如何工作的,但我对此感到困惑。
【问题讨论】:
-
sample_pagination控制器对吗?并且您将页码作为偏移量传递?你不是那样做的。 -
它来自我在网上找到的教程。 w3programmers.com/… 和 'pages' 是控制器,而 'sample_pagination' 是方法。
-
我想我开始明白为什么会这样了。在 codeigniter 活动记录中,您检索的记录有一个偏移量/限制,并且本教程使用偏移量的方式一定与 db 查询混淆了?例如 $this->db->get('table', 1);只会检索 1 行。我从未将两者联系起来,因为我不知道偏移量是正确还是错误,但看到它是一个教程,我只是假设它是正确的。
-
我想是的,我现在看不懂教程,但我会用我使用的代码写一个答案
标签: php codeigniter pagination