• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 34721
  • Last Modified:

phpbb forum: display bbCode

hi,

i want to display the text from a topic from a phpbb forum in a custom page. this works,
but the phpbb code is not converted.

this is what's displayed:
[b:723378836c]Text1[/b:723378836c]: Text2

instead,
Text1: Text2 should be displayed (the text should have a title font and stuff (=bb code))

can someone tell me the correct way to do interprete this bbcode? i know that i have to bbencode_second_pass
the text, but this function gives an error.

Regards
0
cleaverX
Asked:
cleaverX
  • 5
  • 5
1 Solution
 
ShelfieldCollegeCommented:
Below are a number of functions I wrote for retreiving data from a phpBB database, I know the coding in them probably isn't perfect but they seem to work, in it you'll see an example of how to parse the BB Code in a post (check the fetchTopics function). Basically along with the other information from the topic you need to fetch the bbcode ID which is then passed with the bbencode_ functions.

Please feel free to use them, change them improve them whatever works for you.

<?

function fetchTopicPosts($id=0, $start=0, $order="", $limit="") {
  global $db;

  $sql = "SELECT * FROM " . POSTS_TABLE . " WHERE " . POSTS_TABLE . ".topic_id = $id";

  $db->sql_query($sql, FALSE);

  $posts = array();
  $aposts = array();

  $posts = $db->sql_fetchrowset($db->query_result);

  if ($db->sql_numrows($db->query_result)) {
  for ($i = $start; $i < count($posts); $i++) {
    $postdata = getPostDetails($posts[$i]['post_id']);
    $aposts[$postdata['post_subject']] = $postdata;
  }
  }

  return $aposts;
}

function fetchTopics($id=0, $order="", $limit="") {
  global $db;
 
  $sql = "SELECT * FROM `phpbb_topics` WHERE (`forum_id`='$id' AND `topic_type`='0')" . $order . $limit;

  $db->sql_query($sql, FALSE);

  $topics = array();
  $dbtopics = array();

  $dbtopics = $db->sql_fetchrowset($db->query_result);

  if ($db->sql_numrows($db->query_result)) {
  for ($i=0; $i<count($dbtopics); $i++) {
    $topics[$i]['id']         = $dbtopics[$i]['topic_first_post_id'];
    $topics[$i]['title']      = $dbtopics[$i]['topic_title'];
    $topics[$i]['username']   = getUserName($dbtopics[$i]['topic_poster']);
    $topics[$i]['time']       = $dbtopics[$i]['topic_time'];
    $topics[$i]['replies']    = $dbtopics[$i]['topic_replies'];
    $topics[$i]['views']      = $dbtopics[$i]['topic_views'];
    $topics[$i]['bbcode_uid'] = getPostBBCodeUID($dbtopics[$i]['topic_first_post_id']);
    $topics[$i]['body']       = getPostText($dbtopics[$i]['topic_first_post_id']);
    $topics[$i]['body']       = bbencode_second_pass($topics[$i]['body'], $topics[$i]['bbcode_uid']);
    $topics[$i]['body']       = smilies_pass($topics[$i]['body']);
    $topics[$i]['body']       = str_replace("\n", "<br />\n", $topics[$i]['body']);
  }
  }
  return $topics;
}

function getNews() {
  global $db;

  $db->sql_query("SELECT * FROM `phpbb_topics` WHERE `forum_id`='6' ORDER BY `topic_time` DESC LIMIT 10", FALSE);

  $news = array();
  $newsTopics = array();

  $newsTopics = $db->sql_fetchrowset($db->query_result);

  for ($i=0; $i<count($newsTopics); $i++) {
    $news[$i]['id'] = $newsTopics[$i]['topic_first_post_id'];
    $news[$i]['title'] = $newsTopics[$i]['topic_title'];
    $news[$i]['username'] = getUserName($newsTopics[$i]['topic_poster']);
    $news[$i]['time'] = $newsTopics[$i]['topic_time'];
    $news[$i]['replies'] = $newsTopics[$i]['topic_replies'];
    $news[$i]['bbcode_uid'] = getPostBBCodeUID($newsTopics[$i]['topic_first_post_id']);
    $news[$i]['body'] = getPostText($newsTopics[$i]['topic_first_post_id']);
    $news[$i]['body'] = bbencode_second_pass($news[$i]['body'], $news[$i]['bbcode_uid']);
    $news[$i]['body'] = smilies_pass($news[$i]['body']);
  }
  return $news;
}

