Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 3 Articles
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
Show last 5 posts from thread x on a non-vb page (and parse BB)
Digma
Join Date: Nov 2004
Posts: 60

 

Netherlands
Show Printable Version Email this Page Subscription
Digma Digma is offline 04-20-2010, 10:00 PM

Note: The arcticle below is based on working with vb3.7.3 PL1 and it's fully functional there. I ought it be functional within the rest of the 3.x range, but I can't guarantee anything.

Introduction
This is the first article related to programming I've ever written, so bare with me. While looking for a proper solution myself I have noticed that a lot of people were looking on how to pull posts from a specific thread (not just forum, can use RSS for that) and show either the first x or last x posts from that thread on a non-vb page. On top of that I noticed that in the meantime, which isn't less important, that people were also struggling on how to parse BB Code at the same time. I am not a programmer or anything, but am more than willing to share my knowledge of this, after having ploughed through hundreds of posts and dozens of threads. As I know how frustrating this can be. All credits go to all that contributed through their posts in other threads in order for me to complete this piece of coding.

What is not covered?
I am going to assume that you are familiar with connecting to databases, even connecting to multiple databases and that you will familiarize yourself with the structure of your 'post' table in your forum's database. If you want information on how to connect to 2 databases within the same page, check php.net on mysql_connect() for more info on that.

What is covered?
I'll show you what files regarding the interaction with vbulletin are required for fetching the information and parsing BB Code and give you a little comparisson information on what I did with the true/false settings you can do with the parse. Right, lets get started.

