Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Issue with pagination uri_segment
  • Hi,
    I have some trouble when trying to create a simple pagination, using the pagination class

    whene i set the url_segement, ex :

    in the controller : 

    $config = array(
    'pagination_url' => Uri::create( 'posts/all/recent/all_time') ;,
    'total_items'    => DB::count_records('posts'),
    'per_page'       => Config::get('max_listview'),
    'uri_segment'    => 4);
    $pagination = Pagination::forge('mypagination', $config);
    $data['pagination'] = $pagination->render();
    $view->pagination = View::forge('front/listing/pagination',$data,FALSE);

    $data['posts'] = Posts::get($pagination->offset,$pagination->per_page);//model used to retrieve posts
    $view->listview = View::forge('front/listing/listview',$data,FALSE);


    then the generated links look like : posts/all/recent/1 instead of posts/all/recent/all_time/1
    and the pagination class doesn't catche the uri_segement any more!

    whene I use Uri:segment(4) it return nothing.

    thene I increment the uri_segement param  in the pagination config array to 5 :


    $config = array(
    'pagination_url' => Uri::create( 'posts/all/recent/all_time') ;,
    'uri_segment'    => 5
    .....


    now the generated links  are correct : posts/all/recent/all_time/1
    but when I click on the links the pagination seem freezed nothing change, also  the Uri:segment(5)  still return nothing
    I use fuelphp 1.5.3 (latest) and php 5.4
    so what wrong with the pagination!!!
    thanks
  • afaik there is nothing wrong with Pagination. What do you mean by "the pagination seem freezed nothing change"?

    With this URI the uri_segment must indeed be 5, it's the fifth segment that contains the page number.


  • thank for reply

    I've already set the uri_segement to 5.

    I mean by "freezed" that the pagination links does'nt show the page requested, it show always the first one!

    , I actualy switch from segment number to query string, and it work.

    the segement as number does'nt worked for me. it always retirive the wrong count, or override the base url.
  • HarroHarro
    Accepted Answer
    Then you're controller code is wrong, it doesn't seem to pick up the page number from the URL.
    Switching to a query string is a workaround, it doesn't fix the problem.

    The most logical option is to define the segments are controller action parameters. It's easier then using Uri::segment() to get it.

    I think where you go wrong is with your understanding of what a "URI segment" is, for the different FuelPHP components.

    If you consider the URI "posts/all/recent/all_time/1", for a controller (assuming that this is controller "posts", method "action_all"), this URI has 3 segments: "recent", "all_time" and "1". The controller name and method are not part of it, they are stripped from the URI when the request is routed.

    For Pagination however. this URI has 5 segments: "posts", "all", "recent", "all_time" and "1". This is because Pagination sees the entire URL, it has no idea what part of it is routing information, and what part of it are URI parameters.

    So, to fetch the page in the controller use Uri::segment(3), or use:

    public function action_all($select = 'recent', $time = 'all_time', $page = 1)
    {
        // validate the page number
       is_numeric($page) or $page = 1;

       // validate the other params, and use $page to fetch the paginated results
    }
  • thank you.
  • finally i found what cause the problem, it happen when the site is in sub folder like http://localhost/mysite/ and the base_url not set.

    so the pagination allways take the wrong segment because the subfolder was not included in base_url.
    when i created a vhost like http://mysite/ all things works perfect. :D

    thank you again.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion