对,第一次使用分页,无法让它在第一页之外工作(404错误)。这一切似乎都围绕着 $offset 变量,就好像我将偏移量的变量更改为整数(例如 15)一样,它会从第 15 行开始在第一页上显示结果 - 但 $this->uri->segment(2) 变量就是不起作用。这一切都正确显示,但第二页无法加载(正如我所说,404错误)。
控制器:
<?php
class Brands extends CI_Controller
{
public function index()
{
$this->load->library('pagination');
$config['base_url'] = base_url() . '/brands/';
$config['total_rows'] = $this->db->count_all('seixweb');
$config['per_page'] = 15;
$limit = $config['per_page'];
$offset = $this->uri->segment(2);
$this->pagination->initialize($config);
$this->load->model('Default_model');
... Etc.
}
}
型:
<?php
class Default_model extends CI_Model
{
function get_list($limit, $offset)
{
$query = $this->db->get('tbl', $limit, $offset);
return $query->result_array();
}
}
网址(第一页):
http://www.mydomain.com/brands
网址(第二页):
http://www.mydomain.com/brands/15
我做错了什么?
尽管它与错误无关,但请查看:
<!-- Products -->
<div class="products">
<div class="cl"> </div>
<p><?=$this->pagination->create_links();?></p>
<div class="cl"> </div>
</div><!-- End Products -->
那是因为你的链接可能不正确。
代码点火器的默认值是 www.example.com/controller/action
,如果您没有更改任何内容,您的链接将调用控制器品牌中名为 15 的函数。
您需要将链接设置为http://www.mydomain.com/brands/yourfunctionname/15
或告诉代码点火器您希望它通过$config['uri_segment'] = 2;
不推荐在您的情况下查找第二个 uri 段。
设置 OFFSET 的默认参数
$config['per_page'] = $this->uri->segment(3, 15);
第二个参数将默认值定义为 15 if $this->uri->segment(3)
不存在。
$config['base_url'] = base_url() . '/brands/';
删除最后一个斜杠
$config['base_url'] = base_url() . '/brands';
添加规则路由.php
$route['brands/(:num)'] = "brands";
从模型中获取记录时,将您的限制值和隔离值发送到模型中,您将获得所有必需的记录