1
votes

I'm new to Codeigniter pagination so i'm not able to configure it properly, that why i need your help guys.

This is my code so far

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

And this is the result if i visit http://mypage.si/rest/api/nearbay/nearbay result And if i click on any page number i get blank page and my url changes to http://mypage.si/rest/api/nearbay/nearbay? enter image description here

So my question are. How to get pagination working? How to properly config pagination so i will be able to fetch results via ?page and ?per_page values trough url.. My final links should look like this http://mypage.si/rest/api/nearbay/nearbay?page=1&per_page=20

If you need any additional informations please let me know and i will provide... Thank you!

1

1 Answers

1
votes

Here is answer, Hope it will help and url should be like this: 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();
}

For check uri_segment check the number. Currently is 6 try further number if you are not getting.