Having segments after page number in CodeIgniter

1.8k views Asked by At

I am wondering if CodeIgniter allows segments after the page number and what is the best way to do this?

$config['base_url'] = '/controller/view/pg/';

I need my paging to pass this also:

/controller/view/pg/1/v/l/rpp/20 ... etc

I have ran into multiple problems because I am using $this->uri->uri_to_assoc(n) because of the number of segments I will be needing...

I need to be able to pass values to each page, and at this point I'm not sure how to do it.

Do you think the best way to do this is to always move the paging to the end of all other segments? It seems that this leads to problems also.

2

There are 2 answers

3
Jakub On BEST ANSWER

Jason, you are making the problem yourself simply because you loose track of what segements are part of the controller/method, and which are your segments that you find relevant.

I would first of all tell you to stick to one method say to append it to the end (this is from the users perspective in the uri, not your route config):

/view/page/1233/name/blue-skies/pg/20

The above format would mean something like this on the backend: /view/ is the controller, page is your method in the controller, then you would use $this->uri->uri_to_assoc(4) (4th element, being name to start).

That way you correctly capture your page number 1233 and then all relevant data to it.

As a suggestion, I would caution against using unreadable variables, it leads to confusion and does NOT make your site url SEO friendly (who knows what /v/p/123/v/l/20 means in the end?).

Always user your $this->output->profiler(TRUE);, if you are having trouble with uri routing. Don't mess with your route config unless you REALLY need to, this may lead to confusion, which complicates your testing.

EDIT

I created confusion because based on your question you could interpret it as a routing issue from the position of the config.php file OR the pagination class. I took it from the first approach.

To clarify you need to simply stick to a clean url method, if you use uri_to_assoc, thats fine. But just don't loose track of your page number for the pagination.

You can solve this by making the page number the last element in your uri

Last: /view/page/1233/name/blue-skies/user/12/20

Where the 20 is the page number that is generated by pagination, the other are segments you use for whatever.

You would set your $config['uri_segment'] = 6; in this case, and your $config['base_url'] = '/view/page/1233/'.$this->uri->assoc_to_uri($uri_segments);

Where:

$uri_segments = array(
 'name' => 'blue-skies',
 'user' => '12');

IF

It is unknown how many segments you have (say a dynamic $uri_segments array), use $this->uri->total_segments() to count total segments, then your pagination one is the +1 to that (last).

0
Goce Ribeski On

Yes, that can be done.

The way to do it is in pagination configuration array, 'uri_segment' should be variable:

$config['uri_segment'] = $segment_offset;

$segment_offset can be calculated by looking for '/pg/'(from your example) in the URI.

Example code:

  //for pagination      
  $start = 0;
  $limit_per_page = 100;


    //URI to acoc array:
    $uri_array = $this->uri->uri_to_assoc(4);
    /*
     Array
        (
            [page_links] => 0
        )
     */

    //Take the number of pagination segment from uri; return URI number by its name 
    $segment_offset  = 0;

    foreach($uri_array as $key=>$value){
      $segment_offset++;
      if('page_links' == $key){
          //segment founded
          break;
      }
    }

    //calculate actual place or pagination number
    //$segment_offset = $segment_offset + uri_to_assoc(**4**) + **1** place after the segmwnt 'page_links' is actual number for pagination;
    $segment_offset = $segment_offset + 4 + 1;


  //DB query can be here

  // ///////////////////////////////////////////////////////////////////////
  // NOTE: Set up the paging links. Just remove this if you don't need it,
  // NOTE: ...but you must remember to change the views too.
  // ///////////////////////////////////////////////////////////////////////
  $this->load->library('pagination');
  $this->load->helper('url');


  $config['base_url']     = site_url('controller1/browse/pg/'.$pg.'/other_segment/etc..');
  $config['total_rows']   = xxx;
  $config['per_page']     = $limit_per_page;

  //$config['uri_segment'] = xx;
  //now that can be variable, and not just on the end of the URI 
  $config['uri_segment'] = $segment_offset;


  $config['first_url'] = '0';

  $config['num_links'] = 4;

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

  $the_results['page_links'] = $this->pagination->create_links();