(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
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!