1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Pagination

Discussion in 'XenForo Development Discussions' started by Mythotical, Jan 27, 2012.

  1. Mythotical

    Mythotical 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.
     
  2. Bob

    Bob Well-Known Member

    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}" />
     
    Brad L, Hugilore111, Cyb3r and 4 others like this.
  3. Mythotical

    Mythotical Well-Known Member

    Thanks bobster, I found multiple examples, they just all seemed to not work and confused me after awhile. Thankfully it works now.
     
  4. Hugilore111

    Hugilore111 Member

    Bob I do not understand this method
    Code:
    $totalThings = $thingModel->getThingsCount();
     
  5. wang

    wang Well-Known Member

    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.
     
  6. Bob

    Bob Well-Known Member

    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
            '
    );
        }
     
    Steve F, Hugilore111 and wang like this.

Share This Page