MyBB Community Forums

Full Version: Creating custom page to display new posts from specific user
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Right now I've been testing out custom static pages that display content from a specific thread and posts. 

I want to take it up another level and have a page that automatically display the content of the first post of a thread from a specific user when that user makes a new thread.  I believe this would essentially involve a set of "if else" statements in PHP checking into the database for certain parameters being met.  

This is where I'm currently stumped, as I'm not sure if there's a variable or flag within the DB for "new thread" or a way to calculate "all threads by user from specific forum" and display the first post of those threads.  Also looking for the variable that captures the posts count from a specific thread.

I hope this make sense.
<?php
define("IN_MYBB", 1);
require_once "global.php";
require_once "inc/class_parser.php";
$parser = new PostParser();
$parser_options = array(
"allow_html" => 0
				"allow_mycode" =>1
				"allow_smilies" => 1,
				"allow_imgcode" => 1,
				"allow_videocode" => 1,
				"filter_badwords" => 1
);
$userid = 1; // replace this with the actual user's id
$forumid = 1; // Replace this with the forum you want it to be from
$limit = 10; // Replace this with how many you want to show.
$query = $db->query("SELECT t.*, p.* FROM " . TABLE_PREFIX . "threads t
LEFT JOIN " . TABLE_PREFIX . "posts p ON(t.firstpost=p.pid)
WHERE t.uid=$userid AND t.fid=$forumid
ORDER BY t.dateline DESC
LIMIT $limit");
while($thread = $db->fetch_array($query))
{
$thread['message'] = $parser->parse_message($thread['message'], $parser_options);
// either do an echo or use a template here.
}
// use output_page() if you are using a template
?>
Thank you. This is a good starting point for me to look at and start tweaking.

A question I have though...

In the 'Order by' is it possible to set this based on a dateline that's less than 20 days from the current date?


I'm thinking that besides having a finite limit of 10 (as in the sample), I would only list out content from a particular timeframe.
You would change the where clause, not the order by clause to do that. It is stored using timestamps of the current time so it is in seconds.
$cutoff = TIME_NOW - (86400 * 20);
$query = $db->query("SELECT t.*, p.* FROM " . TABLE_PREFIX . "threads t
LEFT JOIN " . TABLE_PREFIX . "posts p ON(t.firstpost=p.pid)
WHERE t.uid=$userid AND t.fid=$forumid AND t.dateline >= $cutoff
ORDER BY t.dateline DESC
LIMIT $limit");
Thank you again.  I didn't know the timestamps were reflected in seconds.  I was able to use this to create a readable format using the following code:

$thread['postdate'] = my_date($mybb->settings['dateformat'], $thread['dateline']);


Another question I have is if it's possible to truncate the message to a certain number of characters.   I was doing some searching and I believe I would use the 'LEFT' SQL statement, but I'm not too sure on how to execute it.
You can use the my_substr function that is built into MyBB.
Awesome!  Thank you again.

Just in case anyone is curious with what I did, here the code I used to get the basic display of content:

	<?php 
		while($thread = $db->fetch_array($query))
		{
		$thread['message'] = $parser->parse_message($thread['message'], $parser_options);
		$preview = my_substr($thread['message'], 0, 500);
		$thread['postdate'] = my_date('l F d, Y', $thread['dateline']);
		echo "<b>{$thread['subject']}</b><br>
		     {$thread['postdate']} by <a href='{$mybb->settings['bburl']}/member.php?action=profile&uid={$thread['uid']}'>{$thread['username']}</a>
		     <p>{$preview}</p>
		     <p><a href='{$mybb->settings['bburl']}/showthread.php?tid={$thread['tid']}'>Read more...</a></p><br><br>";
		}
	?>