MyBB Community Forums

Full Version: Displaying News on your homepage
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
How can i make this display the latest posts in the selected forums and instead of thread creator name, it would have the last postor name.
(2009-12-06, 10:19 AM)karoshio Wrote: [ -> ]How can i make this display the latest posts in the selected forums and instead of thread creator name, it would have the last postor name.

This query right here for example, does what you first describe. It selects the 5 latest posts from the selected forum and orders it by the thread ID in a manner which will display the latest posts first. In this case, I just used 8 for the forum ID. You of course would have to enter the correct ID of the forum you wish to pull the threads from.

$fid = "8"; // The forum ID to pull the annoucements from
$limit = "5"; // The amount of annoucements to show
		
$query = $db->simple_select('threads', '*', "fid='{$fid}' ORDER BY tid DESC LIMIT {$limit}");

As for getting the name of the last poster in each particular thread, within your while loop you would use something like:

echo 'Last post by <a href="member.php?action=profile&uid='.$row['lastposteruid'].'">'.$row['lastposter'].'</a>';

Altogether it should look something like this:

$fid = "8"; // The forum ID to pull the annoucements from
$limit = "5"; // The amount of annoucements to show
$forumpath = "forums/"; // Path to your forums root directory (with trailing slash)
		
$query = $db->simple_select('threads', '*', "fid='{$fid}' ORDER BY tid DESC LIMIT {$limit}");

while($row = $db->fetch_array($query))
{
	$query2 = $db->simple_select('posts', '*', "pid='{$row['firstpost']}'");
	$row2 = $db->fetch_array($query2);

	echo '<a href="'.$forumpath.'showthread.php?tid='.$row['tid'].'">'.$row['subject'].'</a><br />';
	echo 'Last post by <a href="member.php?action=profile&uid='.$row['lastposteruid'].'">'.$row['lastposter'].'</a>';
	echo $row2['message'];
}
Parse error: syntax error, unexpected $end in /home125b/sub008/sc70042-WTCK/www/test.php on line 270

Got that error.
Scroll down to line 270 of test.php at locate the source of the error - a syntax error. Meaning you haven't used proper php syntax.

Remember to use proper quotation marks, symbols, etc.
hello everyones i know this thread are so old but i would known in this part of code.

            $options = array(
                            'allow_html' => '1',
                            'filter_badwords' => '1',
                            'allow_mycode' => '1',
                            'allow_smilies' => '1',
                            'nl2br' => '1',
                            'me_username' => '1'
                            );

How can i allow video ?

thanks in advance
I feel dumb.
I don't understand what you mean by this:
Quote:- The 'forum/' in chdir('forum/') to your forum path relative to the page this is being called from.
- The '../' in chdir('../') to the path back from your forum to the page this is being called from.

So my website is
http://projectrtest.co.cc/site/index.php
and my forum is located here:
http://www.projectrtest.co.cc/forum/index.php

How to I edit
the chdir('../');
and the
The 'forum/' in chdir('forum/')

in order for it to work?
"forum path relative to the page this is being called from" ../forum/
"path back from your forum to the page this is being called from" ../site/
This is already an incredibly old post but for anybody who googled this like me to get the answer to this question make sure you change the code slightly. Since after a lot of trying this for me was the only thing that worked:

This code should be placed at the top of your page (before any output):
<?php

    $fid = 4;
    $limit = 5;
    $forumpath = 'forum/';

    chdir('forum');
    define("IN_MYBB", 1);
    require('./global.php');
    require_once MYBB_ROOT."inc/class_parser.php";
    require_once MYBB_ROOT."inc/functions_post.php";
    require_once MYBB_ROOT."inc/functions_user.php";
    $parser = new postParser;
?>

This code should then be placed where you would like the news to be shown:
<?php  
    $query = $db->simple_select('threads', '*', "fid='{$fid}' ORDER BY tid DESC LIMIT {$limit}");
    if($db->num_rows($query) > 0)
    {
        while($row = $db->fetch_array($query))
        {
            $query2 = $db->simple_select('posts', '*', "pid='{$row['firstpost']}'");
            $row2 = $db->fetch_array($query2);
            
            $date = my_date($mybb->settings['dateformat'], $row2['dateline'], "", 1);
            $time = my_date($mybb->settings['timeformat'], $row2['dateline'], "", 1);

            $options = array(
                        "allow_html" => 1,
			"allow_mycode" => 1,
			"allow_smilies" => 1,
			"allow_imgcode" => 1,
			"allow_videocode" => 1,
			"filter_badwords" => 1
                            );
            $message = $parser->parse_message($row2['message'], $options);
            
            echo("<a href=\"{$forumpath}showthread.php?tid={$row['tid']}\">{$row['subject']}</a> - 
                Posted: {$date} {$time} by <a href=\"{$forumpath}member.php?action=profile&uid={$row2['uid']}\">{$row2['username']}</a><br />");
            echo("{$message}<br /><br />");
            echo("Replies (<a href=\"{$forumpath}showthread.php?tid={$row['tid']}\">{$row['replies']}</a>)<br />");
        }
    }
    else
    {
        echo 'Nothing to display.';
    }

?>
Great stuff, really helped me outing understanding more php took me almost all day to get it haha!
Hmm MrDoom, this is a useful one, but will it show (number of) comments as well ?.
Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33