找不到分页错误网址

时间:2016-02-02 14:48:02

标签: php codeigniter pagination

我在Codeigniter中创建了一个分页,因为我希望在一个页面中有2个新闻,在其他页面上有2个新闻...

所以,这是我的模型:     

class Country extends CI_Model
 {
    public function __construct() {
    parent::__construct();
}

public function record_count() {
    return $this->db->count_all("news");
}

public function fetch_countries($limit, $start) {
    $this->db->limit($limit, $start);
    $query = $this->db->get("news");

    if ($query->num_rows() > 0) {
        foreach ($query->result() as $row) {
            $data[] = $row;
        }
        return $data;
    }
    return false;
   }
}

我可以看到我的新闻。我的问题是,如果我想移动到下一页,我将收到一个错误,url未找到welcome / index / 2(它的第二页)。 localhost / codeigniter / - >这是我的主页,新闻在主页上显示。可以看到新闻,但当我想滚动到下一页时,它会给出错误。

控制器:

  public function __construct()
{
    parent::__construct();
    $this->load->model('news_model');
    $this->load->helper('url_helper');
    $this->load->model("Country");
    $this->load->library("pagination");
}


public function index()
{
    $config = array();
    $config["base_url"] = base_url() . "welcome/index";
    $config["total_rows"] = $this->Country->record_count();
    $config["per_page"] = 2;
    $config["uri_segment"] = 3;

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

    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $data["results"] = $this->Country->
        fetch_countries($config["per_page"], $page);
    $data["links"] = $this->pagination->create_links();

    $data['news'] = $this->news_model->get_news();
    $data['title'] = 'News archive';

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

}

最后是查看:

  <?php
foreach($results as $data) {
echo $data->title . " - " . $data->text . "<br>";
}
?>

我希望我很清楚,但如果没有,那么请问,我会编辑这个问题。

1 个答案:

答案 0 :(得分:0)

您的代码似乎很好,我猜您没有配置您的路线。 URI Guide

 $route['welcome/(:any)'] = 'welcome/index/$1';

如果您只是欢迎将检测索引功能

 $config["base_url"] = base_url('welcome'); 

Base Url

 $config['base_url'] = 'http://localhost/codeigniter/';