Display latest/recent topics from specified forums

Say hello, ask a question or make a comment.
User avatar
clight77
Moderator
Posts: 24
Joined: Wed Apr 04, 2018 7:39 pm

Display latest/recent topics from specified forums

Post by clight77 » Sun Jun 03, 2018 5:39 pm

I did up a newer version that allows you to select which forums to choose from :)


Display latest/recent topics from specified forums

Code: Select all

<?php
/*
* sample.php 
* Description: example file for displaying latest posts and topics
* by battye (for phpBB.com MOD Team)
* September 29, 2009
*/

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('viewforum');



/* create_where_clauses( int[] gen_id, String type )
* This function outputs an SQL WHERE statement for use when grabbing 
* posts and topics */

function create_where_clauses($gen_id, $type)
{
    global $db, $auth;

    $size_gen_id = sizeof($gen_id);

    switch($type)
    {
        case 'forum':
        $type = 'forum_id';
        break;
        case 'topic':
        $type = 'topic_id';
        break;
        default:
        trigger_error('No type defined');
    }

    // Set $out_where to nothing, this will be used of the gen_id
    // size is empty, in other words "grab from anywhere" with
    // no restrictions
    $out_where = '';

    if( $size_gen_id > 0 )
    {
    // Get a list of all forums the user has permissions to read
        $auth_f_read = array_keys($auth->acl_getf('f_read', true));

        if( $type == 'topic_id' )
        {
            $sql     = 'SELECT topic_id FROM ' . TOPICS_TABLE . '
            WHERE ' .  $db->sql_in_set('topic_id', $gen_id) . '
            AND ' .  $db->sql_in_set('forum_id', $auth_f_read);

            $result     = $db->sql_query($sql);

            while( $row = $db->sql_fetchrow($result) )
            {
                        // Create an array with all acceptable topic ids
                $topic_id_list[] = $row['topic_id'];
            }

            unset($gen_id);

            $gen_id = $topic_id_list;
            
        }

        $j = 0;    

        for( $i = 0; $i < $size_gen_id; $i++ )
        {
            $id_check = (int) $gen_id[$i];

            // If the type is topic, all checks have been made and the query can start to be built
            if( $type == 'topic_id' )
            {
                $out_where .= ($j == 0) ? 'WHERE ' . $type . ' = ' . $id_check . ' ' : 'OR ' . $type . ' = ' . $id_check . ' ';
            }

            // If the type is forum, do the check to make sure the user has read permissions
            else if( $type == 'forum_id' && $auth->acl_get('f_read', $id_check) )
            {
                $out_where .= ($j == 0) ? 'WHERE ' . $type . ' = ' . $id_check . ' ' : 'OR ' . $type . ' = ' . $id_check . ' ';
            }    

            $j++;
        }
    }

    if( $out_where == '' && $size_gen_id > 0 )
    {
        trigger_error('A list of topics/forums has not been created');
    }

    return $out_where;
}

// -------------------------------------------------------------------

$search_limit = 75; 
$forum_id = array(272, 273, 274, 275);
$forum_id_where = create_where_clauses($forum_id, 'forum');
$topic_id = array(20, 50);
$topic_id_where = create_where_clauses($topic_id, 'topic');

$posts_ary = array(
    'SELECT'    => 'p.*, t.*, u.username, u.user_colour',
    
    'FROM'      => array(
        POSTS_TABLE     => 'p',
    ),
    
    'LEFT_JOIN' => array(
        array(
            'FROM'  => array(USERS_TABLE => 'u'),
            'ON'    => 'u.user_id = p.poster_id'
        ),
        array(
            'FROM'  => array(TOPICS_TABLE => 't'),
            'ON'    => 'p.topic_id = t.topic_id'
        ),
    ),
    
    'WHERE' => str_replace( array('WHERE ', 'forum_id'), array('', 't.forum_id'), $forum_id_where ) .
		'AND t.topic_status <> ' . ITEM_MOVED . ' AND t.topic_visibility = 1',
	'ORDER_BY'	=> 'p.post_id DESC',
);
$d=1;    
$posts = $db->sql_build_query('SELECT', $posts_ary);
$posts_result = $db->sql_query_limit($posts, $search_limit);

while( $posts_row = $db->sql_fetchrow($posts_result) ):

   $topic_title       = $posts_row['topic_title'];
   $post_author       = get_username_string('full', $posts_row['poster_id'], $posts_row['username'], $posts_row['user_colour']);
   $post_date          = $user->format_date($posts_row['post_time']);
   $post_link       = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $posts_row['forum_id'] . '&amp;t=' . $posts_row['topic_id'] . '&amp;p=' . $posts_row['post_id']) . '#p' . $posts_row['post_id'];

   $post_text = nl2br($posts_row['post_text']);

   $bbcode = new bbcode(base64_encode($bbcode_bitfield));         
   $bbcode->bbcode_second_pass($post_text, $posts_row['bbcode_uid'], $posts_row['bbcode_bitfield']);

   $post_text = smiley_text($post_text);

