phpBBHacks.com - Adding Pagination to a phpBB Page
House M.D. fans unite at DrGregHouse.com
StatsForums Home   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in
FAQFAQ   SearchSearch   MemberlistMemberlist   TutorialsTutorials   ContactContact Us
Add Us:     MySpace     Facebook     StumbleUpon
Username:    Password:
Remember Me?    
Adding Pagination to a phpBB Page
BlinkList
del.icio.us
Furl
linkaGoGo
reddit
Simpy
Mister Wong
Yahoo! My Web

Post new topic   Reply to topic    phpBBHacks.com Support Forums Forum Index -> phpBB 2: Customizing Your phpBB
 See a User Guidelines violation? Please contact us.
Author Message

Thoul
VIP

Joined: 30 Jul 2002
Posts: 17676
Location: USA

PostPosted: September 11th 2003, 2:54 am    Post subject: Adding Pagination to a phpBB Page Reply with quote

As you probably know, phpBB has the ability to display information across many different pages. Forums, topics, search results and the member list are all capable of this when needed. Sometimes it is useful to add this pagination feature to other pages that display a great deal of information. With a little bit of work, you can do this on any phpBB enabled page. If your page is not phpBB enabled, have a look at AJ Quick's Dynamic Sites with phpBB Tutorial.

To add phpBB's pagination to a page, there are four major steps we must complete.

  1. Setting Up Basic Variables
  2. Editing an Existing SQL Query
  3. Adding Pagination Output Code
  4. Adding Pagination Output to a Template File


Step #1 - Setting Up Basic Variables

To begin, open the corresponding *.php file for your page in your favorite text editing program. In this step, we will add some setup variables for pagination near the top of the page's code. It is important that we do this to prevent cross site scripting issues.

First, look in your file for the following code:

Code:

//
// End session management
//


We will add the code for new setup variables, as shown below, after this code. Some of this code may already been in your file; if so, you can leave it out when adding in this step. If your pagination does not work when we are finished with the tutorial, you may wish to come back to this step and add that code after all. In that case, change $start to some other name, such as $pag_start.

Code:

$start = ( isset($HTTP_GET_VARS['start']) ) ? intval($HTTP_GET_VARS['start']) : 0;

$pagination = '&';
$total_pag_items = 1;


Step #2 - Editing an Existing SQL Query

Somewhere in the code for your page there is an SQL query that is used to get all the data displayed on the page from the database. You need to find this query and edit it to limit the amount of data displayed on the page. The query will look different on every page, but you should be able to find it by searching for the word "SELECT" in the code.

Here is a general SQL query that phpBB might use to get the settings of all users other than guests. I will show you how to limit this query for use with pagination, and you can adapt those changes to the SQL query in your page.

Quote:

$sql = 'SELECT * FROM ' . USERS_TABLE . ' WHERE user_id <> ' . ANONYMOUS;


To prevent the query from getting too much data from the database, we'll add a LIMIT command to the end of the query. The query, with this LIMIT command added, is displayed below. The added code is in blue. Using $start tells the query to get data beginning at the end of the last page, while $board_config['topics_per_page'] stops the query from going beyond a set number of database entries.

Quote:

$sql = 'SELECT * FROM ' . USERS_TABLE . ' WHERE user_id <> ' . ANONYMOUS . ' LIMIT ' . $start . ', ' . $board_config['topics_per_page'];


Here is another example, which might get the text of posts on your forum. It is followed by the limited query.

Quote:

$sql = 'SELECT * FROM ' . POSTS_TEXT_TABLE;


Quote:

$sql = 'SELECT * FROM ' . POSTS_TEXT_TABLE . ' LIMIT ' . $start . ', ' . $board_config['topics_per_page'];


Step #3 - Adding Pagination Output Code

Now, we will add the code that actually creates the page number text and links.

First, let us find the place to add the new code. There are several places you could add it, but for the sake of simplicity, find the line in the file similar to this one. The line may be slightly different in your file.

Code:

$template->pparse('body');


We'll add the new code before this line. You'll need to edit some parts of the new code, but we'll get to that in a moment. Here is the new code:

Code:

$sql = 'SELECT count(*) AS total
   FROM ' . USERS_TABLE . ' WHERE user_id <> ' . ANONYMOUS;

if ( !($result = $db->sql_query($sql)) )
{
   message_die(GENERAL_ERROR, 'Error getting total', '', __LINE__, __FILE__, $sql);
}

if ( $total = $db->sql_fetchrow($result) )
{
   $total_pag_items = $total['total'];
   $pagination = generate_pagination("memberlist.php?$mode=$mode&order=$sort_order", $total_pag_items, $board_config['topics_per_page'], $start). '&';
}

$template->assign_vars(array(
   'PAGINATION' => $pagination,
   'PAGE_NUMBER' => sprintf($lang['Page_of'], ( floor( $start / $board_config['topics_per_page'] ) + 1 ), ceil( $total_pag_items / $board_config['topics_per_page'] ))
));


Now, as mentioned before, you need to edit some of that code block. Let's look at the SQL query in this code. Notice that everything between USERS_TABLE and the semi-colon at the end of the line is in red. You need to replace this part of the original query with the corresponding part of the SQL query you edited in Step 2.

Quote:

$sql = 'SELECT count(*) AS total FROM ' . USERS_TABLE . ' WHERE user_id <> ' . ANONYMOUS;


Let's say, for example, that your page contained the second example shown in Step 2. In this case, you would edit the SQL query here to look like this example.

Quote:

$sql = 'SELECT count(*) AS total FROM ' . POSTS_TEXT_TABLE;


Also in this block of code, you need to edit the URL passed to the pagination function, shown in blue below. This should be changed to the URL of your page. The pagination details will be added to this URL.

Quote:

$pagination = generate_pagination("memberlist.php?mode=$mode", $total_pag_items, $board_config['topics_per_page'], $start). '&';


Step #4 - Adding Pagination Output to a Template File

In the final step, we'll add the output into your page's template. Open the *.tpl file for your page and find the place you'd like the pagination details to appear. You can add the HTML shown in this example to that location. {PAGE_NUMBER} will be replaced with the text "Page 1 of 3," while {PAGINATION} will be replaced with the links to other pages.

Code:

<table width="100%" cellspacing="0" cellpadding="0" border="0">
  <tr>
   <td>{PAGE_NUMBER}</td>
   <td align="right">{PAGINATION}</td>
  </tr>
</table>


If you're not using a template on your page, you can also use lines similar to the ones below to print out the same information.

Code:

echo sprintf($lang['Page_of'], ( floor( $start / $board_config['topics_per_page'] ) + 1 ), ceil( $total_pag_items / $board_config['topics_per_page'] ));
echo $pagination;


You should now have pagination links in your page. Enjoy!
_________________
Phantasy Star: The Fringes of Algo

Install, remove, or upgrade SQL with Advanced DB Update Generator! Now with phpBB 3 Support!

My phpBB Books, Hacks, and Other Works «·» 70+ Listings @ phpBBHacks.com
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    phpBBHacks.com Support Forums Forum Index -> phpBB 2: Customizing Your phpBB All times are GMT - 6 Hours
Page 1 of 1
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



Links: Big Message Boards - Free JavaScript - phpBB2 - phpbb styles - Suporte phpBB - phpBB.it - phpBB Česky - phpBB Turkiye - phpBBArabia.com - phpBB-fr.com - Romanian phpBB online community - phpBB-TW.net - phpBBservice.nl - phpBB Brasil

Network: iFroggy Network Blog - iFroggy Hosting - SportsForums.net - KarateForums.com - YanksBlog.com - DeveloperCube - Managing Online Forums - ManagingCommunities.com - CommunityAdmins.com - PhotoshopForums.com - MicrosoftBlog.com - DrGregHouse.com - Bad Boy Blog - BadBoyForums.com - SodaRatings.com - Patrick O'Keefe

< Advertising - Contact Us - Staff - User Guidelines >

Copyright © 2001-2008. iFroggy Network, phpBBHacks.com. All Rights Reserved. Privacy Policy. We Support phpBBHacks.com (of course!).
Powered by phpBB © phpBB Group. phpBB SEO. Hosted by 100MegsWebHosting. We are in no way affiliated with the phpBB Group. phpBB is copyright to the phpBB Group.