【问题标题】:Pagination is not working properly in Codeigniter分页在 Codeigniter 中无法正常工作
【发布时间】:2017-10-10 04:43:14
【问题描述】:

我是 Codeigniter 的新手,我正在研究分页。但是我的代码有问题。我在下面的代码中犯了什么错误? 我在下面附上了我的代码 sn-p。

路线

$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**"

我们将不胜感激有关此问题的任何帮助。提前致谢。

【问题讨论】:

  • 您希望看到哪些记录?超过 5 个吗?
  • 是的,当然。我有 30 多条记录。但是当我点击分页的第二页时。页面未找到错误显示
  • 您也可以发布按钮的代码吗?可能是这样。或者,当您获得 404 时,$page 的值是多少?也许那个文件不存在。
  • $this-&gt;pagination-&gt;create_links(); 函数在 Codeigniter 中自动创建按钮。 $page 的值为 'home'。

标签: codeigniter pagination


【解决方案1】:

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

【讨论】:

  • 是的,你是对的@Peter M。我创建了另一种方法和路线。它工作正常
猜你喜欢
  • 1970-01-01
  • 2017-11-27
  • 1970-01-01
  • 1970-01-01
  • 2018-06-11
  • 1970-01-01
  • 1970-01-01
  • 2012-02-22
  • 1970-01-01
相关资源
最近更新 更多