Pull phpBB2 forum posts (basically a simple CMS)

phpBB2 is currently one of the most popular bulletin boards on the internet (yes, this statement is old, but the post is a repost of old content from my previous website).

Forum Powered Website

A forum is the most popular section of many websites so I was looking for a way to drive the website content from the forum. In the end I decided to write the following code to get topics from a forum which I could then display on other pages.

/*
 * PHPBB2 extension to get posts from a forum.  This allows you to power a news sections of
 * a website by posting in the forum.  Any posts in reply to the topic are classed as
 * comments.
 *
 * $Id$
 */

define('IN_PHPBB', true);

$phpbb_root_path = 'forum/';
$phpEx = 'php';

include($phpbb_root_path . 'config.' . $phpEx);
include($phpbb_root_path . 'includes/constants.' . $phpEx);
include($phpbb_root_path . 'includes/db.' . $phpEx);
include($phpbb_root_path . 'includes/template.' . $phpEx);
include($phpbb_root_path . 'includes/functions.' . $phpEx);
include($phpbb_root_path . 'includes/bbcode.' . $phpEx);

// need template defined as the bbcode uses it
$template = new Template($phpbb_root_path . 'templates/Saphic');

function &getForumPosts($forum_id) {
  global $db;

  $posts_array = array();

  // get list of topics
  $topic_sql = 'SELECT t.topic_id, t.topic_first_post_id, t.topic_replies FROM ' . TOPICS_TABLE . ' t WHERE t.forum_id = ' . $forum_id . ' ORDER BY t.topic_time DESC';
  
  if (!($topic_result = $db->sql_query($topic_sql))) { 
    return $posts_text;
  }
  
  if ($topics = $db->sql_fetchrowset($topic_result)) {
    $db->sql_freeresult($topic_result);
  }
  
  foreach($topics as $topic_row) {
    // get post for topic
    $post_sql = 'SELECT p.post_time, pt.post_subject, pt.post_text, pt.bbcode_uid FROM ' . POSTS_TABLE . ' p, ' . POSTS_TEXT_TABLE . ' pt WHERE p.post_id = ' . $topic_row['topic_first_post_id'] . ' AND p.post_id = pt.post_id';
    
    if (!($post_result = $db->sql_query($post_sql))) {
      continue;
    }
    
    if ($posts = $db->sql_fetchrowset($post_result)) { 
      $db->sql_freeresult($post_result); 
    }
    
    $post_row = $posts[0];  // should always have a result
    $text = bbencode_second_pass($post_row['post_text'], $post_row['bbcode_uid']);

    array_push($posts_array, array('topic_id' => $topic_row['topic_id'],
                                   'topic_replies' => $topic_row['topic_replies'],
                                   'post_time' => $post_row['post_time'],
                                   'post_subject' => $post_row['post_subject'],
                                   'post_text' => $text));
  }

  return $posts_array;
}

In the pages for your actual website, you can include the file above and then do the following.

<dl>
<?php
foreach(getForumPosts(13) as $post) {
  echo('<dt>' . date("jS F, Y", $post['post_time']) . '</dt>' . "n");
  echo('<dd>' . nl2br($post['post_text']) . '<br/>');
  echo($post['topic_replies'] . ' comment/s (<a href="forum/viewtopic.php?t=' . $post['topic_id'] . '">view</a>, <a href="http://www.example.com/forum/posting.php?mode=reply&t=' . $post['topic_id'] . '">add</a>)</dd>' . "n");
}?>
</dl>

Leave a Reply

Your email address will not be published. Required fields are marked *

Security Question * Time limit is exhausted. Please reload the CAPTCHA.