MyBB Community Forums

Full Version: News On Site Homepage (Mod for 1.6.5)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello All,
I just started changing my site over from using a phpbb3 board to now using mybb. Things have been troubling at times trying to make my main site compatible with mybb. One of the main things, is on my site I display news postings from the forums. I looked for a mod with mybb and found only out of date very basic plugins. So I decided I needed to make it myself. It's still pretty basic and just one page of code. The PHP probably isn't written in the best way, but it works. Only requirement is the MyBBIntegrator and the code below.

Let me know what you think please.
Thanks
SkaFreaks

View the results here: http://www.bashmentfest.com

and here is the code

EDIT: Added basic bbCode support. Will replace bbcode with html.

<?php
//MAKE CHANGES BELOW//
define('IN_MYBB', 1);
require_once 'forum/global.php';
require_once 'forum/inc/class.MyBBIntegrator.php';
$MyBBI = new MyBBIntegrator($mybb, $db, $cache, $plugins, $lang, $config);

define('PRINT_TO_SCREEN', true);			// Whether or not to display the results. true | false
define('FORUM_ID', '20,21,23');				// Forum ID(s) to get data from. Seperate multiple forums with (,) comma. Examples: 20 | 20,21,4
define('POST_LIMIT', '10');					// How many posts to display. Examples: 10 | 20
define('MYBB_ROOT_PATH', 'forum/');			// Path to the forum (including trailing /). Examples: forum/ | mybb/
define('SHOW_REPLIES', false);				// Whether or not to show replies to the original post. true | false

//MAKE CHANGES ABOVE//

//DO NOT EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING//
			
$fids = explode(',', FORUM_ID);	
$fcount = count($fids);
$forumids = "(";
for ($i=0; $i<$fcount; $i++){
if ($i==0) { $forumids .= "t.fid = $fids[$i]"; } else { $forumids .= " OR t.fid = $fids[$i]"; } 
}
$forumids .= ")";
if (SHOW_REPLIES) { $news_replies = ""; } else { $news_replies = "AND p.replyto='0'"; }

		$query = $db->query("
			SELECT t.tid,t.fid,t.subject,t.uid,t.username,t.dateline,t.replies,p.message 
			FROM ".TABLE_PREFIX."threads t 
			LEFT JOIN ".TABLE_PREFIX."posts p ON (t.tid = p.tid)
			WHERE ".$forumids." AND t.visible='1' AND t.closed='0' $news_replies 
			ORDER BY dateline DESC LIMIT ".POST_LIMIT
		);
		
$posts = array();
$news = array();
$message = '';
$poster_id = 0;

$count = mysql_num_rows($query);
while ($news = $db->fetch_array($query))
{
   $posts[] = array(
         'tid' => $news['tid'],
         'fid' => $news['fid'],
         'subject' => $news['subject'], 
         'uid' => $news['uid'], 
         'username' => $news['username'], 
         'dateline' => $news['dateline'],
         'replies' => $news['replies'],
         'message' => $news['message'],
         );
}
	
