MyBB Community Forums

Full Version: [MyBBI] Get Latest Active Threads - All Forums (SOLVED)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've accomplished my goal my modifying the MyBBI function 'getLatestThreads()'. Here's my modified function.
function getLatestThreads($forum_id = 0, $fields = '*', $limit = 7, $exclude_invisible = true, $join_forums = true, $join_first_post = true, $orderby = 'lastpost')
	{
		if ($forum_id != 0)
		{
			// If we have multiple values, we have to check permission for each forum!
			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)
					{
						// error_no_permission();
						return false;
					}
				}
			}
			else
			{
				// Do we have permission?
				$forumpermissions = forum_permissions($forum_id);
				if ($forumpermissions['canview'] != 1 || $forumpermissions['canviewthreads'] != 1)
				{
					// error_no_permission();
					return false;
				}
			}
		}
		
		// This is what we will be returning
		$threads = array();
		
		// Do we want to get invisible threads as well?
		$fetch_invisible_threads = ($exclude_invisible == true) ? '1' : '0';
		$condition = 't.`visible` = '.$fetch_invisible_threads;
		
		// Are we fetching threads from multiple forums?
		if (is_array($forum_id) || is_object($forum_id))
		{
			$condition .= ' AND t.`fid` IN ('.implode(', ', $forum_id).')';
			
		}
		// Or are we just fetching threads from one forum?
		else
		{
			$condition .= ($forum_id == 0) ? '' : ' AND t.`fid` = '.$forum_id;
		}
		
		// Do we want to get information of the forum where the thread is located in?
		$forum_join = ($join_forums == true) ? 'INNER JOIN '.TABLE_PREFIX.'forums f ON f.`fid` = t.`fid`' : '';
		
		// Do we want to get the first post from the thread?
		$first_post_join = ($join_first_post == true) ? 'INNER JOIN '.TABLE_PREFIX.'posts p ON p.`pid` = t.`firstpost`' : '';
		
		// Run the Query
		$query = $this->db->query('
			SELECT '.$fields.'
			FROM '.TABLE_PREFIX.'threads t
			'.$forum_join.'
			'.$first_post_join.'
			WHERE '.$condition.'
			ORDER BY t.`'.$orderby.'` DESC
			LIMIT '.intval($limit).'
		');
		
		// Iterate through the results and assign it to our returning array
		while ($thread = $this->db->fetch_array($query))
		{
			$threads[] = $thread;
		}
		
		return $threads;
	}

This is a very simple change, all it does it allow the user to specify what order they'd like to list the latest posts by. The default is 'dateline' which is the original thread start date. I prefer to 'lastpost' which is the Unix Timestamp of the last post.

Here's an example call to my modified function.

					$forums = array('2','3');
					$fields = 't.`tid`, t.`fid`, t.`subject`, t.`uid`, t.`username`, t.`dateline`, t.`lastposter`, t.`lastposteruid`, t.`replies`, t.`lastpost`';
					$latest_threads = $MyBBI->getLatestThreads($forums, $fields, 6, true, false, false, 'lastpost');

Notice all you need to do is add whatever row you'd like to sort by in single quotes at the end. Easy day. It seems to work just fine, if you run into problems, just post them, or contact PHPDave (the author of MBBI). Also there's zero user input validation, easy enough to add, just check the rows against whatever the user input is. I'll fancy it up later, I'm kind of in a hurry.

Here's a list of all the rows in the table if you're too fat to check them yourself:

* tid
* fid
* subject
* prefix
* icon
* poll
* uid
* username
* dateline
* firstpost
* lastpost
* lastposter
* lastposteruid
* views
* replies
* closed
* sticknumratings
* totalratings
* notes
* visible
* unapprovedposts
* attachmentcount
* deletetime

Enjoy.

Note: The reason this wasn't posted over at phpdave's site (the official MBBI website) is because it requires administrator approval to join, and he doesn't seem to be too interested in approving anyone at the moment.