Get Photoshop help and share your work at

Bookmark and Share

Adding Pagination to a phpBB Page

Adding Pagination to a phpBB Page

Postby Thoul » September 11th 2003, 3:54 am

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: Select all
// 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: Select all
$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.

$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.

$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.


$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: Select all

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: Select all
$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). '&';

	'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.

$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.

$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.

$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: Select all
<table width="100%" cellspacing="0" cellpadding="0" border="0">
	<td align="right">{PAGINATION}</td>

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: Select all
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! :D
User avatar
Posts: 18551
Joined: July 30th 2002, 11:30 am
Location: USA

Return to phpBB 2: Customizing Your phpBB

Who is online

Users browsing this forum: No registered users and 0 guests