// Output the posts
foreach($posts as $m)
{

		$query2 = $db->query("SELECT * FROM ".TABLE_PREFIX."users WHERE uid='".$m['uid']."'");
		while ($user_data = $db->fetch_array($query2)) {
		$timezone = $user_data['timezone'];
		}
		
	$post_date = date('Y-m-d H:i:s',$m['dateline']);
		
   $poster_id = $m['uid'];
   
   $message = $m['message'];

   $message = str_replace("\n", '<br>', $message);
    //BBcode to Find..
        $in = array( 
                     '/\[b\](.*?)\[\/b\]/si',    
                     '/\[i\](.*?)\[\/i\]/si',
                     '/\[u\](.*?)\[\/u\]/si',
                     '#\[img\](https?://[-A-Z0-9+&@\#/%?=~_|!:,.;]*[-A-Z0-9+&@\#/%=~_|]\.(?:jpg|jpeg|gif|png|bmp))\[\/img\]#si',
                     '#\[email\]([-A-Z0-9+&@\#/%?=~_|!:,.;]*[-A-Z0-9+&@\#/%=~_|])\[\/email\]#si',
                     '#\[url\=((?:ftp|https?)://[-A-Z0-9+&@\#/%?=~_|!:,.;]*[-A-Z0-9+&@\#/%=~_|])\](.*?)\[\/url\]#si',
                     '/\[size\="?(.*?)"?\](.*?)\[\/size\]/si',
                     '/\[color\="?(.*?)"?\](.*?)\[\/color\]/si',
                     '/\[list\=(.*?)\](.*?)\[\/list\]/si',
                     '/\[list\](.*?)\[\/list\]/si',
                     '/\[\*\]\s?(.*?)\n/si'
    );
    // And replace them by...
    $out = array(
                     '<strong>\1</strong>',
                     '<em>\1</em>',
                     '<u>\1</u>',
                     '<img src="\1" alt="\1" />',
                     '<a href="mailto:\1">\1</a>',
                     '<a href="\1">\2</a>',
                     '<span style="font-size:\1%">\2</span>',
                     '<span style="color:\1">\2</span>',
                     '<ol start="\1">\2</ol>',
                     '<ul>\1</ul>',
                     '<li>\1</li>'
    );
    $message = preg_replace($in, $out, $message);

   $comment = ($m['replies']==1) ? 'comment' : 'comments';
   
   if( PRINT_TO_SCREEN )
   {
      /* Output is in the following format
       *
       * <h3>Thread Title</h3>
       ^ <h4 class="postinfo">date // 5 comments // poster</h4>
       * <p>First post test</p>
       * 
       */

echo "
<table border='0' cellpadding='2' cellspacing='1' width='100%' bgcolor='#000000'><tr><td>
<table border='0' cellpadding='2' cellspacing='0' width='100%'>
<tr>
<td valign='top' rowspan='2' bgcolor='#ffffff'><img src='$newsimage' height='35' alt='News Image'></td>
<td width='70%' align='left' bgcolor='#ffffff'><b><font class='fontg'>{$m['subject']}</font></b></td><td  align='right' bgcolor='#ffffff' nowrap='nowrap'><font class='fontg'>".$post_date."</font></td></tr>
<tr>
<td width='50%' align='left' bgcolor='#ffffff'><font class='fontg'>Posted By <a href=\"".MYBB_ROOT_PATH."member.php?action=profile&uid={$m['uid']}\">{$m['username']}</a></font></td>
<td align='right' bgcolor='#ffffff'><a href=\"".MYBB_ROOT_PATH."showthread.php?tid={$m['tid']}\">{$m['replies']} {$comment}</a></td></tr>
</table></td></tr>
<tr><td>

<table border='0' cellpadding='2' cellspacing='0' width='100%'><tr><td bgcolor='#ffffff'><font class='fontg'>{$message}</font></td></tr></table>

</td></tr></table><br>";

   }
   else
   {
      $news[] = array(
            'topic_id' => $m['tid'], // eg: 119
            
            'topic_time' => $user->format_date($m['dateline']), // eg: 06 June, 07 (uses board default)
            'topic_replies' => $m['replies'], // eg: 26
            
            'username' => $m['username'], // eg: chAos
            'topic_title' => $m['subject'], // eg: "News Post"
            
            'post_text' => $message, // just the text         
            );
   }
   
   unset($message,$poster_id);
}
?>
Thanks SkaFreaks!

I'm coming over from phpbb3 also and was looking for "New Anywhere" type of mod, I'm very glad you took the time to make one.

I have implemented this mod in my mybb but I only see a blank page. Am I doing anything wrong?

My mybb installation is fresh, all default settings with default Forum, with one new topic created under the default forum.

Here is how my changes look like for your mod:
<?php
//MAKE CHANGES BELOW//
define('IN_MYBB', 1);
require_once 'ffaae/global.php';
require_once 'ffaae/inc/class.MyBBIntegrator.php';
$MyBBI = new MyBBIntegrator($mybb, $db, $cache, $plugins, $lang, $config);

define('PRINT_TO_SCREEN', true);            // Whether or not to display the results. true | false
define('FORUM_ID', '1,2');                // Forum ID(s) to get data from. Seperate multiple forums with (,) comma. Examples: 20 | 20,21,4
define('POST_LIMIT', '10');                    // How many posts to display. Examples: 10 | 20
define('MYBB_ROOT_PATH', 'ffaae/');            // Path to the forum (including trailing /). Examples: forum/ | mybb/
define('SHOW_REPLIES', false);                // Whether or not to show replies to the original post. true | false

//MAKE CHANGES ABOVE//

//DO NOT EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING//
            
$fids = explode(',', FORUM_ID);    
$fcount = count($fids);
$forumids = "(";
for ($i=0; $i<$fcount; $i++){
if ($i==0) { $forumids .= "t.fid = $fids[$i]"; } else { $forumids .= " OR t.fid = $fids[$i]"; }
}
$forumids .= ")";
if (SHOW_REPLIES) { $news_replies = ""; } else { $news_replies = "AND p.replyto='0'"; }

        $query = $db->query("
            SELECT t.tid,t.fid,t.subject,t.uid,t.username,t.dateline,t.replies,p.message
            FROM ".TABLE_PREFIX."threads t
            LEFT JOIN ".TABLE_PREFIX."posts p ON (t.tid = p.tid)
            WHERE ".$forumids." AND t.visible='1' AND t.closed='0' $news_replies
            ORDER BY dateline DESC LIMIT ".POST_LIMIT
        );
        
$posts = array();
$news = array();
$message = '';
$poster_id = 0;

$count = mysql_num_rows($query);
while ($news = $db->fetch_array($query))
{
   $posts[] = array(
         'tid' => $news['tid'],
         'fid' => $news['fid'],
         'subject' => $news['subject'],
         'uid' => $news['uid'],
         'username' => $news['username'],
         'dateline' => $news['dateline'],
         'replies' => $news['replies'],
         'message' => $news['message'],
         );
}
    
