Search code examples
codeignitercodeigniter-3

Need understanding $config["uri_segment"] related code in CodeIgniter


Hey CodeIgniter developers I am new in codeIgniter please see my code related to pagination. Pagination is working fine. I just need your help to understand few lines of code, please see the commented lines in code where I just need your help to understand it.

public function example1() {
        $config = array();
        $config["base_url"] = base_url() . "welcome/example1";
        $config["total_rows"] = $this->services->record_count();
        $config["per_page"] = 10;
        $config["uri_segment"] = 3; // Need help on this line
        $config["next_link"] = '>';
        $config["prev_link"] = '<';

        $this->pagination->initialize($config);
        // Need help on this if condition blocks
        if ($this->uri->segment(3)) {
            $page = ($this->uri->segment(3));
        } else {
            $page = 1;
        }
        $data["results"] = $this->services->fetchServicesByPagination($config["per_page"], $page);
        $data["links"] = $this->pagination->create_links();

        $this->load->view("example1", $data);
    }

Solution

  • Agree With Javier Larroulet

    From the Codeigniter documentation $config['uri_segment'] defines what URI segment will contain the page number. It defaults to 3, but you may use another segment if you need. The $this->uri->segment(3) condition on the if clause is checking if the URI Segment number 3 (first after the method name) is set or not. If it is set, use its value as page number, otherwise, default to page 1. Reference