Displaying News on your homepage
#1
I'm sure there was a tutorial on this before but it seems to have vanished... oh well.

This tutorial will enable you to show posts from a specific forum in your MyBB forum on your website's homepage.

This code should be placed at the top of your page (before any output):
<?php

	$fid = 2;
	$limit = 5;
	$forumpath = 'forum/';

	chdir($forumpath);
	define("IN_MYBB", 1);
	require('./global.php');
	require_once MYBB_ROOT."inc/class_parser.php";
	$parser = new postParser;
	chdir('../');
?>

This code should then be placed where you would like the news to be shown:
<?php
	
	$query = $db->simple_select(TABLE_PREFIX.'threads', '*', "fid='{$fid}' ORDER BY tid DESC LIMIT {$limit}");
	if($db->num_rows($query) > 0)
	{
		while($row = $db->fetch_array($query))
		{
			$query2 = $db->simple_select(TABLE_PREFIX.'posts', '*', "pid='{$row['firstpost']}'");
			$row2 = $db->fetch_array($query2);
			
			$date = my_date($mybb->settings['dateformat'], $row2['dateline'], "", 1);
			$time = my_date($mybb->settings['timeformat'], $row2['dateline'], "", 1);

			$options = array(
							'allow_html' => 'no', 
							'filter_badwords' => 'yes', 
							'allow_mycode' => 'yes', 
							'allow_smilies' => 'yes', 
							'nl2br' => 'yes', 
							'me_username' => 'yes'
							);
			$message = $parser->parse_message($row2['message'], $options);
			
			echo("<a href=\"{$forumpath}showthread.php?tid={$row['tid']}\">{$row['subject']}</a> - 
				Posted: {$date} {$time} by <a href=\"{$forumpath}member.php?action=profile&uid={$row2['uid']}\">{$row2['username']}</a><br />");
			echo("{$message}<br /><br />");
			echo("Replies (<a href=\"{$forumpath}showthread.php?tid={$row['tid']}\">{$row['replies']}</a>)<br /><hr />");
		}
	}
	else
	{
		echo 'Nothing to display.';
	}

?>

Then you need to replace:
- The 2 in "$fid = 2" with the fid of the forum you want to show the posts from
- The 5 in "$limit = 5" with whatever you want your limit to be.
- The 'forum/' in chdir('forum/') to your forum path relative to the page this is being called from.
- The '../' in chdir('../') to the path back from your forum to the page this is being called from.

You will also need to change the code in the echo() statements to reflect how you want the news to be displayed on your site.

UPDATE - PLEASE READ
[Image: sig.php]
The little boat gently drifted across the pond exactly the way a bowling ball wouldn't.
Reply
#2
I tried. It didn't work. See here for the error:
<Removed to protect others from Trojans (site was hacked)>
Reply
#3
Remove the '.'

require('.http://stcd.createmybb.com/global.php');

See the dot before the http? Yea, remove it.
Cross Industries, LLC - Affordable Technology Services
Phone: (843) 879-8293 E-mail: [[email protected]]Contact Us[/email]
Reply
#4
Your forum is a CreateMyBB one? This will only work if the forum is on the same server as the website.

I suppose if you wanted to do this with a CreateMyBB forum, you could create a RSS feed for the forum you wanted content from and then integrate an RSS parser and a post parser into your website to parse the information from the RSS feed. If you just wanted the titles of the threads with a link to the post, that would be a lot easier because then you wouldn't need to make your own post parser.
[Image: sig.php]
The little boat gently drifted across the pond exactly the way a bowling ball wouldn't.
Reply
#5
Hmm MrDoom, this is a useful one, but will it show (number of) comments as well?
If it does, im going to integrate it with my website design.
MapleHub Recreation!
http://maplehub.ahplace.com
Reply
#6
Yeah:
echo("Replies (<a href=\"{$forumpath}showthread.php?tid={$row['tid']}\">{$row['replies']}</a>)<br /><hr />");
Shows the number of replies a thread has.
[Image: sig.php]
The little boat gently drifted across the pond exactly the way a bowling ball wouldn't.
Reply
#7
Hmm thanks alot. But then, does it automatically make another post page?
Like when the first 5 announcement its been in the main site, then when i add another post, will it automatically make a new page? It might be a good idea to make that if possible.
MapleHub Recreation!
http://maplehub.ahplace.com
Reply
#8
It will always show the latest (by default) 5 threads from a forum. If you make a new post, it will appear at the top and the previous 5th thread will vanish.

If you want to change it to show all the threads in a forum and add pagination to it, feel free. It is however outside the scope of this tutorial.
[Image: sig.php]
The little boat gently drifted across the pond exactly the way a bowling ball wouldn't.
Reply
#9
I see. thanks for the info!
MapleHub Recreation!
http://maplehub.ahplace.com
Reply
#10
If you want "Multipage".

<?php
	// START VARS
		$fid = 2; // FORUM ID
   		$perpage = 2; // ITEMS PER PAGE
  		$forumpath = "/"; // FORUM PATH
		$filename = ""; // NAME OF THE FILE
	// END VARS

	// START DONT TOUCH STUFF
   		chdir($forumpath);
    		define("IN_MYBB", 1);
    		require('./global.php');
    		require_once MYBB_ROOT."inc/class_parser.php";
    		$parser = new postParser;
   		chdir("../");
	// END DONT TOUCH STUFF
    	
	// INCOMING
	$page = intval($mybb->input['page']);

	// COUNT RECORDS
	$query = $db->simple_select(TABLE_PREFIX."threads", "COUNT(*) AS threads", "fid='{$fid}'");
	$num = $db->fetch_field($query, "threads");

	// PAGING ( MULTI )
	if($page)
	{
		$start = ($page - 1) * $perpage;
	}
	else
	{
		$start = 0;
		$page = 1;
	}
	$multipage = multipage($num, $perpage, $page, $filename."?");

	// GET THREADS
	$query = $db->simple_select(TABLE_PREFIX."threads", "*", "fid='{$fid}' ORDER BY tid DESC LIMIT $start,$perpage");
    	if($db->num_rows($query) > 0)
    	{
        	while($row = $db->fetch_array($query))
        	{
           		$query2 = $db->simple_select(TABLE_PREFIX.'posts', '*', "tid='{$row['tid']}' LIMIT 1");
            	$row2 = $db->fetch_array($query2);
            
           		$date = my_date($mybb->settings['dateformat'], $row2['dateline'], "", 1);
            	$time = my_date($mybb->settings['timeformat'], $row2['dateline'], "", 1);

			$options = array(
							'allow_html' => 'yes', 
							'filter_badwords' => 'yes', 
							'allow_mycode' => 'yes', 
							'allow_smilies' => 'yes', 
							'nl2br' => 'yes', 
							'me_username' => 'yes'
							);
			$message = $parser->parse_message($row2['message'], $options);
            
           		echo("<a href=\"{$forumpath}showthread.php?tid={$row['tid']}\">{$row['subject']}</a> - 
               		Posted: {$date} {$time} by <a href=\"{$forumpath}member.php?action=profile&uid={$row2['uid']}\">{$row2['username']}</a><br />");
            		echo("{$message}<br /><br />");
            		echo("Replies (<a href=\"{$forumpath}showthread.php?tid={$row['tid']}\">{$row['replies']}</a>)<br /><hr />");
        	}
		echo "<div style=\"float:right\">{$multipage}</div>";
    	}
    	else
   	{
        	echo "Nothing to display.";
    	}
?>

Change the VARS to your needs and save.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)