• Working example of displaying latest posts on external pages in phpBB3
  • Working example of displaying latest posts on external pages in phpBB3
    Written by battye
    Sun Nov 22, 2009 8:01 am

    This article is a follow-on from the blog post I made at phpBB.com not too long ago. I recommend reading that post first before reading this, as all of the information you need might be in that post already.

    The blog post assumed some prior knowledge of coding, so in this article I will make it clear enough that anyone can implement the code. I will also give download links to full files so you can simply "drag and drop" to your website.

    It seems that most people are interested in example 4 from the blog post "Display posts from anywhere". This means that you can display the latest X number of posts from anywhere in your forum (no restriction on forum ids, but still subject to user read permissions).

    The first thing you need is open Notepad or another basic text editor. If you want to see what is happening step-by-step, keep reading. If you want to download the entire script, go straight to the end of the page.

    Step by step

    Copy and paste this code in the top of the file (this is the standard phpBB header, it includes all of the required files in order for the script to run properly):

    * home.php
    * Description: example file for displaying latest posts and topics
    * by battye (for phpBB.com MOD Team)
    * September 29, 2009

    define('IN_PHPBB', true);
    $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
    $phpEx = substr(strrchr(__FILE__, '.'), 1);
    include($phpbb_root_path . 'common.' . $phpEx);
    include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
    include($phpbb_root_path . 'includes/functions_display.' . $phpEx);

    // Start session management

    You might need to edit this line:

    $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';

    See the './'? What this means is that phpBB is located in the same directory as the script to display the latest X posts. Suppose you want the latest X posts to be shown in a file called "latest.php" and your website is "http://www.example.com". This setting assumes that the forum is located in the home directory for the website (ie. "http://www.example.com/viewtopic.php"). If it is located in another directory, say "phpBB3" (ie. "http://www.example.com/phpBB3/viewtopic.php") then you will need to edit the './' to './phpBB3/'. See, the directory name of the directory that phpBB3 is located in follows the first forward slash and has another forward slash following it.

    Now, paste the following code after the header block: (note how the create_where_clauses() function shown in the blog post is not needed like it is for examples 1, 2 and 3):

    Replace the 5 in $search_limit to the number of posts you want to display. Currently it will display the latest 5 posts made on the forum.

    See the use of the bbCode functions? Coupled with the inclusion of bbcode.php in the header this means that the end result will have all bbCode parsed so it will look exactly like it does when you read a post.

    $search_limit = 5;

    $posts_ary = array(
    'SELECT' => 'p.*, t.*, u.username, u.user_colour',

    'FROM' => array(
    POSTS_TABLE => 'p',

    'LEFT_JOIN' => array(
    'FROM' => array(USERS_TABLE => 'u'),
    'ON' => 'u.user_id = p.poster_id'
    'FROM' => array(TOPICS_TABLE => 't'),
    'ON' => 'p.topic_id = t.topic_id'

    'WHERE' => $db->sql_in_set('t.forum_id', array_keys($auth->acl_getf('f_read', true))) . '
    AND t.topic_status <> ' . ITEM_MOVED . '
    AND t.topic_approved = 1',

    'ORDER_BY' => 'p.post_id DESC',

    $posts = $db->sql_build_query('SELECT', $posts_ary);

    $posts_result = $db->sql_query_limit($posts, $search_limit);

    while( $posts_row = $db->sql_fetchrow($posts_result) )
    $topic_title = $posts_row['topic_title'];
    $post_author = get_username_string('full', $posts_row['poster_id'], $posts_row['username'], $posts_row['user_colour']);
    $post_date = $user->format_date($posts_row['post_time']);
    $post_link = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "p=" . $posts_row['post_id'] . "#p" . $posts_row['post_id']);

    $post_text = nl2br($posts_row['post_text']);

    $bbcode = new bbcode(base64_encode($bbcode_bitfield));
    $bbcode->bbcode_second_pass($post_text, $posts_row['bbcode_uid'], $posts_row['bbcode_bitfield']);

    $post_text = smiley_text($post_text);

    $template->assign_block_vars('announcements', array(
    'TOPIC_TITLE' => censor_text($topic_title),
    'POST_AUTHOR' => $post_author,
    'POST_DATE' => $post_date,
    'POST_LINK' => $post_link,
    'POST_TEXT' => censor_text($post_text),

    Finally, paste this footer code into the text document. Replace the 'External page' part with what you want the title of the page to be. Likewise, change 'external_body.html' to the desired name of the template file if you wish. If you are not confident changing code, there is no reason to change these.

    page_header('External page');

    'body' => 'external_body.html'


    Now save the file (for instance, as "home.php").

    Now open another text document, and paste this into it (this is the corresponding template file, notice how the variables - the part inside the curly brackets - match what was declared in the $template->assign_block_vars array in the php script):

    <!-- BEGIN announcements -->
    Title: {announcements.TOPIC_TITLE}<br />
    Post author: {announcements.POST_AUTHOR}<br />
    Post date: {announcements.POST_DATE}<br />
    Last post text: {announcements.POST_TEXT}<br />
    Post link: {announcements.POST_LINK}
    <!-- END announcements -->

    This is very basic, I recommend styling it a bit more. In its current state it would look like this:


    Use CSS and HTML to make it look nice, play around with the values... as long as you don't move the variables between curly brackets ({announcements.TOPIC_TITLE}, etc) outside of the loop (between the BEGIN and END) then it will run correctly. There is a good article about template syntax at the phpBB wiki: http://wiki.phpbb.com/Template_Syntax

    Save that file as external_body.html (or if you changed that name, as mentioned above, to whatever you changed the name to). To see how to upload them, go down to the end of the page.

    Download examples

    To download the entire script, the zip file is at http://www.cmxmods.net/downloads/Miscel ... ywhere.zip

    Uploading the files

    Using your favourite FTP (file transfer protocol) client - upload home.php to your desired location, and external_body.html to styles/prosilver/templates/

    Now in your browser, go to home.php (so if you uploaded it to the root directory: "http://www.yourdomainhere.com/home.php", or if it is in the phpBB3 directory: "http://www.yourdomainhere.com/home.php", etc - replace yourdomainhere with whatever your domain name actually is) and you should see the latest X number of posts.

    You can rename home.php to anything you want as long as it ends in .php, and likewise external_body.html can be named anything you want as long as it corresponds to what you entered in the footer code (explained above).

    Remember, if you edit the .html file then when you re-upload it you will need to purge the template cache from the phpBB3 admin panel to the changes are displayed.

    I hope this article has been helpful.

    Useful links:
    - http://blog.phpbb.com/2009/11/09/how-to ... nal-pages/
    - http://wiki.phpbb.com/Template_Syntax
    - http://wiki.phpbb.com/Displaying_posts_ ... rnal_pages
    - http://wiki.phpbb.com/
  • Article search