【问题标题】:CodeIgniter pagination not working when clicking on a link单击链接时,CodeIgniter 分页不起作用
【发布时间】:2014-11-20 20:01:36
【问题描述】:

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

如您所见,我正在尝试在 CodeIgniter 中使用分页,我可以加载包含页面和链接的视图,但是当我单击链接时,它会显示 “找不到对象!”

这是我的控制器:

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 感谢所有 cmets 人员,最后我设法让它工作,但 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'] = '';

【问题讨论】:

    标签: php codeigniter pagination


    【解决方案1】:

    应该看起来像这样:

    路线文件:

    $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);
    }
    

    【讨论】:

    • 这是一个示例,请确保您的 index.php 仍然启用,并进行相应的设置。
    【解决方案2】:

    而不是这个:

    $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);
    

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

    【讨论】:

    • 问题:你在你的 url 中使用 index.php 吗?如果你的 url 中没有 index.php,你的 htaccess 会是什么样子你能给出一个你用于页面的示例 url首次加载和第 2 或第 3 页?
    • 我不在我的网址中使用 index.php。这是 .htaccess 的内容: RewriteEngine on RewriteCond $1 !^(index\.php|images|robots\.txt) RewriteRule ^(.*)$ /index.php/$1 [L] S抱歉不知道怎么做在这里格式化:S
    • 你没有提供我所询问的网址:请在第一次加载时提供页面 url,并提供页面 url 以了解页面 2 或 3 的分页输出
    • 您能否更新您针对 James 的建议所做的代码的回答/或评论?您刚刚说“不工作”...也许您犯了一个错误...可能有助于显示您添加或更改的代码?你得到的错误似乎更像是一个路线问题......虽然我仍然认为我的答案是另一个问题。
    • 现在我不在家,所以我无法使用我尝试过的代码进行更新,但 URLS 是:首次加载时的 localhost/ci/ (这有效并显示列表)和当我点击一个链接,那就是 url:localhost/ci/pagination/2。这是我的代码,现在不能用詹姆斯的建议说,对不起。我完全确定这一定是路线问题。我开始使用 CodeIgniter 不到 3 天,在这段时间内使用 CodeIgniter 遇到了很多路由问题:S
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    相关资源
    最近更新 更多