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

Pagination - pagenav

Discussion in 'XenForo Development Discussions' started by Steve F, Jul 1, 2014.

  1. Steve F

    Steve F Well-Known Member

    I am trying to understand the pagination but having a difficult time getting anywhere. If someone could get me pointed in the right direction I would appreciate it.

    No page nav is being displayed in the source

    <xen:pagenav link="notepad" page="{$page}" perpage="{$maxNotes}" total="{$totalNotes}" />

    I have in my Controller

    class PixelExit_AdminCenter_ControllerPublic_Index extends XenForo_ControllerPublic_Abstract
    public function actionIndex()
            // fetch the Scratchpad_Model_Note class
            $noteModel = $this->_getNoteModel();
            // get the maximum number of notes to fetch
            $maxNotes = XenForo_Application::get('options')->peNotepadMaxNotes;
            $page = max(1, $this->_input->filterSingle('page', XenForo_Input::UINT));
            $notepadNotes = $noteModel->getLatestNotes(array('perPage' => $maxNotes, 'page' => $page));
            $totalNotes = $noteModel->getAllNotes($maxNotes);
            // get the most recent notes
            $notes = $noteModel->getLatestNotes($maxNotes);
            // read the date of the most recent note
            if ($notes)
                $date = $notes[0]['note_date'];
                $date = 0;
            // put the data into an array to be passed to the view so the template can use it
            $viewParams = array(
                'notes' => $notes,
                'date' => $date,
                'page' => $page,
                'notepadNotes' => $notepadNotes,
                'totalNotes' => $totalNotes,
            Zend_Debug::dump($maxNotes, 'Max Notes: ');
            Zend_Debug::dump($page, 'Page: ');
            Zend_Debug::dump($notepadNotes, 'Notepad Notes: ');
            Zend_Debug::dump($totalNotes, 'totalNotes: ');
            // get permissions   
            $permissions = XenForo_Visitor::getInstance()->getPermissions();
            $canCreateNotes = XenForo_Permission::hasPermission($permissions, "peAdminCenter", "createNotes");
            $canViewNotes = XenForo_Permission::hasPermission($permissions, 'peAdminCenter', 'viewNotes');
            // if canViewNotes is true send to template
            if ($canViewNotes)
                // return a View (PixelExit_AdminCenter_ViewPublic_Index) using template 'pe_admin_notepad_index'
                return $this->responseView(
                // if canViewNotes is false display no permission response
                throw $this->getNoPermissionResponseException();

        public function getLatestNotes($maxNotes = 0)
            $sql = '
                FROM pe_admin_notepad_note AS note
                LEFT JOIN xf_user AS user ON
                    (user.user_id = note.user_id)
                ORDER BY note_date DESC
            // ensure we have a meaningful value for $maxNotes
            if ($maxNotes = max($maxNotes, 0))
                // build our LIMIT (or equivalent) clause
                $sql = $this->limitQueryResults($sql, $maxNotes);
            return $this->_getDb()->fetchAll($sql);
         * Fetches all notes together with user info.
         * @return array
        public function getAllNotes($totalNotes)
            return $this->_getDb()->fetchOne('
                SELECT COUNT(*)
                FROM pe_admin_notepad_note AS notepad_note
                ORDER BY notepad_note.note_date DESC

    I do get a dump with what seems the right info.

  2. Steve F

    Steve F Well-Known Member

    I was able to get this partially working. I never passed $maxNotes to the template. Pag nav now shows but the links always redirect to the first page.
    Last edited: Jul 1, 2014
  3. Chris D

    Chris D XenForo Developer Staff Member

    On the assumption that the current code is exactly as it is now, aside from the $maxNotes parameter being passed to the template properly now, I can't see what would be wrong.

    Are the URLs properly formed in the pagenav? If you click on page 2, for example, is it that it takes you to the url, notepad?page=2 or does it actually take you to just notepad or smilar?
  4. Steve F

    Steve F Well-Known Member

    Almost positive it showed the correct page number in the URL and the correct page number selected in the page nav. Not at my PC at the moment but will double check when I get back.

  5. Valhalla

    Valhalla Well-Known Member

    Could be the offset or limit that's the problem.

    Haven't looked thoroughly though.
    Chris D likes this.
  6. Chris D

    Chris D XenForo Developer Staff Member

    @Valhalla is right. I just actually read your code properly (it helps :p)

    In your fetch function you are doing this:
    $sql $this->limitQueryResults($sql$maxNotes);
    $maxNotes in this case is the limit. So your query is basically doing:

    SELECT * FROM table LIMIT 15
    For page 2, for example, what you really want is:

    SELECT * FROM table LIMIT 15 OFFSET 15
    There's a helper that will take your page and perPage (or maxNotes) value and turn it into the correct limit and offset. Instead of passing an int value of $maxNotes to your fetch function, instead pass an array containing page and perPage. Formatted as such as an example:

    $fetchOptions['page'] = 1;
    $fetchOptions['perPage'] = 15;
    That then allows you to use this helper (works in any Model):
    $limitOptions $this->prepareLimitFetchOptions($fetchOptions);
    You then have $limitOptions which is an array that contains a limit and offset key and can be used like this:

    $sql $this->limitQueryResults($sql$limitOptions['limit'], $limitOptions['offset']);

Share This Page