Script to read posts in a forum from PHPBB 3 database

masshuu

Head of the Geese
Community Support
Enemy of the State
Messages
2,293
Reaction score
50
Points
48
I made this to act kind of like a news thing. Basicly i have a forum section thats titled news, this reads the poasts and lists them, cutting them off at a certian number of chars or lines.

You will need to modify it slightly, but for the most part at the end you have the value $PageData that has all the html tags.

if you want an example, visit
http://www.stormraidergames.com/Forums/viewforum.php?f=4
http://www.stormraidergames.com/News

and i try to use self describing variable names.

PHP:
<?
$News['Database']="CHANGE_ME";
$News['User']="CHANGE_ME";
$News['Password']="CHANGE_ME";
$News['server']="localhost";

//The forum ID, found by going to the forum and getting the id of it in the url, ie:
//http://www.stormraidergames.com/Forums/viewforum.php?f=4
//So in my case its 4
$News['forum_id'] = 4;

//The number of recient posts to get
$News['Limit_News_Count'] = 5;

//The maximum allowd chars before cutting off
$News['Limit_Post_CharCount'] = 1500;

//The maximum allowd lines before cutting off
$News['Limit_Post_RowCount'] = 15;

//Path to your forums from this files location
$News['ForumsPath'] = "Forums";

//Date format, look at the folowing website for help
//http://www.php.net/date
$News['Date_Format'] = 'D M d, Y g:i a';



//Sould not need to modify below this.

 
function cutText($string, $length) {
    while ($string{$length} != " " && $string{$length} != "\n" ) {
        $length--;
    }
    return substr($string, 0, $length);
}


$News['Topic_List_Query_String'] = "SELECT `topic_id`, `topic_title`, `topic_poster`, `topic_time`, `topic_replies`, `topic_first_poster_name` FROM `phpbb_topics` WHERE `forum_id` = '{$News['forum_id']}' ORDER BY `topic_id` DESC LIMIT 0 , {$News['Limit_News_Count']} ";


$News['db_connect_id'] = mysqli_connect($News['server'], $News['User'], $News['Password'], $News['Database']) or die("Unable to connect to forum database");


$News['mysqli_query_Result'] = mysqli_query($News['db_connect_id'], $News['Topic_List_Query_String']);


$PageData .= "<h3>News</h3><hr/>";

while($Row = $News['mysqli_query_Result']->fetch_array(MYSQLI_ASSOC))
{
 $News['Post_Query_String'] = "SELECT `post_id`, `topic_id`, `post_subject`, `post_text` FROM `phpbb_posts` WHERE `topic_id` = '{$Row['topic_id']}' AND `forum_id` = '{$News['forum_id']}'";
 
 $News['mysqli_query_Result_Topics'] = mysqli_query($News['db_connect_id'], $News['Post_Query_String']);
 $PostData = $News['mysqli_query_Result_Topics']->fetch_array(MYSQLI_ASSOC);
 
 //The actuall post content
 $PostText = $PostData['post_text'];
 
 $AddedDots = false;
  
 if(strlen($PostText) > $News['Limit_Post_CharCount'])
 {
  $PostText = cutText($PostText, $News['Limit_Post_CharCount']);
  $PostText .= "<a href=\"{$News['ForumsPath']}/viewtopic.php?f={$News['forum_id']}&t={$Row['topic_id']}\">...</a>";
  $AddedDots = true;
 }
 
 //Copyed from the phpbb bbcode file.
 $PostText = str_replace('  ', '&nbsp; ', $PostText);
 $PostText = str_replace('  ', ' &nbsp;', $PostText);
 
 //New lines.
 $PostText = str_replace('\n', ' <br/>\n', $PostText);
 $PostText = nl2br($PostText);
 
 //Remove bb tags
 $pattern = '/\[[\/a-zA-Z0-9=*,#]*:[\/a-zA-Z0-9]*\]/';
 $pattern2 = '/\[[\/a-zA-Z0-9=*,#]*:[\/a-zA-Z0-9]*:[\/a-zA-Z0-9]*\]/';
 $pattern3 = '/\[[\/a-zA-Z0-9=*,#]*:[\/a-zA-Z0-9]*:[\/a-zA-Z0-9]*\]/';

 $PostText = preg_replace($pattern, '', $PostText);
 $PostText = preg_replace($pattern2, '', $PostText);
 $PastPostText = $PostText;
 
 //cut off at 25 lines
 if(substr_count($PostText,"<br") > $News['Limit_Post_RowCount'])
 {
  $AtPos = 0;
  $pos = 0;
  $Pastpos = -1;
  while($AtPos < $News['Limit_Post_RowCount'])
  {
    $pos = strpos($PostText, "<br", $pos + 1); //take into account > or />
    if($pos > $Pastpos)
      $Pastpos = $pos;
    else
    {
      $pos = $Pastpos;
    break;}
    
    $AtPos++;
  }
 
 
  $PostText = substr($PostText, 0, $pos);
   if($PastPostText != $PostText && $AddedDots == false)
     $PostText .= "<a href=\"{$News['ForumsPath']}/viewtopic.php?f={$News['forum_id']}&t={$Row['topic_id']}\">...</a>";

 }
  
  
 
 $PostedAt = date($News['Date_Format'], $Row['topic_time']); 

 
 //Lines that prints the news post
 $PageData .= "<h4> <a href=\"{$News['ForumsPath']}/viewtopic.php?f={$News['forum_id']}&t={$Row['topic_id']}\">{$Row['topic_title']}</a></h4>";
 $PageData .= "<sub>Posted by <a href=\"{$News['ForumsPath']}/memberlist.php?mode=viewprofile&u={$Row['topic_poster']}\">{$Row['topic_first_poster_name']}</a> on $PostedAt</sub><br>----------<br>";
 $PageData .= "$PostText";
}
$News['db_connect_id']->close();
?>
 
Top