在Codeigniter中,分页无法正常工作

时间:2017-10-10 04:43:15

标签: codeigniter pagination

我是Codeigniter的新手,我正在进行分页。但我的代码有问题。我在以下代码中做了什么错误? 我在下面附上了我的代码段。

路线

$route['default_controller'] = "pages";
$route['default_controller/(:num)'] = "pages/index/$1";

控制器

public function index($page='home')

    {

       if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
        {
        // Whoops, we don't have a page for that!
        show_404();
        }



        $config=[

            'base_url' => base_url().'/pages/index',
            'per_page' => 5,
            'total_rows' =>$this->products_model->record_count()
        ];

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

        $data['title'] = ucfirst($page); // Capitalize the first letter
        $data['products'] = $this->products_model->get_products($config['per_page'], $this->uri->segment(3));

        $data['request'] = 'pages/home';
        $this->load->view('templates/content',$data);
    }

模型

public function get_products($limit, $offset)
{
    $this->db->order_by('id', 'DESC');
    $this->db->limit($limit, $offset);
    $query=$this->db->get('product'); 
    return $query->result_array();
}

public function record_count($value='')
{
    return $this->db->count_all("product");
}

查看

<?php echo $this->pagination->create_links() ?>

在视图中,我正在显示表中的记录。显示第一页记录,但是当我单击第二页时显示错误

"**404 Page Not Found**"

对此问题的任何帮助都将受到高度赞赏。提前谢谢。

1 个答案:

答案 0 :(得分:0)

根据您的逻辑,行&#34;索引($ page =&#39; home&#39;)索引方法从传递给它的参数中获取要显示的脚本。在第一次加载时,默认页面是&#39; home.php&#39;是匹配的,但是当你选择第二页时,CI路由器和索引功能可能会找到&#39; 1.php&#39;或&#39; 2.php&#39;依此类推,根据您的分页,您请求的每个页面都存在这些文件吗?否则,您应该检查页面变量是否是有效页面(例如非负数),然后继续执行并将结果加载到视图中

相关问题