Blog

How to display posts and topics on external pages

Posted by battye in Modifications with the tags on November 9th, 2009

A very common question about phpBB is how to display recents posts or topics on a separate page, such as a website homepage. It can be very handy to do this, as it allows visitors to your website a chance to quickly see recent activity.

This blog post details how displaying a list of recent posts and topics externally can be done. There are a couple of different formats that are covered:

  • The latest topics (including only from specified forums)
  • The first post of the latest topics (including from specified forums)
  • The latest posts from specified topics
  • The latest posts from the entire forum

Everything displayed is subject to the users forum read permissions.

With the information in this article you could either choose to make additions to a current php file or create a brand new php file. To start from the ground up, you need to create a new file (for example, home.php) and include the standard phpBB3 header information. The $phpbb_root_path variable may need to be altered depending on where the phpBB installation is located. If the file is in the same directory as the phpBB root (the same area as viewtopic.php, viewforum.php, etc) then the relative file path ‘./’ does not need to be changed.

If you have your phpBB installation in a directory separate to your main website, you will need to edit the $phpbb_root_path variable. Suppose phpBB was located in a directory called “forums” (ie. http://www.example.com/forums/) and you want the file home.php to be located in the website root directory (ie. http://www.example.com/home.php), then you would need to set the $phpbb_root_path to:

Code: Select all
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './forums/';

To go “up” a directory, you can use ‘../’. You would use this if phpBB was installed in the website root directory (ie. http://www.example.com/) and you wanted to have your home.php script located in a sub-directory (ie. http://www.example.com/info/home.php).

The standard phpBB header looks like this: (bbcode.php needs to be included as when displaying the latest posts you may wish to parse the bbCode)

Code: Select all
<?php
/*
* 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
$user->session_begin();
$auth->acl($user->data);
$user->setup('viewforum'); 

For the sake of simplicity when dealing with extracting posts or topics from multiple sources (ie. multiple topics or multiple forums), a function can be used to create the where clauses for the SQL query.

Code: Select all
/* create_where_clauses( int[] gen_id, String type )
* This function outputs an SQL WHERE statement for use when grabbing
* posts and topics */

function create_where_clauses($gen_id, $type)
{
global $db, $auth;

$size_gen_id = sizeof($gen_id);

switch($type)
{
case 'forum':
$type = 'forum_id';
break;
case 'topic':
$type = 'topic_id';
break;
default:
trigger_error('No type defined');
}

// Set $out_where to nothing, this will be used of the gen_id
// size is empty, in other words "grab from anywhere" with
// no restrictions
$out_where = '';

if ($size_gen_id > 0)
{
// Get a list of all forums the user has permissions to read
$auth_f_read = array_keys($auth->acl_getf('f_read', true));

if ($type == 'topic_id')
{
$sql = 'SELECT topic_id FROM ' . TOPICS_TABLE . '
WHERE ' . $db->sql_in_set('topic_id', $gen_id) . '
AND ' . $db->sql_in_set('forum_id', $auth_f_read);

$result = $db->sql_query($sql);

while ($row = $db->sql_fetchrow($result))
{
// Create an array with all acceptable topic ids
$topic_id_list[] = $row['topic_id'];
}

unset($gen_id);

$gen_id = $topic_id_list;
$size_gen_id = sizeof($gen_id);
}

$j = 0;

for ($i = 0; $i < $size_gen_id; $i++)
 {
$id_check = (int) $gen_id[$i]; // If the type is topic, all checks have been made and the query can start to be built if( $type == 'topic_id' ) { $out_where .= ($j == 0) ? 'WHERE ' . $type . ' = ' . $id_check . ' ' : 'OR ' . $type . ' = ' . $id_check . ' '; } // If the type is forum, do the check to make sure the user has read permissions else if( $type == 'forum_id' && $auth->acl_get('f_read', $id_check) )
{
$out_where .= ($j == 0) ? 'WHERE ' . $type . ' = ' . $id_check . ' ' : 'OR ' . $type . ' = ' . $id_check . ' ';
}

$j++;
}
}

if ($out_where == '' && $size_gen_id > 0)
{
trigger_error('A list of topics/forums has not been created');
}

return $out_where;
}

We now include some of the forum and topic ids that will be used throughout the script. $forum_id and $topic_id are both arrays that allow you to grab information from multiple sources. The $forum_id_where and $topic_id_where variables both call a new function, create_where_clauses, which will simplify the database queries.

Code: Select all
$search_limit = 5;

$forum_id = array(2, 5);
$forum_id_where = create_where_clauses($forum_id, 'forum');

$topic_id = array(20, 50);
$topic_id_where = create_where_clauses($topic_id, 'topic');

$search_limit is the number of rows you want to grab. So if you want to get the 5 latest topics, you would set $search_limit to equal 5;

By leaving the array “$forum_id” empty, it will mean there are no restrictions and it will take the latest posts/topics from the entire forum instead of any particular area. Examples 3 and 4 demonstrate how to take the latest posts from specified areas and the entire board respectively.

Do not forget to change the values in $forum_id and $topic_id from the default values currently given.

There are four examples given below, the code in each example being slightly different to the others. When writing your php file, you will only need to include one of these examples (ie. you do not need to copy/paste the code for all four) to achieve the desired result. Think of each example as being completely separate to the others.

EXAMPLE 1: Display latest topics from specified forums

The first example is selecting the 5 latest topics from the forum(s) chosen above. An example of this can be seen at http://www.bilvardsforum.se/ (Swedish)

Code: Select all
$topics = 'SELECT * FROM ' . TOPICS_TABLE . '
' . $forum_id_where . '
AND topic_status <> ' . ITEM_MOVED . '
AND topic_approved = 1
ORDER BY topic_id DESC';

$topics_result = $db->sql_query_limit($topics, $search_limit);

while ($topics_row = $db->sql_fetchrow($topics_result))
{
$topic_title = $topics_row['topic_title'];
$topic_author = get_username_string('full', $topics_row['topic_poster'], $topics_row['topic_first_poster_name'], $topics_row['topic_first_poster_colour']);
$topic_date = $user->format_date($topics_row['topic_time']);
$topic_last_post = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "p=" . $topics_row['topic_last_post_id'] . "#" . $topics_row['topic_last_post_id']);
$topic_last_author = get_username_string('full', $topics_row['topic_last_poster_id'], $topics_row['topic_last_poster_name'], $topics_row['topic_last_poster_colour']);
$topic_link = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "t=" . $topics_row['topic_id']);

$template->assign_block_vars('announcements', array(
'TOPIC_TITLE' => censor_text($topic_title),
'TOPIC_AUTHOR' => $topic_author,
'TOPIC_DATE' => $topic_date,
'TOPIC_LAST_POST' => $topic_last_post,
'TOPIC_LAST_AUTHOR' => $topic_last_author,
'TOPIC_LINK' => $topic_link,
));
}

