Pagination - pagenav

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

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


I have in my Controller

Code:
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'];
        }
        else
        {
            $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(
                'PixelExit_AdminCenter_ViewPublic_Index',
                'pe_admin_notepad_index',
                $viewParams
            );   
        }
        else
        {
            // if canViewNotes is false display no permission response
            throw $this->getNoPermissionResponseException();
        }
    }
}

=================
Model

Code:
    public function getLatestNotes($maxNotes = 0)
    {
        $sql = '
            SELECT
                note.*,
                user.*
            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.

Max Notes: string(2) "15"

Page: int(1)

Notepad Notes: array(1) {
[0] => array(33) {
["note_id"] => int(94)
["user_id"] => int(1)
["note_date"] => int(1404081188)
["message"] => string(1) "5"
["username"] => string(5) "Steve"
["email"] => string(17) "**********"
["gender"] => string(0) ""
["custom_title"] => string(0) ""
["language_id"] => int(1)
["style_id"] => int(1)
["timezone"] => string(13) "Europe/London"
["visible"] => int(1)
["user_group_id"] => int(2)
["secondary_group_ids"] => string(3) "3,4"
["display_style_group_id"] => int(3)
["permission_combination_id"] => int(5)
["message_count"] => int(0)
["conversations_unread"] => int(0)
["register_date"] => int(1401420303)
["last_activity"] => int(1404173037)
["trophy_points"] => int(0)
["alerts_unread"] => int(0)
["avatar_date"] => int(1404012555)
["avatar_width"] => int(192)
["avatar_height"] => int(192)
["gravatar"] => string(0) ""
["user_state"] => string(5) "valid"
["is_moderator"] => int(1)
["is_admin"] => int(1)
["is_banned"] => int(0)
["like_count"] => int(0)
["warning_points"] => int(0)
["is_staff"] => int(1)
}
}

totalNotes: int(94)
 
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:
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?
 
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.

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

In your fetch function you are doing this:
PHP:
$sql = $this->limitQueryResults($sql, $maxNotes);

$maxNotes in this case is the limit. So your query is basically doing:

Code:
SELECT * FROM table LIMIT 15

For page 2, for example, what you really want is:

Code:
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:

PHP:
$fetchOptions['page'] = 1;
$fetchOptions['perPage'] = 15;

That then allows you to use this helper (works in any Model):
PHP:
$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:

PHP:
$sql = $this->limitQueryResults($sql, $limitOptions['limit'], $limitOptions['offset']);
 
Back
Top Bottom