MyBB Community Forums

Full Version: Pages in plugin
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a custom page in the Admin CP, but a part of it is searching for stuff in the database. How can i easily make it appear in pages (like the search returns 150 items, and i want 15 in each page max)?

One of the problems was that if i'd switch the page, the plugin would need to search the items every time (it would be better if you search for them once and when you switch pages the results carry over from page to page).
Then store the results somewhere, like the MyBB search does.
Well okay, but what about pagination then? There are a lot of rules that go into pagination (like if there are more than X pages, add a ... before the last link or if you are close to the last one, remove the last page link).
multipage() does that for you somewhat
I had to write pagination code for my ACP module.

// Pagination stuff
// Will MyBB do this for me? Too tired to find out
$user_count = $db->fetch_array( $db->query( 'SELECT COUNT(*) AS `count` FROM `'.TABLE_PREFIX.'users` WHERE '.$where.';' ) );
$user_count = $user_count[ 'count' ];
$per_page = 42; // I suppose you could change this if you wanted to, but it's a nice even number and works well with the table layout
$window = 3; // Used for generation of pagination links below
$page_count = ceil( $user_count / $per_page );
$page_current = 1;
if ( !empty( $mybb->input[ 'page' ] ) && is_numeric( $mybb->input[ 'page' ] ) )
{
    $page_current = intval( $mybb->input[ 'page' ] );
}
// Page validation stuff
if ( $page_current > $page_count )
{
    $page_current = 1;
}
if ( $page_current < 1 )
{
    $page_current = 1;
}
$offset = ( ( $page_current - 1 ) * $per_page );
// Get all the users
$user_query = $db->query( 'SELECT `uid`,`username` FROM '.TABLE_PREFIX.'users` WHERE '.$where.' ORDER BY `uid` DESC LIMIT '.$offset.','.$per_page.';' );

/* Output here */

// Pagination links
echo '<div class="pagination"><span class="pages">'.$lang->pages.': </span>';
if ( $page_current > 1 )
{
    echo '<a href="index.php?page=1" class="pagination_first">&laquo '.$lang->themepeek_first.'</a> ';
    $page_previous = $page_current - 1;
    echo '<a href="index.php?page='.$page_previous.'" class="pagination_previous">'.$lang->previous.'</a> ';
}
for ( $i = ( $page_current - $window ); $i < ( ( $page_current + $window ) + 1 ); $i++ )
{
    if ( ( $i > 0 ) && ( $i <= $page_count ) )
    {
        if ( $i == $page_current )
        {
            echo ' <span class="pagination_current">'.$i.'</span> ';
        }
        else
        {
            echo ' <a href="index.php?page='.$i.'">'.$i.'</a> ';
        }
    }
}
if ( $page_current != $page_count )
{
    $page_next = $page_current + 1;
    echo ' <a href="index.php?page='.$page_next.'">'.$lang->next.'</a> ';
    echo ' <a href="index.php?page='.$page_count.'" class="pagination_last">'.$lang->themepeek_last.' &raquo;</a> ';
}
echo '</div>';

You can use this as a starting point.