单击链接时CodeIgniter分页不起作用

时间:2014-09-25 22:03:18

标签: php codeigniter pagination

可能这个问题真的很愚蠢,但我尝试了很多东西而且我总是得到同样的错误......

正如您所看到的,我尝试在CodeIgniter中使用Pagination,我可以使用页面和链接加载视图,但是当我点击链接时,它会显示"找不到对象! "

这是我的控制器:

class Controller extends CI_Controller {

    public function __construct() {
        parent:: __construct();
        $this->load->helper("url");
        $this->load->model("Blogs_model");
        $this->load->library("pagination");
    }

    public function index() {
        $config = array();
        $config["base_url"] = base_url() . "pagination/";
        $config["total_rows"] = $this->Blogs_model->record_count();
        $config["per_page"] = 2;
        $config["uri_segment"] = 3;
        $config['first_link'] = 'First';
        $config['last_link'] = 'End';
        $config['next_link'] = 'Next →';
        $config['prev_link'] = '← Prev';

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

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

        $this->load->view("example1", $data);
    }
}

routes.php文件:

route['default_controller'] = "controller";
$route['pagination/(:any)'] = 'pagination';
$route['404_override'] = '';

的config.php:

config['base_url']  = 'http://localhost/ci';
有人可以帮帮我吗?谢谢。

修改 多数民众赞成我的模特:

class Blogs_model extends CI_Model {

    public function __construct() {
        parent::__construct();
    }

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

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

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

问题是当我加载页面时:localhost / ci /它工作正常,但是当我点击链接时,url会更改为localhost / ci / pagination / 2并且不起作用。我想这是路线的问题,但无论如何我不确定。

编辑2 感谢所有评论家伙,最后我设法让它工作,但Index.php活跃,我的意思是我的网址现在是:

http://localhost/ci/index.php/controller/index/2 

我想让它在没有index.php的情况下运行。我的代码现在是这样的: 控制器:

class Controller extends CI_Controller {

    public function __construct() {
        parent:: __construct();
        $this->load->helper("url");
        $this->load->model("Blogs_model");
        $this->load->library("pagination");
    }

    public function index() {
        $config = array();
        $config["base_url"] = "http://localhost/ci/index.php/controller/index";
        $config["total_rows"] = $this->Blogs_model->record_count();
        $config["per_page"] = 2;

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

        $data["results"] = $this->Blogs_model->fetch_entries($config["per_page"], $this->uri->segment(3));
        $data["links"] = $this->pagination->create_links();

        $this->load->view("example1", $data);
    }
}

config.php中的base_url是

http://localhost/ci;

此外,路线文件现在只有:

$route['default_controller'] = "controller";
$route['404_override'] = '';

2 个答案:

答案 0 :(得分:0)

应该看起来像这样:

路线档案:

$route['blog/(:num)'] = "controller/index/$1";
$route['blog'] = "controller/index";

控制器:

public function index($page = 0) {
  $news = $this->blog_model->get_blog($page);
}

型号:

public function get_blog($page) {
  $offset = $page * 2;
  $stuff = $this->db->get('articles', $offset, 2);
}

答案 1 :(得分:0)

而不是:

$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->Blogs_model->fetch_entries($config["per_page"], $page);

试试这个:

$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$offset = $page==0? 0: ($page-1)*$config["per_page"];
$data["results"] = $this->Blogs_model->fetch_entries($config["per_page"], $offset);

除非你的模型正在做这项工作,否则你需要传递偏移而不是页面。