A very primitive example of the templating code associated with this would be as follows. (for much more detailed templating examples, see the templating syntax article at the wiki):

Code: Select all
<!-- BEGIN announcements -->
Title: {announcements.TOPIC_TITLE}
Author: {announcements.TOPIC_AUTHOR}
Date: {announcements.TOPIC_DATE}
Last post: {announcements.TOPIC_LAST_POST}
Topic link: {announcements.TOPIC_LINK}
<!-- END announcements -->

EXAMPLE 2: Display first post from the last five topics

The next example involves extracting the first post of each of the five last topics. It consists of quite a lot of the same code, except this time it also gets the post text and makes some bbCode related calls. Notice how the posts and topics are selected within the same SQL query – by doing a left join it means you do not have to include a second database query when looping through the results of the initial query.

For examples 2 to 4, see how a new bbcode object has been created and its associated functions are used to allow the post text to be correctly parsed.

An example of this can be seen at http://www.cricketmx.com/ (English)

Code: Select all
$posts_ary = array(
'SELECT' => 'p.*, t.*',

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

'LEFT_JOIN' => array(
array(
'FROM' => array(TOPICS_TABLE => 't'),
'ON' => 't.topic_first_post_id = p.post_id'
)
),

'WHERE' => str_replace( array('WHERE ', 'forum_id'), array('', 't.forum_id'), $forum_id_where) . '
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'];
$topic_author = get_username_string('full', $posts_row['topic_poster'], $posts_row['topic_first_poster_name'], $posts_row['topic_first_poster_colour']);
$topic_date = $user->format_date($posts_row['topic_time']);
$topic_link = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "t=" . $posts_row['topic_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),
'TOPIC_AUTHOR' => $topic_author,
'TOPIC_DATE' => $topic_date,
'TOPIC_LINK' => $topic_link,
'POST_TEXT' => censor_text($post_text),
));
}