// Output the posts
foreach($posts as $m)
{

        $query2 = $db->query("SELECT * FROM ".TABLE_PREFIX."users WHERE uid='".$m['uid']."'");
        while ($user_data = $db->fetch_array($query2)) {
        $timezone = $user_data['timezone'];
        }
        
    $post_date = date('Y-m-d H:i:s',$m['dateline']);
        
   $poster_id = $m['uid'];
  
   $message = $m['message'];

   $message = str_replace("\n", '<br>', $message);
    //BBcode to Find..
        $in = array(
                     '/\[b\](.*?)\[\/b\]/si',    
                     '/\[i\](.*?)\[\/i\]/si',
                     '/\[u\](.*?)\[\/u\]/si',
                     '#\[img\](https?://[-A-Z0-9+&@\#/%?=~_|!:,.;]*[-A-Z0-9+&@\#/%=~_|]\.(?:jpg|jpeg|gif|png|bmp))\[\/img\]#si',
                     '#\[email\]([-A-Z0-9+&@\#/%?=~_|!:,.;]*[-A-Z0-9+&@\#/%=~_|])\[\/email\]#si',
                     '#\[url\=((?:ftp|https?)://[-A-Z0-9+&@\#/%?=~_|!:,.;]*[-A-Z0-9+&@\#/%=~_|])\](.*?)\[\/url\]#si',
                     '/\[size\="?(.*?)"?\](.*?)\[\/size\]/si',
                     '/\[color\="?(.*?)"?\](.*?)\[\/color\]/si',
                     '/\[list\=(.*?)\](.*?)\[\/list\]/si',
                     '/\[list\](.*?)\[\/list\]/si',
                     '/\[\*\]\s?(.*?)\n/si'
    );
    // And replace them by...
    $out = array(
                     '<strong>\1</strong>',
                     '<em>\1</em>',
                     '<u>\1</u>',
                     '<img src="\1" alt="\1" />',
                     '<a href="mailto:\1">\1</a>',
                     '<a href="\1">\2</a>',
                     '<span style="font-size:\1%">\2</span>',
                     '<span style="color:\1">\2</span>',
                     '<ol start="\1">\2</ol>',
                     '<ul>\1</ul>',
                     '<li>\1</li>'
    );
    $message = preg_replace($in, $out, $message);

   $comment = ($m['replies']==1) ? 'comment' : 'comments';
  
   if( PRINT_TO_SCREEN )
   {
      /* Output is in the following format
       *
       * <h3>Thread Title</h3>
       ^ <h4 class="postinfo">date // 5 comments // poster</h4>
       * <p>First post test</p>
       *
       */

echo "
<table border='0' cellpadding='2' cellspacing='1' width='100%' bgcolor='#000000'><tr><td>
<table border='0' cellpadding='2' cellspacing='0' width='100%'>
<tr>
<td valign='top' rowspan='2' bgcolor='#ffffff'><img src='$newsimage' height='35' alt='News Image'></td>
<td width='70%' align='left' bgcolor='#ffffff'><b><font class='fontg'>{$m['subject']}</font></b></td><td  align='right' bgcolor='#ffffff' nowrap='nowrap'><font class='fontg'>".$post_date."</font></td></tr>
<tr>
<td width='50%' align='left' bgcolor='#ffffff'><font class='fontg'>Posted By <a href=\"".MYBB_ROOT_PATH."member.php?action=profile&uid={$m['uid']}\">{$m['username']}</a></font></td>
<td align='right' bgcolor='#ffffff'><a href=\"".MYBB_ROOT_PATH."showthread.php?tid={$m['tid']}\">{$m['replies']} {$comment}</a></td></tr>
</table></td></tr>
<tr><td>

<table border='0' cellpadding='2' cellspacing='0' width='100%'><tr><td bgcolor='#ffffff'><font class='fontg'>{$message}</font></td></tr></table>

</td></tr></table><br>";

   }
   else
   {
      $news[] = array(
            'topic_id' => $m['tid'], // eg: 119
            
            'topic_time' => $user->format_date($m['dateline']), // eg: 06 June, 07 (uses board default)
            'topic_replies' => $m['replies'], // eg: 26
            
            'username' => $m['username'], // eg: chAos
            'topic_title' => $m['subject'], // eg: "News Post"
            
            'post_text' => $message, // just the text        
            );
   }
  
   unset($message,$poster_id);
}
?>
Hey,
Thanks for the thanks. The issue with it not showing up has to do with the 'closed thread'. The query looks for the closed column to be 0, but the default entry for closed is nothing. So there are a few options.
1. Remove "AND t.closed='0'" from the query (This will then display closed threads)
2. Edit the Threads table and make the default value for closed = 0
3. Change "AND t.closed='0'" to "AND (t.closed='0' OR t.closed='')" (haven't tested this but it should work)

I chose option 2 for myself.
EDIT: Fixed it

Thanks