MyBB Community Forums

Full Version: Latest Threads On We Website
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I think this is the right place but I am looking for codes to implement on my website from my forums. The one I am looking for now is a "Latest Threads" code so I can display it on the main page of my website, not my forums index.

Thanks
Ok I was able to find a code to implement this but I need some help on limiting the chracters of the title of the thread. Here is the code

<?php
define("IN_MYBB", 1);
require_once("./global.php"); // Change this if needed
$tlimit = 5; // How many titles you want

$query = $db->query("SELECT * FROM ".TABLE_PREFIX."threads ORDER BY tid DESC LIMIT $tlimit");
while($fetch = $db->fetch_array($query)){
echo '<a href="./showthread.php?tid='.$fetch['tid'].'">'.$fetch['subject'].'</a><br />' . "\n";
}
?>
You should be able to use the substr method on the title.
Unfortunately I do not know php, maybe you can assist on code? Just one more thing, what would the code be for "posted by: member"?

Thanks
I was able to go thru some other code and I got my posted by. But its pulling from all the forums. Is there a code to specify which forums to pull from?
Would someone be able to show how to make it show a small blurb from the post my sql skills aren't fantastic? I can make the code to show it I just would like the query to be update to get the post from the table (The original post...not the latest in the thread, but if you supply the latest I can figure out how to change it...probably)

As for the substr thing...just change the value "50" to how ever many characters you want it to allow...

$subject = $fetch['subject'];

echo '<a href="./showthread.php?tid='.$fetch['tid'].'">'.substr($subject,0,50) . ( strlen($subject) > 50 ? '...' : '' ).'</a><br />' . "\n";


If you want to add say another thing like who posted the last post on that thread just add $fetch['lastposter'] into what is being echoed...for who posted it add $fetch['username'].

Also if you want to make it only show posts from a particular board like I did edit the query to include
WHERE fid = '2'

where '2' is set that to the ID of your board Smile
(2012-03-14, 01:32 AM)cwindham Wrote: [ -> ]<?php
define("IN_MYBB", 1);
require_once("./global.php"); // Change this if needed
$tlimit = 5; // How many titles you want

$query = $db->query("SELECT * FROM ".TABLE_PREFIX."threads ORDER BY tid DESC LIMIT $tlimit");
while($fetch = $db->fetch_array($query)){
echo '<a href="./showthread.php?tid='.$fetch['tid'].'">'.$fetch['subject'].'</a><br />' . "\n";
}
?>

(2012-07-09, 07:58 PM)pisoj1 Wrote: [ -> ]
$subject = $fetch['subject'];

echo '<a href="./showthread.php?tid='.$fetch['tid'].'">'.substr($subject,0,50) . ( strlen($subject) > 50 ? '...' : '' ).'</a><br />' . "\n";


If you want to add say another thing like who posted the last post on that thread just add $fetch['lastposter'] into what is being echoed...for who posted it add $fetch['username'].

Also if you want to make it only show posts from a particular board like I did edit the query to include
WHERE fid = '2'

where '2' is set that to the ID of your board Smile

Can we use this in "additional pages" pluggin?

thanks
No idea, give it a go?
The OP code works great but I would like to get the latest thread post content + timestamp as well.

I'm ok with PHP so I know I'll need to concatenate the fetch string. I guess I'm just needing to know what code I need to define the variable with.

Example:
$time = $fetch['time']
$post = $fetch['message']

Could someone provide an example that goes along with the OP code?

Really I'm trying to obtain something like this in my homepage. (See attachment)

Thank you.
(2012-07-18, 11:49 PM)CWB Wrote: [ -> ]The OP code works great but I would like to get the latest thread post content + timestamp as well.

First I would like to say thanks to dave as this code is 100% his...
second this is based of his class files which can be found here:
http://phpdave.com/MyBBIntegrator/

Use that if you would like to do any changes or maybe you'll find exactly what you want there Smile

I imported the functions you need...for the script to work without his class file...but feel free to choose how ever you want to do it..