EXAMPLE 3: Display posts from specified topics

The third example is grabbing the latest posts from a specified topic(s). An example of this can be seen at http://www.tokiohotelfans.se/ (Swedish)

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

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

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

'WHERE' => str_replace( array('WHERE ', 'topic_id'), array('', 't.topic_id'), $topic_id_where) . '
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),
));
}

EXAMPLE 4: Display posts from anywhere

This final example demonstrates how to display the latest posts from the entire board with no restrictions on topics. The only difference is the removal of the line ‘ . str_replace(‘topic_id’, ‘t.topic_id’, $topic_id_where) . ‘. It is replaced with an sql_in_set call that restricts the returned results to areas that the user has correct permissions for. An example of this example can be seen at http://www.rmcgirr83.org/ (English)

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

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

'LEFT_JOIN' => array(
array(
'FROM' => array(USERS_TABLE => 'u'),
'ON' => 'u.user_id = p.poster_id'
),
array(
'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),
));
}

By editing the SQL query you can manipulate the examples above to do almost anything you would like with regards to displaying posts and topics on external pages.

By using the templating variables, you can also incorporate the results into a page styled like phpBB3. To do this you should end the page with the code given below, and create an associated phpBB3 template (*.html) file. As above, for further information about templating please read the phpBB wiki page on template syntax.

Code: Select all
page_header('The title of your page goes here');

$template->set_filenames(array(
'body' => 'home_body.html'
));

page_footer();
?>

Also, see the corresponding wiki article to this blog post at Displaying posts and topics on external pages

53 Responses to “How to display posts and topics on external pages”

Posted by Eric Martindale on November 10th, 2009 at 4:08 am:

Some clarification is necessary, I think:

To go “back” a directory, you can use ‘../’.

This does not mean “back” a directory, as this is misleading – the correct term is “up” a directory, where “up” specifies “parent directory” of the current working directory.

Code: Select all
../

implies the “parent” directory, whereas

Code: Select all
./

is the current working directory. There is no concept of “back” when working in a traditional script environment.

Aside from the above point, this is a very good guide from the wiki. Will it be updated to reflect any changes on the original page?

Posted by Elyaradine on November 10th, 2009 at 7:53 am:

Thanks for the useful post.

Posted by damsprivate on November 10th, 2009 at 4:05 pm:

omg , this post is what im waiting . how about templateing tricks ? 🙂

Posted by dcz on November 11th, 2009 at 8:30 am:

Looks like it’s not using the latest code : http://wiki.phpbb.com/Displaying_posts_and_topics_on_external_pages
http://wiki.phpbb.com/index.php?title=Displaying_posts_and_topics_on_external_pages&diff=4410&oldid=4407

I think it’s better to use the same links as phpBB would.

Regards,

dcz

Posted by Mike S on November 13th, 2009 at 5:19 pm:

I am very interested in adding this feature. I just simply downloaded this forum from GoDaddy and made basic chnages in teh Admin area. I am not a PHP programmer. Can I do this from my Admin Panel or do I have to go inside the code? Please explain where these changes need to take place. If I need to go in the code, where do I start and give me a better step-by-step vision. Where is the exact file, etc. I am looking for EXAMPLE 4: Display posts from anywhere.

Thanks.
Mike

Posted by Byron on November 16th, 2009 at 10:45 pm:

