【发布时间】:2017-10-10 04:43:14
【问题描述】:
我是 Codeigniter 的新手,我正在研究分页。但是我的代码有问题。我在下面的代码中犯了什么错误? 我在下面附上了我的代码 sn-p。
路线
$route['default_controller'] = "pages";
$route['default_controller/(:num)'] = "pages/index/$1";
控制器
public function index($page='home')
{
if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$config=[
'base_url' => base_url().'/pages/index',
'per_page' => 5,
'total_rows' =>$this->products_model->record_count()
];
$this->pagination->initialize($config);
$data['title'] = ucfirst($page); // Capitalize the first letter
$data['products'] = $this->products_model->get_products($config['per_page'], $this->uri->segment(3));
$data['request'] = 'pages/home';
$this->load->view('templates/content',$data);
}
型号
public function get_products($limit, $offset)
{
$this->db->order_by('id', 'DESC');
$this->db->limit($limit, $offset);
$query=$this->db->get('product');
return $query->result_array();
}
public function record_count($value='')
{
return $this->db->count_all("product");
}
查看
<?php echo $this->pagination->create_links() ?>
在视图中,我正在显示表中的记录。记录的第一页显示,但是当我单击第二页时它显示错误
"**404 Page Not Found**"
我们将不胜感激有关此问题的任何帮助。提前致谢。
【问题讨论】:
-
您希望看到哪些记录?超过 5 个吗?
-
是的,当然。我有 30 多条记录。但是当我点击分页的第二页时。页面未找到错误显示
-
您也可以发布按钮的代码吗?可能是这样。或者,当您获得 404 时,
$page的值是多少?也许那个文件不存在。 -
$this->pagination->create_links();函数在 Codeigniter 中自动创建按钮。$page的值为 'home'。