【问题标题】:Unable to configure pagination properly in Codeigniter无法在 Codeigniter 中正确配置分页
【发布时间】:2017-09-14 18:36:43
【问题描述】:

我是 Codeigniter 分页的新手,所以我无法正确配置它,这就是为什么我需要你们的帮助。

这是我目前的代码

public function nearbay_get($idNearBay = null)
  {
    $config['base_url'] = 'http://mypage.si/rest/api/nearbay/nearbay';
    $config['total_rows'] = $this->Near_bays->count();
    $config["per_page"] = 5;
    $config['first_link'] = 'First';
    $config['last_link'] = 'Last';
    $config['suffix'] = '?'.http_build_query($_GET, '', "&");
    $config['use_page_numbers'] = TRUE;
    $config['enable_query_strings'] = 'page=';

    $choice = $config["total_rows"] / $config["per_page"];
    $config["num_links"] = round($choice);
    $this->pagination->initialize($config);

    $page = ($this->uri->segment(6))? $this->uri->segment(6) : 0;
    $results = $this->Near_bays->get(null, [null, null], [null, null], '', $config["per_page"], $page);
    foreach($results as $data) {
        echo $data->id_near_bay . " ------- " . $data->name . "<br>";
    }
    echo $this->pagination->create_links();
  }

如果我访问http://mypage.si/rest/api/nearbay/nearbay,这就是结果 如果我点击任何页码,我会得到空白页,我的 url 会更改为http://mypage.si/rest/api/nearbay/nearbay

所以我的问题是。如何让分页工作?如何正确配置分页,以便我能够通过 ?page 和 ?per_page 值通过 url 获取结果。我的最终链接应该如下所示http://mypage.si/rest/api/nearbay/nearbay?page=1&per_page=20

如果您需要任何其他信息,请告诉我,我会提供...谢谢!

【问题讨论】:

    标签: php rest codeigniter pagination


    【解决方案1】:

    这是答案,希望对您有所帮助,网址应该是这样的:http://mypage.si/rest/api/nearbay/nearbay/1

    public function nearbay_get($idNearBay = null)
    {
        $config['base_url'] = 'http://mypage.si/rest/api/nearbay/nearbay';
        $config["per_page"] = 5;
        $config["num_links"] = 3;
        $config["uri_segment"] = 6;
        $config['use_page_numbers'] = TRUE;
    
        $config['first_link'] = 'First';
        $config['last_link'] = 'Last';
    
        $config['suffix'] = '?'.http_build_query($this->input->get(), '', "&");
        $config['first_url'] = $config['base_url'] . $config['suffix'];
    
        $page = ($this->uri->segment(6))? $this->uri->segment(6) : 0;
        $offset = ($page == 0 ? 0 : ($page - 1) * $config["per_page"]);
    
        $config['total_rows'] = $this->Near_bays->count();
    
        $this->pagination->initialize($config);
    
        $results = $this->Near_bays->get(null, [null, null], [null, null], '', $config["per_page"], $offset);
        foreach($results as $data) {
            echo $data->id_near_bay . " ------- " . $data->name . "<br>";
        }
    
        //$current_page = $page == 0 ? 1 : $page;
        //$start = $page == 0 ? 1 : (($page - 1) * $config["per_page"] + 1);
        //$end = ($start + count($results) - 1);
        //$total_page = ceil($config['total_rows'] / $config['per_page']);
        echo $this->pagination->create_links();
    }
    

    要检查uri_segment,请检查号码。目前是6,如果没有收到,请尝试更多号码。

    【讨论】:

    • 这在我的情况下不起作用,但我从你的代码中得到了一些提示,所以我会接受答案!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多