OK yes im a dummy! I just want to simply display recents posts or on a separate page. After reading this article im just as clueless as when I started. I dont know php at all! a the article assumes this. But If the person knows php then he wouldn’t need this article. for example; the script- Display first post from the last five topics. were do i put it ie. before the html in the body of the web page?? also were n the script do i point to the actual content address i’m trying display?? ie. http://jstclus.com/board/index.php/board,1.0.html

Posted by Ger on November 17th, 2009 at 9:53 am:

You don’t even have a phpBB forum, so this doesn’t apply to your forum at all.

And with knowledge of php, you don’t immediatly know how to display forum info on an external page. You don’t know by default how the permissions are setup, how the topics and posts are stored and sorted, etc. So to put it simple: your post doesn’t make sense at all.

Posted by joe on November 18th, 2009 at 9:53 pm:

This is great. Can you tell me how to embed this code on my Front Page, we use Joomla.
Thank you

Posted by Gopal on November 19th, 2009 at 8:46 am:

I am very interested in add to this feature. I just simply downloaded this forum and made basic chnages in Admin area.

Thanks.
Gopal Rathod

Posted by Ron on November 21st, 2009 at 5:02 pm:

If you use Joomla, here’s a module that will show your latest posts:

http://brandworkspace.com/ronposts

Posted by battye on November 22nd, 2009 at 8:11 am:

It looks like there are a few people here with only a small amount of programming knowledge that had trouble following this article so what I’ve done is I’ve written a follow-up post at http://www.cricketmx.com/articles/read/working-example-of-displaying-latest-posts-on-external-pages-in-phpbb3/

That article is hopefully a little easier to understand (I’ve only gone through example 4 as it seems to be the most popular one), and I’ve included a zip file containing the 2 files you need (the php file and the html template file) so all you need to do is upload them to see them work. Then you can play around with the script to get it the way you want. Eric Martindale, thanks; I have changed it to “up”.

Posted by sopas on November 22nd, 2009 at 8:18 pm:

I tried this with example 4, just as Battye suggested but I get this error message:

[phpBB Debug] PHP Notice: in file /includes/functions.php on line 4183: Cannot modify header information – headers already sent by (output started at /home/sopas/site_html/home.php:2)

Posted by Vegard on November 23rd, 2009 at 1:16 pm:

Is there any way to show pictures on an external page too?

I mean, I want to show some posts on an external page and I will be using example nr. 3 (If I can make it work someday..)
But I also want to show the pictures that are included in the posts.
Is there any way to do this?

Thanks

Posted by John on November 24th, 2009 at 5:22 am:

To me this seems unnecessarily complicated. Can be done in a much easier way…

Posted by Ger on November 24th, 2009 at 2:10 pm:

Well, it’s nice to read is can me done in a much easier way. Should be even nicer when you explain that statement a bit instead of just mentioning it… 😉

Posted by Jidine on November 26th, 2009 at 4:24 am:

I just install phpBB forum and already ok ut my problems is I cannot shows my forum’s topic due to this warning: Please delete, move or rename the install directory before you use your board. If this directory is still present, only the Administration Control Panel (ACP) will be accessible.
What is the meaning actually? Can you please consult me what to do.

Posted by charlton php on November 27th, 2009 at 11:59 am:

i hve just intalled the phpbb 3.06 and its working fine. i want when i click on the index.php i should see all the topics posted at least on page but i only see the page to login or register. how can i do it?please help.

Posted by MattS on November 28th, 2009 at 2:09 am:

Couple requests if you’re feeling generous.

Can option #2 be modified to drop BB code from post?

Can you set limit of 1st post to 500 characters or better yet user defined?

Can you Include number of replies?

I also get the following error when trying to include on my index.php. Page loads fine by itself.

[phpBB Debug] PHP Notice: in file /includes/functions.php on line 4183: Cannot modify header information – headers already sent by

Posted by Vegard on November 28th, 2009 at 1:23 pm:

Trying once again since my first post may be miss understood.

When you use lets say Example 3, and show postes on an external page, is there any way to include the attachments with the posts??

If one of the posts has an attachment (picture), it does not show on the external page.
How can I make that happen?

If a picture is included with the normal [img] [/img] command, it does show. But I need this attachment thing to work.

