MyBB Community Forums

Full Version: Display threads from a specific forum on my main websites index.php
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi there,

I've been hunting around for guides on how to get my "news" forum posts to show up on my main index.php page and I was wondering if anyone could help me out?

I'm currently using MyBB 1.8.12 and all of the guides I've been able to track down appear to be for much earlier versions. Any help would be much appreciated.
you can follow this method & improve depending on your requirement

you can replace WHERE 1=1 with WHERE fid = x (where x is fid number of required news forum)

if you want to select a number of forums then you can use WHERE fid IN (x,y,z) ..
(2017-05-30, 02:50 PM).m. Wrote: [ -> ]you can follow this method & improve depending on your requirement

you can replace WHERE 1=1 with WHERE fid = x (where x is fid number of required news forum)

if you want to select a number of forums then you can use WHERE fid IN (x,y,z) ..

This is excellent, the only issue is that this actually only shows the post thread and not the content of the post itself. Which is what I'm aiming for.
Try with this query:

$query = $db->query("
	SELECT t.tid, t.fid, t.uid, t.username, t.subject, t.replies, t.views, t.lastposter, p.message
	FROM ".TABLE_PREFIX."threads t
	INNER JOIN ".TABLE_PREFIX."posts p ON (p.tid=t.tid)
	WHERE t.fid='2' AND t.visible='1' AND t.closed NOT LIKE 'moved|%' AND t.firstpost=p.pid
	ORDER BY t.lastpost DESC
	LIMIT 6" // Change the last digit to how many recent post you want to be shown
);

If you want the order by the date of publication of the thread (so not the last post) use in the ORDER BY keyword:

ORDER BY t.dateline DESC

To show the message, use inside the while loop:

$fetch['message']

If you want to limit the characters of the message insert inside the while loop:

$message = htmlspecialchars_uni($fetch['message']);
if(my_strlen($message) > 140) {
	$message = my_substr($message, 0, 140) . "...";
}

Replacing 140 with the maximum characters that you want.
(2017-05-31, 12:49 AM)Mipher Wrote: [ -> ]Try with this query:

$query = $db->query("
	SELECT t.tid, t.fid, t.uid, t.username, t.subject, t.replies, t.views, t.lastposter, p.message
	FROM ".TABLE_PREFIX."threads t
	INNER JOIN ".TABLE_PREFIX."posts p ON (p.tid=t.tid)
	WHERE t.fid='2' AND t.visible='1' AND t.closed NOT LIKE 'moved|%' AND t.firstpost=p.pid
	ORDER BY t.lastpost DESC
	LIMIT 6" // Change the last digit to how many recent post you want to be shown
);

If you want the order by the date of publication of the thread (so not the last post) use in the ORDER BY keyword:

ORDER BY t.dateline DESC

To show the message, use inside the while loop:

$fetch['message']

If you want to limit the characters of the message insert inside the while loop:

$message = htmlspecialchars_uni($fetch['message']);
if(my_strlen($message) > 140) {
	$message = my_substr($message, 0, 140) . "...";
}

Replacing 140 with the maximum characters that you want.

Absolutely perfect. This gives me the perfect baseline to get started on what I want to accomplish. I'll be sure to post my finished product (with a link to my site) in case anyone else wants to do something similar to me.

Thanks for the awesome support!
Alright, for those of you that were wondering I was able to get this working exactly how I wanted it. It displays the subject of the post as well as the post content.

Here is the code... Thanks to everyone that helped me!

<?php
chdir("./forum/"); // path to MyBB
define("IN_MYBB", 1);
require("./global.php");
global $mybb, $db, $cache, $lang, $plugins;  // Trim this list of global vars, if you want to.
?>

<?php
$query = $db->query("
    SELECT t.tid, t.fid, t.uid, t.username, t.subject, t.replies, t.views, t.lastposter, p.message
    FROM ".mybb_."threads t
    INNER JOIN ".mybb_."posts p ON (p.tid=t.tid)
    WHERE t.fid='3' AND t.visible='1'
    ORDER BY t.dateline DESC
    LIMIT 6
	"// Change the last digit to how many recent post you want to be shown
	
); 
$list = '';
    while($fetch = $db->fetch_array($query))
    {
        $list .= "<strong><a href=\"forum/showthread.php?tid={$fetch['tid']}\">".htmlspecialchars_uni($fetch['subject'])."</a></strong><br />";
		$list .= "".htmlspecialchars_uni($fetch['message'])."<br>";
        $poster = "<a href=\"forum/member.php?action=profile&uid=".$fetch['uid']."\">{$fetch['username']}</a>";
        $list .= "Posted by: {$poster}<br /><br />";
    
    }
    //output
    echo $list;
?>
(2017-06-02, 09:41 PM)DemonReborn Wrote: [ -> ]Alright, for those of you that were wondering I was able to get this working exactly how I wanted it. It displays the subject of the post as well as the post content.

Here is the code... Thanks to everyone that helped me!

<?php
chdir("./forum/"); // path to MyBB
define("IN_MYBB", 1);
require("./global.php");
global $mybb, $db, $cache, $lang, $plugins;  // Trim this list of global vars, if you want to.
?>

<?php
$query = $db->query("
    SELECT t.tid, t.fid, t.uid, t.username, t.subject, t.replies, t.views, t.lastposter, p.message
    FROM ".mybb_."threads t
    INNER JOIN ".mybb_."posts p ON (p.tid=t.tid)
    WHERE t.fid='3' AND t.visible='1'
    ORDER BY t.dateline DESC
    LIMIT 6
 "// Change the last digit to how many recent post you want to be shown
 
); 
$list = '';
    while($fetch = $db->fetch_array($query))
    {
        $list .= "<strong><a href=\"forum/showthread.php?tid={$fetch['tid']}\">".htmlspecialchars_uni($fetch['subject'])."</a></strong><br />";
 $list .= "".htmlspecialchars_uni($fetch['message'])."<br>";
        $poster = "<a href=\"forum/member.php?action=profile&uid=".$fetch['uid']."\">{$fetch['username']}</a>";
        $list .= "Posted by: {$poster}<br /><br />";
    
    }
    //output
    echo $list;
?>

Thanks, but I get a 500 error (PHP 7.3).

There is an error somewhere here:

$query = $db->query("
    SELECT t.tid, t.fid, t.uid, t.username, t.subject, t.replies, t.views, t.lastposter, p.message

But I don't know what.