Pagination

Cupara

Well-known member
I know there are a few threads out there, I have read them all. I have downloaded mods and attempted to recreate a page nav for a custom mod. The problem is I am not getting this at all.

Could someone please give me some example of what to do to paginate my results? Just use any basic example. BTW, please don't point out files, I have been through that a million times and it just confuses me considering the means to paginate is completely different than what I'm used to with other forum software.
 
Its really not that complicated at all...

in your Things Controller

PHP:
$thingsPerPage = XenForo_Application::get('options')->thingsPerPage;
$page = max(1, $this->_input->filterSingle('page', XenForo_Input::UINT));
$things = $thingModel->getThings(array('perPage' => $thingsPerPage, 'page' => $page));
$totalThings = $thingModel->getThingsCount();


in your "thing" model

PHP:
public function getThings(array $fetchOptions)
{
    $limitOptions = $this->prepareLimitFetchOptions($fetchOptions);

    return $this->_getDb()->fetchAll($this->limitQueryResults('
        SELECT thing.*
        FROM xf_thing_table AS thing
        ORDER BY thing.date_create DESC
        ', $limitOptions['limit'], $limitOptions['offset']
    ));
}

PHP:
 public function getThingsCount()
{
    return $this->_getDb()->fetchOne('
        SELECT COUNT(*)
        FROM xf_thing_table
    ');
}

Then you pass in the above 4 variables (and others that you have in your controller) to your template and use the following template syntax for the "NAV" part. The $things variable you use the standard foreach loop to spit out the data.

Code:
<xen:pagenav link="things" page="{$page}" perpage="{$thingsPerPage}" total="{$totalThings}" />
 
Thanks bobster, I found multiple examples, they just all seemed to not work and confused me after awhile. Thankfully it works now.
 
Bob I do not understand this method
Code:
$totalThings = $thingModel->getThingsCount();

Looking at the date, this is 4 years old thread. I think it would be better if you started your own thread rather then necro posting.

Anyways, the code posted above, is the call to a function that counts things in your code. You must register $totalThings variable so you can use it in your template for the pagination, or to display the count of total stuff.
 
At the most basic level (which is where you should start), its just fetching a total count of the things.

PHP:
    public function getThingsCount()
    {
        return $this->_getDb()->fetchOne('
            SELECT COUNT(*)
            FROM xf_thing_table
        ');
    }
 
Top Bottom