Thank you.

Posted by Dario de Judicibus on November 29th, 2009 at 10:10 pm:

During a discussion with a forum owner about the usage of links in forums and sustainable web, I realized that we could save a lot of space in the post database if quotes would be link rather than duplication of text. In practise, the quoted text is embedded in the referencing post ONLY when the post is displayed, otherwise it is just a link to the original fragment. Do you think it is possible to change the phpBB code to implement it? It would save a lot of storage. See my article for details:
http://lindipendente.splinder.com/post/21788665/Webecology
It could be a good way to save resources in web. Thank you in advance.

Posted by Jimbob on December 1st, 2009 at 7:55 pm:

doesnt work at all for me, its not explained very well, my page just sits thier loading and nothing else 🙁

Posted by Lorenzo Porretti on December 7th, 2009 at 12:30 pm:

😀 very good! thanks a lot!

Posted by Carsten on December 10th, 2009 at 11:58 pm:

Thank you very much for your very interesting and informative article!

For the same purpose (displaying the first post of each topic from a specified forum, that is, for displaying a “news” page in my Joomla CMS that is made from forum posts), I used to use (before phpBB 3.0.6) an RSS feed MOD that provides the desired posts as an RSS feed.
As Joomla has a built-in component for displaying news feeds as content pages, the problem was/is half solved.

The “other half” of this approach is dealing with CSS formatting issues, the proper display of phpBB inline attachments, etc., which require some hand-coding to get right.

This “feed detour” approach has the advantage of being *very* flexible and conceptually simple (i.e. no mix of phpBB and Joomla code is necessary), and you get the same content as news feed “as a free side effect”.

With the new phpBB 3.0.6, which now supports news feeds natively, the same concepts apply (the original RSS feed MOD is no longer necessary as it can be replaced by the native code).

But as with the original feed MOD, the problem is in the details of getting CSS formatting, images, inline attachments, custom BBcodes and similar issues right. I’m currently preparing a MOD for the native phpBB feed.php code in this regard – any help appreciated. 🙂

Hope this is useful for someone, e.g. as a complement / alternative in the set of available approaches to the problem. 🙂

Btw., you can see the three steps “phpBB — newsfeed — Joomla” at http://www.cafu.de/forum/ and http://www.cafu.de/news (It still uses the newsfeed MOD, but I’ll switch from the newsfeed MOD to the native feed.php during the next couple of weeks.)

Best regards,
Carsten

Posted by wireless on December 11th, 2009 at 2:33 pm:

I also get the following error when trying to include on my index.php. Page loads fine by itself.

Posted by Nathan on December 16th, 2009 at 1:26 pm:

Great blog post, it works 🙂

Posted by ColinB on December 21st, 2009 at 4:05 pm:

Will this work with a WordPress-based site? I’d like to give it a spin but would be interested in the prognosis before I attempt it!!! Thanks.

Posted by Reto on December 22nd, 2009 at 10:10 pm:

Hi
Great article and just what I was looking for but I have a problem: the page stops after the included code (above) because of the page_footer() but I would like to have some more content. How is this possible?

Posted by Max on December 28th, 2009 at 10:07 am:

Thank you for the post ..its the most common question asked to us as well by our customers..your post will help us to explain them.

Thank you.

Posted by Ed on December 28th, 2009 at 10:54 am:

It works for me but it display in new windows. I was thinking on how to run and display those posts inside my zencart online shop template. Is it possible to do that? Thank you.

Posted by emma on January 2nd, 2010 at 8:14 pm:

I just get a blank page when using this D= no errors or anything. Although when I use the zip file from http://www.cricketmx.com/articles/read/working-example-of-displaying-latest-posts-on-external-pages-in-phpbb3/ it works, pitty I want to use example 2 =(

Posted by Jason on January 6th, 2010 at 8:05 am:

All this chatter, and no mention of Ca5ey’s “phpBBFetchall” from the phpBB2 days. I really do lament the loss of that project. It, and the “RSS Everywhere” mod were two very good solutions. FetchAll requiring NO local phpBB file modification whatsoever.

