MyBB Community Forums

Full Version: Displaying latest 3 topics on top of every forum page
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

Just upgraded my forum from IPB 1.2 to MyBB (via the PHPBB converter then the PHPBB > MyBB) and I have a question.

Basically, I want to display the top 3 latest posts on the very top of every forum page, I know this will be in the header template, and I have tried adapting the latest posts on front page thing but this doesnt seem to work, and I also tried writing my own SQL query, but that wouldn't work.

Ill post it here but its probably terrible(I put this all in the header template):
PHP Code:
<?php
                            $query
=("SELECT parent.tid, parent.subject FROM mybb_posts AS parent, mybb_threads AS child WHERE parent.tid = child.tid ORDER BY child.pid DESC LIMIT 3");
$result mysql_query($query); 
$row_comments mysql_fetch_assoc($result);    ?>
                            <table width="100%" border="0" cellspacing="0" cellpadding="0">
 <?php do { ?>
��<tr>
����<td><a href="URL/forum/showthread.php?tid=<?php echo $row_comments[tid]; ?>"><?php echo $row_comments[subject]; ?></a></td>
��</tr>
�� <?php } while ($row_comments == mysql_fetch_assoc($result)); ?>
</table> 

I also converted the RSS feed into a page and tried to use php include, but that wont work either (on both MyBB and IPB i couldn't get includes to work).

Can anyone help? Thanks in advance!
You may want to refer to this thread: http://community.mybboard.net/showthread...6#pid12296
Maybe you can do something with this plugin: Advanced Stats on Index/Portal

It can show links to latest posts, latest topics, most rated member and a lot more on the index and/or portal page.
SmittyD Wrote:You may want to refer to this thread: http://community.mybboard.net/showthread...6#pid12296

Thank-you so much for that - I looked but I never found that one. It didn't suit what I wanted to do exactly - so I modified it slightly, and this is what I ended up with:-
Saved into MyBB directory I called it latesttopics.php
<?php
/*/ latest topic code
written by p0iz @ roflnub.com
for QuickGold
/*/

// Database Configuration
$username = "";
$password = "";
$database = "";

$tlimit = 5; // How many titles you want
// End of Configuration

// Connect

$connect = mysql_pconnect("localhost","$username","$password");
if(!$connect){
echo"couldnt connect..";
}

$omg = mysql_select_db($database);
if(!$omg){
echo"couldnt select database..";
}

// get latest topics..

$query = mysql_query("SELECT parent.tid, parent.subject FROM mybb_threads AS parent, mybb_posts AS child WHERE parent.tid = child.tid ORDER BY child.pid DESC LIMIT $tlimit"); 
while($fetch = mysql_fetch_array($query)){
echo 'document.write(\'<a href="forum url/showthread.php?tid='.$fetch['tid'].'">'.$fetch['subject'].'</a><br />\');' . "\n";
}
?>

And put this in the headers template
<script src="latesttopics.php"></script>

The reason that I couldn't directly use the script SmittyD suggested, was that it showed the latest topics, by the date they were posted. If someone posted in a very old topic, it would not appear on the most recent list. So this code allowed me to show the name of the topic that has the latest post in it.

Thanks also Cipher - but I really just needed the latest topics on every forum page - so I didn't use the Advanced Stats Mod.
I changed the MySQL query to the simpler:
SELECT tid, subject FROM mybb_threads WHERE fid != 13 AND fid != 12 AND fid != 14 AND visible = 1 ORDER BY lastpost DESC LIMIT $tlimit

The reasons for this were:-
  • Displays recent topics instead of the topic that goes with the latest post - no duplicate topics
  • Only displays visible topics
  • Can exclude some forum IDs (where it says fid != 13) This means it will show topics from forums where the forum is not equal to 13.

Hope this helps someone.
You can shorten "fid != 13 AND fid != 12 AND fid != 14" to "fid NOT IN ('12','13','14')".