echo $d++;
echo "<a href='". $post_link ."'>&nbsp; &bull; &nbsp;";
echo $topic_title;
echo "</a>&nbsp;&nbsp;&nbsp;by&nbsp;";
echo $post_author;
echo "&nbsp;&nbsp;&nbsp;&raquo;&nbsp;";
echo $post_date;
echo "<br><hr>";

endwhile;

page_footer();
	
?>
--------------------------------------------------------------------------------------------------------------



And of course you have to use the pages ext with iframe

User avatar
clight77
Moderator
Posts: 24
Joined: Wed Apr 04, 2018 7:39 pm

Re: Display latest/recent topics from specified forums

Post by clight77 » Sun Jun 03, 2018 8:12 pm

Something wrong, it is not picking from selected forum ID, it is just grabbing latest post :(

User avatar
Sniper_E
Site Founder
Posts: 140
Joined: Sat Mar 24, 2018 10:52 pm
Location: Shreveport Louisiana

Re: Display latest/recent topics from specified forums

Post by Sniper_E » Sun Jun 03, 2018 8:21 pm

Hmmm, I saw you had something going there. I was going to check it out later.
Image
No is NEVER an Option and NEVER is the only Option when it comes to Giving Up!™

User avatar
clight77
Moderator
Posts: 24
Joined: Wed Apr 04, 2018 7:39 pm

Re: Display latest/recent topics from specified forums

Post by clight77 » Sun Jun 03, 2018 8:40 pm

I was hoping it would grab from certain forum ID's , it should, but it seems to be grabbing last posts from everywhere,.


Me stupid.. lol

User avatar
Sniper_E
Site Founder
Posts: 140
Joined: Sat Mar 24, 2018 10:52 pm
Location: Shreveport Louisiana

Re: Display latest/recent topics from specified forums

Post by Sniper_E » Mon Jun 04, 2018 2:32 am

You can look in the acp to see how I setup each page with the iframes pulling in different files.

I placed all of the files for my four forums in the root/recent-topics/ directory.
These are you old templates not the new one you just created.

And these are the files. Each with a different forum id# in them.
recent-topics.zip
(4.48 KiB)
recent-topics.zip
(4.48 KiB) Downloaded 144 times





You can see each link in the header navbar Quick links drop down.
Image
No is NEVER an Option and NEVER is the only Option when it comes to Giving Up!™

User avatar
Martin
Site Admin
Posts: 64
Joined: Sun Mar 25, 2018 6:29 am
Location: Heaven

Re: Display latest/recent topics from specified forums

Post by Martin » Mon Jun 04, 2018 2:35 am

May have to steal this :roll:

User avatar
clight77
Moderator
Posts: 24
Joined: Wed Apr 04, 2018 7:39 pm

Re: Display latest/recent topics from specified forums

Post by clight77 » Mon Jun 04, 2018 8:50 am

You did good Ed :)

Perfect

User avatar
clight77
Moderator
Posts: 24
Joined: Wed Apr 04, 2018 7:39 pm

Re: Display latest/recent topics from specified forums

Post by clight77 » Mon Jun 04, 2018 10:21 am

You did good Ed :)

Needs an array so you can call from multiple forum ID's.

$forum_id = array(2, 10);


Right now you can only get from the 1 ID

User avatar
clight77
Moderator
Posts: 24
Joined: Wed Apr 04, 2018 7:39 pm

Re: Display latest/recent topics from specified forums

Post by clight77 » Mon Jun 04, 2018 12:37 pm

I think I got it :)
https://www.nzbforyou.com/qmax-all

Code: Select all

<?php
/*
* home.php 
* Description: example file for displaying latest posts and topics
* by battye (for phpBB.com MOD Team)
* September 29, 2009
*/

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('viewforum');



/* create_where_clauses( int[] gen_id, String type )
* This function outputs an SQL WHERE statement for use when grabbing 
* posts and topics */