function getUserName($id=0) {
  global $db;

  $db->sql_query("SELECT `username` FROM `phpbb_users` WHERE `user_id`='$id'", FALSE);

  $tmp = $db->sql_fetchrow($db->query_result);
  return $tmp['username'];
}

function getPostText($id=0) {
  global $db;

  $db->sql_query("SELECT `post_text` FROM `phpbb_posts_text` WHERE `post_id`='$id'", FALSE);

  $tmp = $db->sql_fetchrow($db->query_result);
  return $tmp['post_text'];
}

function getPostDetails($id=0) {
  global $db;

  $db->sql_query("SELECT * FROM `phpbb_posts_text` WHERE `post_id`='$id'", FALSE);

  $tmp = $db->sql_fetchrow($db->query_result);

    $tmp['post_text'] = bbencode_second_pass($tmp['post_text'], $tmp['bbcode_uid']);
    $tmp['post_text'] = smilies_pass($tmp['post_text']);
    $tmp['post_text'] = str_replace("\n", "<br />\n", $tmp['post_text']);

  return $tmp;
}

function getPostBBCodeUID($id=0) {
  global $db;

  $db->sql_query("SELECT `bbcode_uid` FROM `phpbb_posts_text` WHERE `post_id`='$id'", FALSE);

  $tmp = $db->sql_fetchrow($db->query_result);
  return $tmp['bbcode_uid'];
}


Cheers
0
 
ShelfieldCollegeCommented:
I forgot to mention, but basically the fetchTopics function will get a list of topics from a particular forum, including their title and the text from the first post of the topic.  The order and limit parameters should be formatted as they would if you placed them directly into the SQL query as they are simply concatonated onto the end, e.g.

$topics = fetchTopics(10,' ORDER BY `topic_title`', ' LIMIT 10');

I know this is a very dirty way to code the function, and that some of the other functions aren't fully using the paremeters passed to them, however I was just after a quick and dirty way of getting data from the forum database to get a site online and haven't had time to revise the code at the moment.

Cheers

Matt
0
 
cleaverXAuthor Commented:
i'm stuck on the global $db already...
i don't use an object to db normally.

However, i 've been tracing. The function that fails is located in forum/includes:

function load_bbcode_template()
{
global $template; //this one is undefined
//...rest of func
}

i have no idea what's going wrong........plz help
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
cleaverXAuthor Commented:
I checked some other things too:
$txt and $bbcode_uid are filled in correctly

it must be the template that's the problem....?
0
 
cleaverXAuthor Commented:
hi ShelfieldCollege,

i've changed your code to make it work here (without the $db).
your queries and all stuff work, except
$topics[$i]['body']       = bbencode_second_pass($topics[$i]['body'], $topics[$i]['bbcode_uid']);

this bbencode_second_pass function just does not want to work here!!!!
i think the problem lies in initializing sessions and stuff.
can you send me the entire code of the page and the function call plz? where is your file located (i mean compared to /forum)?

good night...
0
 
ShelfieldCollegeCommented:
Hi there, the code i pasted was the entire code, it was simply placed into a page_functions.php from within the main phpbb directory, e.g. /forum

Try making the code echo $topics[$i]['body'] and $topics[$i]['bbcode_uid']  just to check that they do indeed contain the correct values.

basically what happens is:

it checks the topics table for posts made in a certain forum:
it then gets the details from those topics, including the ID of the first post within the topic
next it gets the bbcode ID and post text from the tables by using the post ID

This part takes the current post ID and returns the bbcode ID

$news[$i]['bbcode_uid'] = getPostBBCodeUID($newsTopics[$i]['topic_first_post_id']);

This next part takes the first post ID and retreives the text that goes with that post:

$news[$i]['body'] = getPostText($newsTopics[$i]['topic_first_post_id']);


These are both then used in the bbencode_second_pass() function.

Try checking that the above sections are working correctly by echoing the values of $news[$i]['bbcode_uid'] and $news[$i]['body']

And appolages for the use of $db, I missed it completely. It's simple a connection id from the database connection i create in another file.

Cheers

-Matt-
0
 
cleaverXAuthor Commented:
Hi,

