Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Pagination class (returns empty string)
  • Hi,

    I'm trying to forge a pagination instance, but when I try to echo this out in the view, it returns an empty string

    Does anyone know what I'm doing incorrectly? 

    Thanks in advance


    Controller:

    $config = array(
    'total_items'    => Model_Yourvoice::count(),
    'per_page'       => 10,
    'uri_segment'    => 3,
    );
    $pagination = Pagination::forge('yourvoicepagination', $config);

    $data['yourvoices'] = Model_Yourvoice::find('all',array('order_by' => array('created_at' => 'desc')), array('rows_offset' => $pagination->offset), array('rows_limit' => $pagination->per_page));

    // we pass the object, it will be rendered when echo'd in the view
    $data['pagination'] = $pagination;
    $this->template->title = "Yourvoices";
    $this->template->content = View::forge('yourvoice/index', $data);


    In the view, var_dump($pagination) returns string(0) ""
    $pagination->render() exits with: Method Fuel\Core\View::__toString() must not throw an exception


  • HarroHarro
    Accepted Answer
    If you pass the object like that, it will be cleaned by the View as per your security settings, and I don't think that will work.

    So either whitelist the pagination class in your main config file ("whitelisted_classes"), or pass it to the view like so:

    $this->template->set_safe('pagination', $pagination);

    Alternatively, don't pass anything, but use this in your view:

    echo Pagination::instance('yourvoicepagination');
  • Thanks for the response,

    I've added the $this->template->set_safe('pagination', $pagination);
    The same issue remained, I've just tried the echo Pagination::instance('yourvoicepagination'); 

    That didn't render anything on the view either, it would seem the pagination instance isn't working, is there a way I can view the output of the pagination variable directly from the controller? i.e var_dump or such like?

    Update:
    After white listing the class, I can now var_dump the $pagination but calling $pagination->render() does not output any HTML



  • HarroHarro
    Accepted Answer
    Which Fuel version are you on?

    Does your app have a custom pagination config file, or do yo use the framework defaults?

    How many pages of output do you have (check the "total_pages" config value in your var_dump). If only one, pagination will not output anything, as there is nothing to paginate.

    If you call $pagination->render(true), it will return a raw array with all variables instead of the rendered HTML, perhaps that will show you more?
  • It's always the simplest solution, the per page value was set higher than the total records!
    Now I just have to figure out why it's displaying all the available records, instead of the "per_page" value of the pagination.

    I imagine this orm query is suspect

    $data['yourvoices'] = Model_Yourvoice::find('all',array('order_by' => array('created_at' => 'desc')), array('rows_offset' => $pagination->offset), array('rows_limit' => $pagination->per_page));

    Edit:
    Replaced with, 

    $data['yourvoices'] = Model_Yourvoice::query()
    ->order_by('created_at', 'DESC')
    ->rows_offset( $pagination->offset )
    ->rows_limit( $pagination->per_page )
    ->get();
    Resolved!


    Thanks for the help!
  • HarroHarro
    Accepted Answer
    You're welcome.

    To be complete, your initial query went wrong because all options have to be in a single array:

    $data['yourvoices'] = Model_Yourvoice::find('all',array(
        'order_by' => array('created_at' => 'desc'),
        'rows_offset' => $pagination->offset,
        'rows_limit' => $pagination->per_page
    );

    would have fixed it too.

Howdy, Stranger!

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

In this Discussion