function create_where_clauses($gen_id, $type)
{
    global $db, $auth;

    $size_gen_id = sizeof($gen_id);

    switch($type)
    {
        case 'forum':
        $type = 'forum_id';
        break;
        case 'topic':
        $type = 'topic_id';
        break;
        default:
        trigger_error('No type defined');
    }

    // Set $out_where to nothing, this will be used of the gen_id
    // size is empty, in other words "grab from anywhere" with
    // no restrictions
    $out_where = '';

    if( $size_gen_id > 0 )
    {
    // Get a list of all forums the user has permissions to read
        $auth_f_read = array_keys($auth->acl_getf('f_read', true));

        if( $type == 'topic_id' )
        {
            $sql     = 'SELECT topic_id FROM ' . TOPICS_TABLE . '
            WHERE ' .  $db->sql_in_set('topic_id', $gen_id) . '
            AND ' .  $db->sql_in_set('forum_id', $auth_f_read);

            $result     = $db->sql_query($sql);

            while( $row = $db->sql_fetchrow($result) )
            {
                        // Create an array with all acceptable topic ids
                $topic_id_list[] = $row['topic_id'];
            }

            unset($gen_id);

            $gen_id = $topic_id_list;
            
        }

        $j = 0;    

        for( $i = 0; $i < $size_gen_id; $i++ )
        {
            $id_check = (int) $gen_id[$i];

            // If the type is topic, all checks have been made and the query can start to be built
            if( $type == 'topic_id' )
            {
                $out_where .= ($j == 0) ? 'WHERE ' . $type . ' = ' . $id_check . ' ' : 'OR ' . $type . ' = ' . $id_check . ' ';
            }

            // If the type is forum, do the check to make sure the user has read permissions
            else if( $type == 'forum_id' && $auth->acl_get('f_read', $id_check) )
            {
                $out_where .= ($j == 0) ? 'WHERE ' . $type . ' = ' . $id_check . ' ' : 'OR ' . $type . ' = ' . $id_check . ' ';
            }    

            $j++;
        }
    }

    if( $out_where == '' && $size_gen_id > 0 )
    {
        trigger_error('A list of topics/forums has not been created');
    }

    return $out_where;
}



// -------------------------------------------------------------------

$search_limit = 70;
$forum_id = array(289, 288);
$forum_id_where = create_where_clauses($forum_id, 'forum');
$topic_id = array(20, 50);
$topic_id_where = create_where_clauses($topic_id, 'topic');



$posts_ary = array(
    'SELECT'    => 'p.*, t.*, u.username, u.user_colour',
    
    'FROM'      => array(
        POSTS_TABLE     => 'p',
    ),
    
    'LEFT_JOIN' => array(
        array(
            'FROM'  => array(USERS_TABLE => 'u'),
            'ON'    => 'u.user_id = p.poster_id'
        ),
        array(
            'FROM'  => array(TOPICS_TABLE => 't'),
            'ON'    => 'p.topic_id = t.topic_id'
        ),
    ),
    
    'WHERE' => str_replace( array('WHERE ', 'forum_id'), array('', 't.forum_id'), $forum_id_where ) .
		'AND t.topic_status <> ' . ITEM_MOVED . ' AND t.topic_visibility = 1',
	'ORDER_BY'	=> 'p.post_id DESC',
);
    
$posts = $db->sql_build_query('SELECT', $posts_ary);
$posts_result = $db->sql_query_limit($posts, $search_limit);

while( $posts_row = $db->sql_fetchrow($posts_result) ):

   $topic_title       = $posts_row['topic_title'];
   $post_author       = get_username_string('full', $posts_row['poster_id'], $posts_row['username'], $posts_row['user_colour']);
   $post_date          = $user->format_date($posts_row['post_time']);
   $post_link       = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $posts_row['forum_id'] . '&amp;t=' . $posts_row['topic_id'] . '&amp;p=' . $posts_row['post_id']) . '#p' . $posts_row['post_id'];

   $post_text = nl2br($posts_row['post_text']);

   $bbcode = new bbcode(base64_encode($bbcode_bitfield));         
   $bbcode->bbcode_second_pass($post_text, $posts_row['bbcode_uid'], $posts_row['bbcode_bitfield']);

   $post_text = smiley_text($post_text);


echo "<tr>";
echo "<th scope='row'>";
echo $d++;
echo "</th>";
echo "<a href='". $post_link ."'> - ";
echo $topic_title;
echo "</a><br>";
echo "</td>";
echo $post_author;
echo "</td> - ";
echo $post_date;
echo "</td><div style=\"height:8px;\"><hr>";
echo "</tr>";

endwhile;


    $template->set_filenames(array(
        'body' => 'recent.html',
  ));

page_footer();
	
?>

User avatar
clight77
Moderator
Posts: 24
Joined: Wed Apr 04, 2018 7:39 pm

Re: Display latest/recent topics from specified forums

Post by clight77 » Mon Jun 04, 2018 2:01 pm

Here is where I found the cure for a lot of it.

https://gist.github.com/archipoeta/599c1c6caa544d5fcd9f

Post Reply