2

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

控制器:

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 段有关?

正在加载的 URL 是第二页的http://www.example.com/home/2

4

3 回答 3

4

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

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

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

于 2012-06-06T00:59:02.207 回答
2

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

 http://www.example.com/home/index/2
于 2012-06-05T17:31:51.313 回答
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?>
于 2013-06-22T07:22:02.420 回答