我在博客页面上尝试使用CodeIgniter进行分页。编号和限制似乎工作正常,除了尝试访问另一页时始终收到404之外。
奇怪的是,导致此问题的正常原因是正确的。baseUrl和uri_segment。
我的控制器如下所示:
$config = array();
$config["base_url"] = $this->config->site_url("/blog");
$config["total_rows"] = $this->blog_model->count();
$config["per_page"] = 2;
$config["uri_segment"] = 2;
$config["num_links"] = round($config["total_rows"] / $config["per_page"]);
$config['use_page_numbers'] = TRUE;
$this->pagination->initialize($config);
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$this->load->view('blog', array(
'user' => $this->user,
'blog' => $this->blog_model->loadPosts($config['per_page'], $page),
'links' => $this->pagination->create_links(),
'footer' => $this->blog_model->loadFooter()
));
Run Code Online (Sandbox Code Playgroud)
然后在我的模型中,我抓住了帖子
public function loadPosts($limit, $start)
{
$this->db->limit($limit, $start);
$this->db->order_by("date", "desc");
//this loads the contact info
$query = $this->db->get('entries');
return $query->result();
}
Run Code Online (Sandbox Code Playgroud)
我的完整网址为www.mysite.com/blog,然后显示为www.mysite.com/blog/2。
对于base_Url我也尝试过base_url() . "/blog";。
我尝试将uri_segment设置为1和3,但是似乎没有任何效果。
同样,我尝试使用路由,并添加了路由以查看是否可以执行任何操作:
$route['blog/(:num)'] = 'blog/$1';
Run Code Online (Sandbox Code Playgroud)
如果您的代码在index方法中,则可以使用以下代码行:
$ route ['blog /:any'] =“博客/索引/ $ 1”;
因为您使用了segment(2),所以应该将blog / index / $ 1更改为blog /:any。
| 归档时间: |
|
| 查看次数: |
931 次 |
| 最近记录: |