Required lines of code for vbulletin interaction and parsing BB Code
PHP Code:
$curdir getcwd ();
chdir('/path to forum');
require_once(
'/path to forum/global.php');
require_once(
'/path to forum/includes/class_bbcode.php');
$parser =& new vB_BbCodeParser($vbulletinfetch_tag_list());
// $parsed_text = $parser->do_parse($text, $do_html, $do_smilies, $do_bbcode, $do_imgcode, $do_nl2br, $cachable);    
chdir ($curdir); 
What this basically does is get the interaction going with the vbulletin database and get the required information to properly parse your BB Code. As you can see I commented (//) the line with $parsed_text out, as I only use this specific line for reference later on in the script. We will get back on that later on.

Don't forget to set your path to your forum!

Setting your variables for the Query on the post table
I think the code below is obvious. Of course you can replace 'your_forum_db' and 'thread_id' with values you pull from another database, but lets keep it simple here. Also make sure your threadid exists

PHP Code:
// Set variables
$db_name 'your_forum_db'// for example 'forums'
$db_table 'your_post_table'// for example 'prefix_post'
$thread_id 'thread_id'// for example '6201' 
Determine postcount for thread
Now we're going to make sure you get those last 5 posts and we have to set a limit for that. Now there are other way to approach this, but I am planning to keep it simple and basically this is what suits my needs.

PHP Code:
$max "SELECT COUNT(postid) AS highest_id FROM ".$db_name.".".$db_table." WHERE threadid=".$thread_id;
$max_result mysql_query($max);
$max_output mysql_fetch_array($max_result);
$limit $max_output['highest_id']-5
So what I have done here is count the occurances of postid where the information equals a specific threadid. In other words, how many posts have already been made to this thread. In the end I set a $limit variable and deduct 5 from the postcount that was queried. This is not the query for getting the posts themselves, we will cover that in the next bit.

Getting the post information based on the limit
Now that we know the postcount on thread x, we can go on querying the post table for its contents.

PHP Code:
$query3 "SELECT postid, username, userid , threadid, pagetext FROM ".$db_name.".".$db_table." WHERE threadid=".$thread_id." ORDER BY postid ASC LIMIT ".$limit.",5";    
$result3 mysql_query($query3);
while (
$output3=mysql_fetch_array($result3)) 
Above I have definied 5 cellnames I found interesting to see (postid, username, userid, threadid and pagetext) from my post table and I had the data extracted from the variables I set earlier ($db_name and $db_table). The condition set was to only get the information where my variable $thread_id aplies and in the end set the $limit and show 5 records. If you properly examine it, it gets as easy as reading a comic book at one point

Parsing the BB-code and output the posts
Now it's time to get things going, parse the bb-code (and in my case also strip it of quotes) and get the information returned onto your screen.

PHP Code:
{
// Strip the text of QUOTES
$pagetext1 strip_quotes($output3['pagetext']);
// Parse the text for BB CODE, look at line 7 for reference
$parsed_poster $parser->do_parse($pagetext1falsefalsetruefalsefalsefalse);
$parsed_post $parsed_poster// Not a clue why I build in this step
// Output your 5 posts on your non-vb page
echo "<b>".$output3['username']."(".$output3['userid'].")</b><br />;".$parsed_post."<br />&nbsp;(".$output3['threadid'].")<br />";

As you can see in the 2nd line, I stripped the quote tags and its content from my posts, as I simply don't like to show that bit, as in my opinion it clutters the page.

Next up is the actuall parsing of the BB-Code. Remember when I commented out that line at the beginning of this article?

This is why and I urge you to only use this to compare the two lines, do not add them (again):
Code:
$parsed_text = $parser->do_parse($text, $do_html, $do_smilies, $do_bbcode, $do_imgcode, $do_nl2br, $cachable);
$parsed_poster = $parser->do_parse($pagetext1, false, false, true, false, false, false);
Now, the starting variable is irrelevant to the idea behind this. What I would like to make clear with this is that you have to look at what you're doing. I noticed that people had problems with getting the parse line set up properly. But if you now compare the two, I think it becomes quite clear.

What I am basically telling the parser to do is the following
Quote:
$pagetext1 (this is what I set earlier, that contains the pagetext (read: post) information)
$do_html = false (NO, do not parse HTML code --> security risk)
$do_smilies = false (NO, do not show any smilies)
$do_bbcode = true (YES, parse normal BB code for me)
$do_imgcode = false (NO, do not show images but their url instead)
$do_nl2br = false (NO, I don't want you to use linebreaks --> I use it for a comment system)
$cachable = false (NO, do not cache the contents --> Not sure of the impact but set to false)
You can probably see the $parsed_post line. I don't have any clue why I built that in anymore.. Hehe.. I think it was because I added a limit of 200 characters or something and ran into problems with quoted text (this was before I added the strip quote).

Then last but not least, the output to a browser client and we're done.

For those who don't want to pickup the bits and pieces, below the full code where everything is put together (again without code for db connection).

Full code
PHP Code:
<?
$curdir = getcwd ();
chdir('/path to forum');
require_once('/path to forum/global.php');
require_once('/path to forum/includes/class_bbcode.php');
$parser =& new vB_BbCodeParser($vbulletin, fetch_tag_list());
// $parsed_text = $parser->do_parse($text, $do_html, $do_smilies, $do_bbcode, $do_imgcode, $do_nl2br, $cachable);    
chdir ($curdir);

// Set variables
$db_name = 'your_forum_db'; // for example 'forums'
$db_table = 'your_post_table'; // for example 'prefix_post'
$thread_id = 'thread_id'; // for example '6201'
  
    echo "<p>5 latest post from thread X set to ASC</p>";

    // Determine postcount on thread and deduct 5    
    $max = "SELECT COUNT(postid) AS highest_id FROM ".$db_name.".".$db_table." WHERE threadid=".$thread_id;
    $max_result = mysql_query($max);
    $max_output = mysql_fetch_array($max_result);
    $limit = $max_output['highest_id']-5;
    
    // Query the information you want
    $query3 = "SELECT postid, username, userid , threadid, pagetext FROM ".$db_name.".".$db_table." WHERE threadid=".$thread_id." ORDER BY postid ASC LIMIT ".$limit.",5";    
    $result3 = mysql_query($query3);
     while ($output3=mysql_fetch_array($result3))
       {
        // Strip the text of QUOTES
        $pagetext1 = strip_quotes($output3['pagetext']);
        // Parse the text for BB CODE, look at line 7 for reference
        $parsed_poster = $parser->do_parse($pagetext1, false, false, true, false, false, false);
        $parsed_post = $parsed_poster; // Not a clue why I build in this step
        // Output your 5 posts on your non-vb page
        echo "<b>".$output3['username']."(".$output3['userid'].")</b><br />;".$parsed_post."<br />&nbsp;(".$output3['threadid'].")<br />";
       }    
?>
Conclusion
Well there you go, connect it to a database, get the PATH to your forum right and put it in a php file and see if it works. Hope this was useful to some people and gives a bit more insight of what is needed to fetch information from post and parse bb-code. And if anyone has suggestions, please be my guest, can only be beneficial to us all :up:
Reply With Quote
  #12  
Old 04-13-2012, 04:02 PM
mahmo0od mahmo0od is offline
 
Join Date: Nov 2011
Posts: 20
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

thank u friend
i will try and come back to tell you the results

back soon >>>>>
Reply With Quote
  #13  
Old 06-24-2012, 12:49 PM
mohammad6006's Avatar
mohammad6006 mohammad6006 is offline
 
Join Date: May 2008
Location: IRAN (Tabriz)
Posts: 116
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in forum/includes/class_bootstrap.php on line 26
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:14 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.03791 seconds
  • Memory Usage 2,250KB
  • Queries Executed 19 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)bbcode_code
  • (6)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (3)post_thanks_box
  • (3)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (3)post_thanks_postbit_info
  • (2)postbit
  • (3)postbit_onlinestatus
  • (3)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete