Codeigniter分页 - 404错误

时间:2012-06-05 17:20:51

标签: codeigniter codeigniter-2 codeigniter-url

我正在尝试在我的主页上使用分页博客文章:

控制器:

public function index()
{
   $data['view'] = "home";

    /** Pagination **/
    $config['base_url'] = base_url().'home/';
    $config['total_rows'] = $this->Blog_model->count_articles();
    $config['per_page'] = 2; 
    $config['uri_segment'] = 2;

    $this->pagination->initialize($config);

    $data['paginate'] = $this->pagination->create_links();
    $data['articles'] = $this->Blog_model->get_articles($config['per_page'],$this->uri->segment(2));

    $this->load->view('template', $data);

}

一切似乎都可以正常使用信息检索和分页,但是,当我点击数字链接或下一个链接时,我得到404 Not Found错误。

我假设这与URI段有关吗?

第二页正在加载的网址为http://www.example.com/home/2

3 个答案:

答案 0 :(得分:4)

您还可以添加路由规则,如:

$route['home/(:num)'] = 'yourhomecontroller';

然后你可以在没有索引或任何方法的情况下使用它,num告诉它在home /之后用一个数字路由任何url到home的索引

答案 1 :(得分:2)

Codeigniter页面的格式为http://domain.com/controller/method/arguments 如果省略方法,则会加载默认方法,但如果需要传递参数,则必须按原样放置方法。

 http://www.example.com/home/index/2

答案 2 :(得分:0)

控制器代码

public function index()
{

$home=$this->uri->segment(2);

$limit_ti=2;
if(!$home):
offset_ti = 0;
else:
$offset_ti = $home;
endif;


$this->load->model('Blog_model');// call model
$this->load->library('pagination'); // call library

$query=$this->Blog_model->get_articles($limit_ti,$offset_ti); // geting aan article 

$total_page = $this->Blog_model->count_articles(); // count row

 /** Pagination **/
 $config['base_url'] = base_url().'index.php/home/'; // assuming you doesn't have htaccess
 $config['total_rows'] =$total_page->num_rows();
 $config['per_page'] = 2; 
 $config['uri_segment'] = 2;

    $this->pagination->initialize($config);
    $data = array('query' => $query,'page'=>$home);

    $data['view'] = "home";
    $this->load->view('template', $data);

}

模型代码

function get_articles($limit,$ofset){
$query=$this->db->query("select * from *table* order by id ASC LIMIT $ofset,$limit");
return $query;
}

function count_articles(){
$query=$this->db->query("select * from table");
return $query;
}

在视图中

<?php echo $this->pagination->create_links(); // call the pagnation?>