thanks for the reply.
The text (body) and the bbcode_uid which are the params for the bbencode_second_pass function are correct.
Inside the bbeconde_second_pass function there is a global var used: global $template and this one is undefined.  That's the problem .

Could you give me some general info what you do to initialize the board (init_user_data, session management, includes of files,...) whatever is necessary?

Thanks
0
 
ShelfieldCollegeCommented:
Sure, here's how my pages initialise the board connection etc...

Below is the coding used on the page where my news content is desplayed, the content being retrieved from a news forum in phpbb.  This code is in my home.php page which is located in the root phpbb folder, e.g. /forum and I've commented as much of the code as I can.

<?

// The following lines initialize the phpBB stuff, ensure that $phpbb_root_path is set correctly, in this
// instance "./" tells it that the file is located in the same location as phpbb.
define('IN_PHPBB', true);
$phpbb_root_path = './';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);
include($phpbb_root_path . 'includes/bbcode.'.$phpEx);

//
// Start session management
//
$userdata = session_pagestart($user_ip, PAGE_FRONTEND);
init_userprefs($userdata);
//
// End session management
//

// This page being included (page_functions.php) is there the functions I posted previously in this question are located, e.g. the fetchTopics functions etc.
include "page_functions.php";


error_reporting(E_ALL); // This is simply so I can debug any errors that may occur (as the site is still in development)

// This below line simply defines a variable used in the <title> tag within the page header, and following this it includes the page_header.php file
// which is simple HTML used so the main site ties in with the theme for the forum.
$page_title = "FPSCN :: Home"; include "page_header.php";

// This line calls the fetchTopics function, tells it to use forum #6, to order the results by the time they were posted in decending order (ie so most
// recent is first) and to limit it to only 10 results.
$news = fetchTopics("6", " ORDER BY `topic_time` DESC", " LIMIT 10");

?>

Then I simply loop through the $news variable like

<?

for ($i = 0; $i < count($news); $i++) {
  echo $news[$i]['title']; // and so on...
}

?>

This is with a totally standard phpBB forum installation as well, without any extra BB Code mods or anything such like that.

if you're still having trouble then post the code you're using and I'll take a look for you.

Cheers

-Matt-
0
 
cleaverXAuthor Commented:
hi Matt,

today is my best day of the week, unless i meet the girl of my life tomorrow.
i finally got it to work ;-))))

it were actually some stupid things that caused the failure:the lines below were missing :-S
$userdata = session_pagestart($user_ip, PAGE_INDEX);
init_userprefs($userdata);

they once were in the page, but they must have been deleted accidently.
btw: what's the difference between PAGE_INDEX and PAGE_FRONTEND, where are they defined?

however, it works,
thanks for your example code!
0
 
ShelfieldCollegeCommented:
The PAGE_INDEX and PAGE_FRONTEND are used to record in the database what page a user is looking at, if you've noticed in the admin panel of phpBB and whilst browsing the forum it tells you which other users are looking at the page and the admin panel tells you when a guest is logging in etc and so on.

I believe these are part of the constants.php file found within the includes/ folder in phpBB some examples are as follows:

define('PAGE_INDEX', 0);
define('PAGE_LOGIN', -1);
define('PAGE_SEARCH', -2);
define('PAGE_REGISTER', -3);
define('PAGE_PROFILE', -4);
define('PAGE_VIEWONLINE', -6);
define('PAGE_VIEWMEMBERS', -7);
define('PAGE_FAQ', -8);
define('PAGE_POSTING', -9);
define('PAGE_PRIVMSGS', -10);
define('PAGE_GROUPCP', -11);
define('PAGE_DOWNLOAD', -12);
define('PAGE_CARD', -13);
define('PAGE_LINKS', -14);
define('PAGE_STAFF', -22);
define('PAGE_RECENT', -33);
define('PAGE_TOPIC_OFFSET', 5000);

As you can see they are simply definations of integer values.  I wouldn't recommend using the PAGE_FRONTEND one as if I remember right that's simply one I used to record when a user was looking at part of my site which wasn't an actual part of phpBB.

you can of course define your own constants here and have the database record which pages of your site the user is looking at and then track this yourself.

Glad you finally got it working, the missing lines were probably and oversight on my part, if they were I do appolagise :)

Anything else you need just let me know.

Cheers

-Matt-
0
 
hujiCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: ShelfieldCollege {http:#13874026}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Huji
EE Cleanup Volunteer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now