This post is great, but the examples are just so difficult. Steps towards wrapping them up into classes/functions that would let you have a few includes and easy functions would go a long way.

No having to muck with BBCode sanity, no rewriting session handling, no templating nonsense, just a ton of provided functions that get the job done.

R.I.P phpbbfetchall. If anyone knows of an up-to-date phpbb3 compatible package, I would greatly appreciate the tip.

Posted by Jeffrey on January 8th, 2010 at 3:16 pm:

Hi… I have one question.. How do you add a reactions-count to the information, as seen on the 2 of the example pages? Thanks for the info!

Posted by Dan Leedham on January 9th, 2010 at 10:22 am:

Would anyone be able to explain how to use this method for pulling events from the calendar mod:

“phpBB Calendar 0.1.0 (alightner)” found here: http://www.phpbb.com/community/viewtopic.php?f=70&t=666195 . I myself have tried using the methods above and modifying them but got nowhere. I’ve also tried an include but ended up with loads of { } brackets everywhere and no CSS.

Posted by mrniss on January 13th, 2010 at 7:42 am:

is there any way to make this whole thing NOT try to force headers onto a page? i am using method 2 and basically using a forum as a blog, each topic as an entry. it works well, but i get this:

[phpBB Debug] PHP Notice: in file /includes/session.php on line 1006: Cannot modify header information – headers already sent by (output started at (path deleted)/index.php:18)

i’ve looked through the code but it’s monstrous. it’d be nice to not have some 70 lines just to generate an sql query i could probably make, but the code is so insane that i can’t figure out how to just trim it down without it breaking.

Posted by mrniss on January 14th, 2010 at 5:13 am:

also, is there a way to make this parse custom bbcode tags?

Posted by James on January 22nd, 2010 at 3:10 am:

Hello all,
Bare with me, as I’m new to php, I have set up example 3 above, and its working. BUT its displaying a real random topic.
What part do I change to make it display posts from forum id 2

Thank you very much

Posted by James on January 22nd, 2010 at 10:09 pm:

Sorry, I mean I was using example 1. I want it to display the latest posts (Which its doing) from forum id 2

Posted by James on January 22nd, 2010 at 11:09 pm:

Alright, just ignore me,
I’ve just played around with everything and its all working now, I need to spend sometime learning php I think.
Now I’m going to try make it so when you click on the title it links to the topic.

Thanks for such an awesome script.

Posted by Thomas Hedin on January 29th, 2010 at 1:04 am:

Is there anyone that is willing to walk me through the whole process of how to work this into my website?

I’m new to working with php and could really use a good tutor.

