简体   繁体   English

Codeigniter中的分页返回错误

[英]Pagination in Codeigniter returns error

My problem is that pagination is not working, its perfectly shows the number of pages << 1 2 3 >> etc... but when I tried to go on page 2 then it gives me an error like: 我的问题是分页不起作用,它完美地显示了页数<< 1 2 3 >>等...但是当我尝试进入第2页时,它给了我一个错误,例如:

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

Here is my Model called: Country.php 这是我的模型: 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;
   }
}

Here is my Controller which shows the news on the page, and the pagination, but unfortunately as I said when I try to go for page 2 then it returns an error message. 这是我的控制器,它在页面上显示新闻和分页,但是不幸的是,正如我在尝试进入页面2时所说的那样,它返回了一条错误消息。

My Controller called Welcome.php 我的控制器名为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);
}
}

The routes are the default one 路由是默认路由

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

Also I can see my news and pagination on the homepage: 我也可以在首页上看到我的新闻和分页:

localhost/mysite

I hope I was clear, please help me out why the pagination is not working. 我希望我很清楚,请帮我解释为什么分页不起作用。

Thanks for your kind help. 感谢您的帮助。

do like this Controller 确实喜欢这个控制器

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

In Model 在模型中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM