Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Question on Orm and Pagination
  • Using FuelPHP 1.8 Dev

    Can you use pagination with a template? This is in my Admin Module.

    passing the pagination through the template content?

        $this->template->content   = \View::forge('admin/pages/index', $data)

    I cannot get it to work at all with  'uri_segment'  => 3, but if I set it to 'uri_segment'    => 'page',

    I can get the second page but not the first page, it's just blank.

    CODE:

    public function action_index()
    {
    // ----- Start of Pagination -----

    $config = array(
    'pagination_url' => null, //'http://nikita.dev/admin/pages/index',
    'total_items'    => Model_Page::count(),
    'per_page'       => 2,
    'uri_segment'    => 'page',
    );

    $pagination = \Pagination::forge('pagepagination', $config);

    $data['example_data'] = Model_Page::query()
    ->rows_limit($pagination->offset)
    ->rows_offset($pagination->per_page)
    ->get();

    // we pass the object, it will be rendered when echoed in the view
    $data['pagination'] = $pagination;

    // ----- End of Pagination -----

    //$data['pages'] = Model_Page::find('all');

    $this->template->title     = 'Pages';
    $this->template->content   = \View::forge('admin/pages/index', $data);

    }

  • Off course you can.

    Most common error is that the URI segments are counted wrong. If your page URI is /admin/pages/index, and you want the second page to be /admin/pages/index/2, the URI segment number is 4.
  • Hi Harro,

    If I set it to 4 I'am getting this error now

    ErrorException [ Fatal Error ]:
    Method Fuel\Core\Pagination::__toString() must not throw an exception

    APPPATH/modules/admin/views/admin/pages/index.php @ line 0

  • Can you change your template from

    echo $pagination; // or some other var...

    to

    echo $pagination->render();

    so you can see which exception is thown?
  • RuntimeException [ Error ]:
    Not enough segments in the URI, impossible to insert the page number

    COREPATH/classes/pagination.php @ line 592

    587                $segs = isset($url['path']) ? explode('/', trim($url['path'], '/')) : array();
    588
    589                // do we have enough segments to insert? we can't fill in any blanks...
    590                if (count($segs) < $this->config['uri_segment'] - 1)
    591                {
    592                    throw new \RuntimeException("Not enough segments in the URI, impossible to insert the page number");
    593                }
    594
    595                // replace the selected segment with the page placeholder
    596                $segs[$this->config['uri_segment'] - 1 + $seg_offset] = '{page}';
    597                $url['path'] = '/'.implode('/', $segs);
  • On http://nikita.dev/admin/pages/index ?

    Of did you leave the "" in (which is absolutely not correct)?
  • Leave the  "" in where?

    No it is set to null I remarked out the http part

    'base_url'  => null,  // config.php

    'pagination_url' => null, 

    And my admin route is like this in app/conifg

    'admin/(:any)' => 'admin/admin/$1',

  • Hi Harro,

    I got it to work using 'uri_segment'    => 'page',

    But it still will not work using the 'uri_segment' => 3, or 4,

    Problem was had old 1.8 dev docs

    ->rows_limit($pagination->offset)
    ->rows_offset($pagination->per_page)

    Replaced with

    ->rows_offset($pagination->offset)
    ->rows_limit($pagination->per_page)

    Why it is not working with 'uri_segment' => 3, or 4, I have no idea.

    If I set it to 3 it gives me a listing but then it errors clicking on 2 or ->

    I would prefer to use segments and not page queries but they are working.

  • If you don't pas a pagination_url, it will be set to Uri::main(), which is the URI that is used to get the page containing the pagination.

    So you need to count the number of segments on that (excluding hostname, including controller, method and parameters). The uri_segment for pagination is that count + 1.

    If you say that with 3 it works, but pagination gives errors: what errors? What link is exactly generated when you look at 2 or -> ?

    You're going wrong in your code somewhere, but I can't figure out where. Pagination is used heavily on a daily basis, and most of our apps run on 1.8/dev, so I'm pretty sure pagination isn't broken.
  • Hi Harro,

    This works fine now using segment and page except for below:

    $config = array(
    'total_items'    => Model_Page::count(),
    'per_page'       => 2,
    'uri_segment'    => 4, //'page',
    );

    Ok it is working for both but there is a error with this:
    It seems that the null is not finding the uri

    $config = array(
    'pagination_url' => null, //'http://nikita.dev/admin/pages/index/',
    'total_items'    => Model_Page::count(),
    'per_page'       => 2,
    'uri_segment'    => 4,
    );

    ERROR:

    ErrorException [ Fatal Error ]:
    Method Fuel\Core\Pagination::__toString() must not throw an exception

    APPPATH/modules/admin/views/admin/pages/index.php @ line 0

  • HarroHarro
    Accepted Answer
    What is your URI?

    Do you really type in http://nikita.dev/admin/pages/index/ in your browser, or something else? If you type for example http://nikita.dev/admin/pages/, because the index part is default, null will not work, since Uri::main() will return 2 segments instead of 3.

    Which means that if you specify 4 as uri_segment, you get the exception "
    Not enough segments in the URI, impossible to insert the page number", because segment 4 containing the page number can not be added because there is no segment 3.

Howdy, Stranger!

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

In this Discussion