thomasahedin at q dot com (get me on the msn messenger if possible.

Posted by Pony99CA on February 1st, 2010 at 8:09 am:

I don’t think Example #1 works if you want topics returned from all forums. If you leave $forum_id empty, create_where_clauses will return an empty string and the SQL query will fail because there’s no WHERE keyword, just an AND keyword.

Posted by Axel on February 4th, 2010 at 4:09 pm:

Hi there,

I’ve spent all afternoon on this and finally got Example 1 to work. Thx for the info on the page.

Have 2 – 3 Questions though …

1. By changing the html template I managed to link the topics to the corresponding forum but it is not possible to link to the topic directly. In the template ZIP on CMX there is a POST_LINK included .. how would I implement this into Example 1?

2. How can I truncate the output in the announcement statements to avoid to long entries?

3. Same as Q1 .. but with date and Author. How would I add POST_Author and POST_DATE to Example 1 ??

Thx again for these instructions and any hints on the Q’s would really be appreciated ..

Greetings from Germany

Posted by damadailal on February 13th, 2010 at 12:12 pm:

I was jsut wondering how to get RSS feeds from PHPbb forums. I know I can get from Vbulleting. any idea?

Posted by radiofranky on March 9th, 2010 at 12:16 am:

I was wondering if is possible to post a teaser text to the display.

for example:

title, user name
teaser text

also, does anyone know how to display top 5 most viewed topics?

thanks

Posted by James on March 16th, 2010 at 2:50 pm:

got this working, but I want the last 5 post from 5 topics I choose to show, but cannot get that bit to work, I select forum ID`s in the array, and topic IDs in the array, but it ignores topic ID numbers 🙁

Posted by Kalliste on June 7th, 2010 at 9:18 pm:

I have this working as per the example but now how did I style it the way I want?

I’m not sure what use I have of the author linking when nothing else does.

How can I get the Title to link to the topic also?

Posted by Bruno on July 15th, 2010 at 1:54 pm:

The rest of my page gets cut off as soon as the script ends on

page_footer();
?>

Can anyone explain to me how to stop this from happening?

Posted by SalsaForte on July 30th, 2010 at 3:33 pm:

Does anyone have these pieces of code in “function” version?

Posted by Bluekewlade on August 3rd, 2010 at 2:42 am:

Ok, so I did a bit of work on this. I have come back to this posting many many times and it continues to frustrate me because there just doesn’t seem to be enough info. So, I figured it out.

This is for example 1, and my set up basically displays the most recent posts from 1 specific forum…

Code: Select all

//I limit it to the most recent 2 posts.
$search_limit = 2;

//this declares which forum I want.
$forum_id = array(7);
$forum_id_where = create_where_clauses($forum_id, 'forum');

$topic_id = array(0, 70);
$topic_id_where = create_where_clauses($topic_id, 'topic');

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

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

'LEFT_JOIN' => array(
array(
'FROM' => array(TOPICS_TABLE => 't'),
'ON' => 't.topic_first_post_id = p.post_id'
)
),

'WHERE' => str_replace( array('WHERE ', 'forum_id'), array('', 't.forum_id'), $forum_id_where) . '
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'];
$topic_author = get_username_string('full', $posts_row['topic_poster'], $posts_row['topic_first_poster_name'], $posts_row['topic_first_poster_colour']);
$topic_date = $user->format_date($posts_row['topic_time']);
$topic_link = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "t=" . $posts_row['topic_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);

//truncate the post to the first 50 letters?...
//I added this function to limit the amount of letters displayed.
$post_text = myTruncate2($post_text, 75);

$template->assign_block_vars('announcements', array(
'TOPIC_TITLE' => censor_text($topic_title),
'TOPIC_AUTHOR' => $topic_author,
'TOPIC_DATE' => $topic_date,
'TOPIC_LINK' => $topic_link,
'POST_TEXT' => censor_text($post_text),
));
}
?>

Most of this stuff is the same, but I commented what is changed up there. I have this section of code smashed above the declaration at the top of the page.

Code: Select all

set_filenames(array('body' => 'news_styles.html'));

page_footer();
?>

This section goes in where I have the content, styled all up based on the template. I have it smack in the middle of a . As you can see, I took out the headers part because of the error messages it keeps sending. I havent tested this on other browsers yet.

Code: Select all

/*create_where_clauses( int[] gen_id, String type )
* This function outputs an SQL WHERE statement for use when grabbing
* posts and topics */

function create_where_clauses($gen_id, $type) {
global $db, $auth;

$size_gen_id = sizeof($gen_id);

switch($type) {
case 'forum':
$type = 'forum_id';
break;

case 'topic':
$type = 'topic_id';
break;

default:
trigger_error('No type defined');
}

// Set $out_where to nothing, this will be used of the gen_id
// size is empty, in other words "grab from anywhere" with
// no restrictions
$out_where = '';

if ($size_gen_id > 0) {
// Get a list of all forums the user has permissions to read
$auth_f_read = array_keys($auth->acl_getf('f_read', true));

if ($type == 'topic_id') {
$sql = 'SELECT topic_id FROM ' . TOPICS_TABLE . '
WHERE ' . $db->sql_in_set('topic_id', $gen_id) . '
AND ' . $db->sql_in_set('forum_id', $auth_f_read);

$result = $db->sql_query($sql);

while ($row = $db->sql_fetchrow($result)) {
// Create an array with all acceptable topic ids
$topic_id_list[] = $row['topic_id'];
}

unset($gen_id);

$gen_id = $topic_id_list;
$size_gen_id = sizeof($gen_id);
}

$j = 0;

for ($i = 0; $i acl_get('f_read', $id_check) ) {
$out_where .= ($j == 0) ? 'WHERE ' . $type . ' = ' . $id_check . ' ' : 'OR ' . $type . ' = ' . $id_check . ' ';
}

$j++;
}
}

if ($out_where == '' && $size_gen_id > 0) {
trigger_error('A list of topics/forums has not been created');
}

return $out_where;
}
//end function

And of course, I have a php section along the bottom of the file after the section to hold my functions. That one goes there. Not much else is changed. Thats about it, I commented some areas that needed discussion and I tried to explain a bit what I did to get it a little more understandable.

Hope I did butcher this post by slapping some code in there…hope it helps?

Posted by Bluekewlade on August 3rd, 2010 at 2:45 am:

good lord that post is ugly, my apologies.

this snippit of code goes along with my truncated text in the post above. This is the function for anyone who is interested that will truncate the post to a specified amount of letters. Good for using your PHPBB boards as a blog in an external page:

Code: Select all

//truncate the post message so it is a bit smaller
// Original PHP code by Chirp Internet: www.chirp.com.au
// Please acknowledge use of this code by including this header.

function myTruncate2($string, $limit, $break=" ", $pad="...")
{
// return with no change if string is shorter than $limit
if(strlen($string) <= $limit) {
return $string;
}

$string = substr($string, 0, $limit);
if(false !== ($breakpoint = strrpos($string, $break)))
{
$string = substr($string, 0, $breakpoint);
}
return $string . $pad;
}

basically just ripped the code from another php site, but it works perfectly. I don’t know if this topic is dead, but there were some issues that needed help, so hope these help!

Posted by sabrewulf on August 21st, 2010 at 5:56 am:

having a problem with trying to place my recent topic posts in a area on my phpBB3 main page. I have used the code from Example 1 and it works flawless outputting the last 5 topics on a separate page and I have even got it to work to post the topics on a separate server not containing the .php code … BUT .. when I go to add the code:

[code][/code]

(glance.php being the file I have the Example 1 code located in my root directory of my forums)

to a section of my overall_header.html on my forums I get the error:

[code]Fatal error: Cannot redeclare deregister_globals() (previously declared in /phpBB3/common.php:36) in /phpBB3/common.php on line 33[/code]

If I go directly to the .php page it displays the correct out put in the correct format I have laid out in the corresponding glance.html output file in my templates directory.

any help would be appreciated as I’m about to punch my screen ……

Posted by Idiot on August 31st, 2010 at 9:36 am:

Code: Select all
function get_latest_posts($limit = 10)
{
// Function will return the most recently updated topics and return the topic title and enough to construct the URL.

$my_db = new mysqli('localhost', 'user_name', 'password', 'database_name');

if (mysqli_connect_error())
{
return false;
}

$q_posts = "
SELECT t2.topic_title, t1.forum_id, t1.topic_id FROM phpbb_posts AS t1 JOIN phpbb_topics AS t2 ON (t1.topic_id = t2.topic_id)
GROUP BY t2.topic_title
ORDER BY t1.post_time DESC
LIMIT $limit
";

if ($result = $my_db->query($q_posts))
{
if ($result->num_rows > 0)
{
// Initialize array index variable.
$row_count = 0;

while ($row = $result->fetch_assoc())
{
// Package data in array for return so you do not have to work with a query result resource.
foreach ($row as $key => $value)
{
$rowdata[$row_count][$key] = $value;
}

// Increment row counter.
$row_count++;
}
return $rowdata;
}

}
else
{
return false;
}
}

$latest_posts = @get_latest_posts(10);

if($latest_posts)
{
// Echo them out.
foreach ($latest_posts as $post)
{
echo '' . $post['topic_title'] . '';
}

}

Posted by Idiot on August 31st, 2010 at 9:37 am:

It looks like the blog constructed a URL out of my sample. I didn’t think of using the code tag. Sorry.

Posted by daroPL on September 20th, 2010 at 8:03 am:

The last example is strongly not optimal!

Commenting is disabled for this blog post