MyBB Community Forums

Full Version: mybb intergration help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi, I would like to put the first 15 posts from a forum on my homepage in an I-frame. For example, the home page would be index.html and the frame would be the first fifteen posts from the news forum generated from the file news.php. I found a thread about this earlier, but didn't understand what they said at that time and cannot find the thread again. If anybody could help me with this, it would be greatly appreciated. Also, I don't know any PHP , but it would still be nice if somebody could help me. Thanks
I can help you with this in a few days.
k, you rock!
never got help, could some one elce help me?
this will help you

regards
Personally, I'd rather use the code already provided by MyBB, as it makes it easier and is probably a lot cleaner.

I do it simply like this (for my website, I put all the posts in the announcements forum onto my main page):

On the page you wish to include posts, at the top of the page before everything, put:
<?php
chdir('forums'); // path to MyBB
require './global.php';
require './inc/functions_post.php';
?>
The global gives you all the functions required to retrieve the actual posts. The functions_post gives you the functions to parse the posts and output them on your page.

You can do it easily like this:
$query = $db->query("SELECT * FROM `".TABLE_PREFIX."posts` WHERE `fid` = 2 AND `replyto` = 0 ORDER BY `pid` DESC LIMIT 15");
while ($post = $db->fetch_array($query))
{
	$post['date'] = mydate($mybb->settings['dateformat'].", ".$mybb->settings['timeformat'], $post['dateline'], "", 0);
	// Parse the message
	if($post['smilieoff'] == "yes")
	{
		$allowsmilies = "no";
	}
	else
	{
		$allowsmilies = $forum['allowsmilies'];
	}
	$post['message'] = postify($post['message'], $forum['allowhtml'], $forum['allowmycode'], $allowsmilies, $forum['allowimgcode'], "yes");
	// do me code
	if($forum['allowmycode'] != "no")
	{
		$post['message'] = domecode($post['message'], $post['username']);
	}
	
	$avatar_query = $db->query("SELECT `avatar` FROM `".TABLE_PREFIX."users` WHERE `username` = '".$post['username']."' LIMIT 1");
	$post_avatar = $db->fetch_array($avatar_query);
	
	$reply_query = $db->query("SELECT * FROM `".TABLE_PREFIX."threads` WHERE `tid` = '". $post['tid'] ."' LIMIT 1");
	$thread_replies = $db->fetch_array($reply_query);
You can probably clean that code up a bit. I needed to make several extra variables to target the user table and thread table to get extra info. Basically the user's avatar and how many replies the thread had.

Also take note of the replyto = '0', as that makes it so it only retrieves the first post from each thread in the forum. If that's not your aim, then get rid of it.

You may not want all that, so tidy it up to what you want. Also make sure to change the fid in the first query to the forum id that you wish to pull the posts from.

Finally, just echo the contents to where you want, eg:
if ($post_avatar['avatar'])
	{
	echo '
		<div class="avatar">
		<img src="'. $post_avatar['avatar'] .'" alt="User Avatar" />
		</div>
		';
	}
	echo '	
		<div class="subject">'. $post['subject'] .'</div>
		<div class="date_author">Posted at '. $post['date'] .' by <a href="forums/member.php?action=profile&amp;uid='. $post['uid'] .'">'. $post['username'] .'</a></div>
		<div class="post">'. $post['message'] .'</div>
	';

Obviously you'll want to modify that to how you want, as that's just an example. Hope this helps you. It should be pretty straight forward with that.

I also leave it to your responsibility to check for any errors in that code, or any optimisations. I just posted this quickly to give you an idea of how I do it.
however, this is good and all, but, it loads all the things that is required for MyBB if you include general.php, and well, face it, you only need so little.
This thread will help you -> http://community.mybboard.net/showthread...1#pid24911
ok so i used the code from the page that CraKteR linked to, however when i run the page i get "No Database Selected" what should i do?
You changed this?
$forumpath = "./forums/"; // Path to your forums root directory (with trailing slash)
$fid = "15"; // The forum ID to pull the annoucements from
$limit = "7"; // The amount of annoucements to show

$forumpath = path to your forum, f.ex if you have your page an path down from the forum, and the forum is in folder called forum, you change it to
$forumpath = "./forum/";
understand?
limit is how many posts you want to display, fid is the fid for the forum you want to select and display from,.
yep here's my code:

<?php 

$forumpath = "http://forum.backstageneopets.com/"; // Path to your forums root directory (with trailing slash)
$fid = "3"; // The forum ID to pull the annoucements from
$limit = "15"; // The amount of annoucements to show

function bb_code(&$string){
    $pattern = array(
        //[ul]
        '#\[list\](.*?)\[\/list\]#si',
        //[*]
        '#\[\*\](.*?)(\r\n|\r|\n|<br>|<br />|<br/>)#si',
        //[url]
        '#\[url\](.*?)\[\/(url)\]#si',
        //[url=XXX]
        '#\[url=(&quot;|"|\'|)(.*?)\\1\](.*?)\[\/url\]#si',       
        //[i],[u]
        '#\[u\](.*?)\[/u\]#si',
        '#\[i\](.*?)\[/i\]#si',
        //[b]
        '#\[b\](.*?)\[/b\]#si',
        //[img]
        '#\[img\](.*?)\[\/img\]#si',
        // font size
        '#\[size=(&quot;|"|\'|)([0-9\+\-]+)\\1\](.*?)\[\/size\]#si',        '#(\r|\n|\r\n)#si');

    $replacement = array(
        //ul
        "\t\t<ul>$1</ul>\r\n",
        //li
        "\t\t\t<li>$1</li>\r\n",
        //a
        '<a href="$1">$1</a>',
        //a
        '<a href="$2">$3</a>',
        //i, u
        '<u>$1</u>',
        '<em>$1</em>',
        //b
        '<strong>$1</strong>',
        //img                             
        '<img src="$1" alt="User Posted Image" />',
        //size                  
        '<font size="\2">\3</font>',        "<br />");
    $string = preg_replace ($pattern, $replacement, stripslashes($string) );    $string = str_replace ('<br /><br />', "</p>\r\n\t\t<p>", $string);
}
mysql_select_db($config['database'], mysql_connect($config['localhost'], $config['user'], $config['pass']));

$query = mysql_query("SELECT t.tid,t.subject,t.uid,t.username,t.dateline,t.replies,p.message,u.avatar FROM ".$config['table_prefix']."threads t LEFT JOIN ".$config['table_prefix']."posts p ON (t.tid = p.tid) LEFT JOIN ".$config['table_prefix']."users u ON (t.uid = u.uid) WHERE t.fid='".$fid."' AND t.visible='1' AND t.closed NOT LIKE 'moved|%' AND p.replyto='0' ORDER BY t.tid DESC LIMIT 0, ".$limit."") or die(mysql_error());
while ($news = mysql_fetch_array ($query)){
    $date = gmdate ("jS F Y", $news['dateline']);
    $time = gmdate ("h:i A", $news['dateline']);

    //Parse BBCode
    bb_code($news['message']);

    if($news['replies'] == "1"){
        $replytext = "reply";
    } else {
        $replytext = "replies";
    }

    if($news['avatar'] != ""){
        if(substr($news['avatar'], 0, 7) != "http://")
        {
            $url = $forumpath;
        }
        $avatarbit = "<img style='float:left; margin:10px;'src=\"".$url.$news['avatar']."\" alt=\"".$url.$news['avatar']."\" />\r\n\t\t";
    } else {
        $avatarbit = "";
    }

    echo "\t<div class=\"news\">\r\n\t\t";
    echo "<div class='details'>(<a href=\"".$forumpath."showthread.php?tid=".$news['tid']."\">".$news['replies']." ".$replytext." </a>) | Posted by <a href=\"".$forumpath."member.php?action=profile&amp;uid=".$news['uid']."\">".$news['username']."</a> on ".$date." at ".$time."</div>\r\n\t\t";
    echo "<h2>".$news['subject']."</h2>\r\n\t\t";
    echo $avatarbit."<p>".$news['message']."</p>\r\n\t\t<div style=\"clear:both;\"></div>\r\n\t";
    echo "</div>\r\n";
}
?>
Pages: 1 2