Codeigniter中的分页返回错误

时间:2016-02-05 15:59:45

标签: php codeigniter pagination

我的问题是分页不起作用,它完美地显示了页数<< 1 2 3>等...但是当我试图继续第2页时,它给了我一个错误:

The requested URL /mysite/welcome/index/2 was not found on this server.

这是我的模型名为: Country.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

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

这是我的控制器,它显示页面上的新闻和分页,但不幸的是,当我尝试去第2页时,我说它会返回错误信息。

我的控制器名为 Welcome.php

 class Welcome extends CI_Controller {

/**
 * Index Page for this controller.
 *
 * Maps to the following URL
 *      http://example.com/index.php/welcome
 *  - or -
 *      http://example.com/index.php/welcome/index
 *  - or -
 * Since this controller is set as the default controller in
 * config/routes.php, it's displayed at http://example.com/
 *
 * So any other public methods not prefixed with an underscore will
 * map to /index.php/welcome/<method_name>
 * @see https://codeigniter.com/user_guide/general/urls.html
 */

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

}

public function view($slug = NULL)
{
    $data['news_item'] = $this->news_model->get_news($slug);

    if (empty($data['news_item']))
    {
            show_404();
    }

    $data['title'] = $data['news_item']['title'];

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

路线是默认路线

 $route['default_controller'] = 'welcome';

我也可以在主页上看到我的新闻和分页:

localhost/mysite

我希望我很清楚,请帮助我解释为什么分页不起作用。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这样做 控制器

public function managecategory() {
        $this->load->helper(array('form', 'url'));
        $this->load->view('moderator/templates/header');
        $this->load->view('moderator/templates/sidebar');
        $parent = '0';
        $data['catdata'] = $this->b2bcategory_model->form_select($parent);
        $this->load->library('pagination');
        // bootstrap style for maintaining pagination
        $config = array();
        $config["base_url"] = base_url() . "moderator/B2BCategory/managecategory";
        $config["total_rows"] = $this->b2bcategory_model->record_count();
        $config["per_page"] = 20;
        $config["uri_segment"] = 4;
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['first_link'] = 'first';
        $config['last_link'] = 'last';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['prev_link'] = '&laquo';
        $config['prev_tag_open'] = '<li class="prev">';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = '&raquo';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $this->pagination->initialize($config);
        $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
        $data["results"] = $this->b2bcategory_model->fetch_data($config["per_page"], $page);
        $data["links"] = $this->pagination->create_links();
        $this->load->view('moderator/managecategory', $data);
        $this->load->view('moderator/templates/footer');
    }

在模型中

 public function fetch_data($limit, $start) {
        $this->db->limit($limit, $start);

        $query =   $this->db->get_where("jil_category",  array('ctg_parent'=>'0'));


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