<?php
define('IN_MYBB', NULL);
require_once '/your/forum/location/global.php';
$MyBBI = new MyBBIntegrator($mybb, $db, $cache, $plugins, $lang, $config); 

	function getLatestThreads($forum_id = 0, $fields = '*', $limit = 7, $exclude_invisible = true, $join_forums = true, $join_first_post = true)
	{
		if ($forum_id != 0)
		{
			if (is_array($forum_id))
			{
				foreach ($forum_id as $single_forum_id)
				{
					$forum_permissions = forum_permissions($single_forum_id);
					if ($forum_permissions['canview'] != 1 || $forum_permissions['canviewthreads'] != 1)
					{
						return false;
					}
				}
			}
			else
			{
				$forumpermissions = forum_permissions($forum_id);
				if ($forumpermissions['canview'] != 1 || $forumpermissions['canviewthreads'] != 1)
				{
					return false;
				}
			}
		}
		
		$threads = array();
		
		$fetch_invisible_threads = ($exclude_invisible == true) ? '1' : '0';
		$condition = 't.`visible` = '.$fetch_invisible_threads;
		
		if (is_array($forum_id) || is_object($forum_id))
		{
			$condition .= ' AND t.`fid` IN ('.implode(', ', $forum_id).')';
			
		}
		else
		{
			$condition .= ($forum_id == 0) ? '' : ' AND t.`fid` = '.$forum_id;
		}
		
		$forum_join = ($join_forums == true) ? 'INNER JOIN '.TABLE_PREFIX.'forums f ON f.`fid` = t.`fid`' : '';
		
		$first_post_join = ($join_first_post == true) ? 'INNER JOIN '.TABLE_PREFIX.'posts p ON p.`pid` = t.`firstpost`' : '';
		
		$query = $this->db->query('
			SELECT '.$fields.'
			FROM '.TABLE_PREFIX.'threads t
			'.$forum_join.'
			'.$first_post_join.'
			WHERE '.$condition.'
			ORDER BY t.`dateline` DESC
			LIMIT '.intval($limit).'
		');
		
		while ($thread = $this->db->fetch_array($query))
		{
			$threads[] = $thread;
		}
		
		return $threads;
	}
	
	
	
	function getPostsOfThread($thread_id, $fields = '*', $options = array())
	{
		$arr = array();
		
		$query_thread = $this->db->query('SELECT `fid` FROM '.TABLE_PREFIX.'threads WHERE `tid` = '.intval($thread_id).' LIMIT 1');
		$thread_forumid = $this->db->fetch_field($query_thread, 'fid');
		
		$forumpermissions = forum_permissions($thread_forumid);
		if ($forumpermissions['canview'] != 1 || $forumpermissions['canviewthreads'] != 1)
		{
			return false;
		}
		
		$query = $this->db->simple_select('posts', $fields, '`tid` = '.intval($thread_id), $options);
		
		while ($post = $this->db->fetch_array($query))
		{
			$arr[] = $post;
		}
		
		return $arr;
	}

	
	
	
$forums = array('2');
$fields = 't.`tid`, t.`fid`, t.`subject`, t.`dateline`';
$latest_threads = $MyBBI->getLatestThreads($forums, $fields, 5, true, true, false);
foreach ($latest_threads as $latest_thread)
{
	$tid = $latest_thread['tid'];
	
	echo $latest_thread['subject'].'<br />';

	$posts = $MyBBI->getPostsOfThread($tid, '*', array('order_by' => 'dateline', 'order_dir' => 'ASC', 'limit' => 1));
	foreach ($posts as $post)
	{
		echo $post['message'].'<br />';
		echo "<h1>BREAK</h1>";
	} 
} 



?>

This gets the 5 newest threads from the the board "2" and gets the FIRST post from it and puts it under the threads title.

If you want the timestamp you just need to get variables for it so something like

$latest_thread['timestamp'].'<br />'

I don't know what it is....If you want the newest post check the class file and modify it just a tab with what I have to do it...there is a separate function for that.

I hope this helps!