2

I'm new to EE and I'm having problems using the Pagination Class. I've created a custom module and have the following method in my mod file. The pagination is rendering on the template fine and the URLs look fine (mydomain/series/pagination_test/P24) but the results are not changing as I navigate the pagination.

public function pagination_test() {

    $this->EE->load->library('pagination');
    $pagination = $this->EE->pagination->create();
    $this->EE->TMPL->tagdata = $pagination->prepare(ee()->TMPL->tagdata);
    $tagdata = $this->EE->TMPL->tagdata;
    $results = $this->EE->gt_model->pagination_test_data();

    foreach($results as $row)
    {
        $variables[] = array(
          'alt_title' => $row['alt_title']
          );
    }

    if ($pagination->paginate === TRUE)
    {
        $total_items = count($results);
        $per_page = ee()->TMPL->fetch_param('limit');
        $status = $pagination->build($total_items, $per_page);
    }

    $this->return_data = $this->EE->TMPL->parse_variables( $tagdata, $variables );
    $this->return_data = $pagination->render($this->return_data);

    return $this->return_data;
}
1

The step you're missing is actually filtering your query/dataset by the offset generated by the Pagination class. (Usually this is used inside a database query, but you can also just use it to slice your array.)

if ($pagination->paginate === TRUE)
{
    $total_items = count($results);
    $per_page = ee()->TMPL->fetch_param('limit');
    $pagination->build($total_items, $per_page);

    // The important bit
    $variables = array_slice($variables, $pagination->offset, $per_page);
}
| improve this answer | |
  • If you were doing this within the query instead, would you just pass $pagination->offset in the offset() method of the query builder? – Miura-shi Feb 28 '18 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.