【问题标题】:pagination page gives 404 in codeigniter3 framework分页页面在 codeigniter3 框架中给出 404
【发布时间】:2017-09-22 20:25:16
【问题描述】:

我已经在Codeigniter3框架中编写了分页代码,但是当我想去第二,第三等页面时,它显示给我404页面。问题可能出在哪里?我的路线文件:

$route['courses/category/(:any)'] = 'courses/all_courses/$1';
$route['courses/(:any)'] = 'courses/single_course/$1';

我的控制器:

public function all_courses($categories_slug = NULL, $offset = 0) {

            //pagination
            $config['base_url'] = base_url() . 'courses/category/'.$categories_slug;
            $config['total_rows'] = $this->db->count_all('courses');
            $config['per_page'] = 2;
            $config['uri_segment'] = 4;

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


            $data['title'] = 'Bütün Kurslar Burada';

            $data['courses'] = $this->courses_model->get_all_courses($categories_slug, $config['per_page'], $offset);


            $this->load->view('templates/header');
            $this->load->view('courses/courses', $data);
            $this->load->view('templates/footer');
        }

我的模特:

public function get_all_courses($categories_slug = FALSE, $limit = FALSE, $offset = FALSE) {
        if($limit) {
            $this->db->limit($limit, $offset);
        }
<-- and other codes here -->
}

我的看法:

<div class="col-md-12 courses_page_btn2">
                <nav>
                    <ul class="pagination">
                        <li><a><?php echo $this->pagination->create_links(); ?> </a></li>
                    </ul>
                </nav>
            </div>

【问题讨论】:

标签: php codeigniter pagination


【解决方案1】:

首先,在你的路由文件中,你从 course/category/(:any) 的路由设置为路由到 courses/all_courses/$1,但是你的控制器的 all_courses 方法有第二个参数。所以你需要为此做点什么。我猜由于您的分页使用的是 URI 段 4,因此您需要像这样调整您的路线:

$route['courses/category/(:any)/(:num)'] = 'courses/all_courses/$1/$2';

该路由的问题在于,如果没有 (:num),那么您将得到 404。在大多数分页方案中,您会希望它默认为 1(第一页)。请考虑这条路线:

$route['courses/category/(.+)'] = function ($x, $y = 1)
{
    return 'courses/all_courses/' . $x . '/' . $y;
};

这将路由到您的 all_courses 方法,并提供这两个参数,即使您省略了第二个参数。

该分页适用于该路线:

public function all_courses( $x, $y )
{
    $this->load->helper('url');
    $this->load->library('pagination');

    $pconfig['base_url'] = base_url() . 'courses/category/'.$x;
    $pconfig['total_rows'] = 10;
    $pconfig['per_page'] = 2;
    $pconfig['uri_segment'] = 4;

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

    echo $this->pagination->create_links();
}

【讨论】:

  • 但我没有得到任何东西。我应该也写第二个代码,还是第一个可以?
  • 还有一个问题。如果此分页在最后显示第 3 页的记录,则它在顶部的第 4 页显示相同的元素。我该如何解决?另外,当我点击第三页时,它会转到第四页
  • 这真的与你的问题无关。您问为什么会收到 404。我解决了这个问题吗?如果是这样,您应该考虑接受我的回答,然后针对该特定需求提出一个新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-26
  • 2012-07-05
相关资源
最近更新 更多