Display first post from first 5 topics for a specific forum

All web design discussion, including Ultimate Quiz MOD support.

Moderator: CricketMX Forum Moderators

Post Reply
Dingo23
Greenhorn
Greenhorn
Posts: 4
Joined: Sat Apr 27, 2013 7:04 am

Hello,

I've tried to search this forum to find the solution, but I can't seem to do it. I'm using the external page script to display the first post, from the last 5 topics in a specific forum and it's working, sort of. Instead of the first post from the first 5 topics, it's displaying the last 5 posts, instead of topics.

I have two questions,

1. What do I need to modify to make it display the first post, from the 5 most recent topics in a specific forum.
2. when the post appears on the external page, I have the template formated to match my site, but each post is acompanied by an avatar, currently the template has a default avatar, I want to include the post to show what ever the posters forum avatar is instead, how can I do that? I understand it'll prob require adding a new variable to call, but I don't know how to insert it in.

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');

$search_limit = 5;

$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'     => $db->sql_in_set('t.forum_id', array(6)) . '
                        AND t.topic_status <> ' . ITEM_MOVED . '
                         AND t.topic_approved = 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", "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);

         $template->assign_block_vars('announcements', array(
         'TOPIC_TITLE'       => censor_text($topic_title),
         'POST_AUTHOR'       => $post_author,
         'POST_DATE'       => $post_date,
         'POST_LINK'       => $post_link,
         'POST_TEXT'         => censor_text($post_text),
         ));
      }

page_header('External page');

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

    page_footer();
?>
Dingo23
Greenhorn
Greenhorn
Posts: 4
Joined: Sat Apr 27, 2013 7:04 am

So, I've managed to get it to display only the first post from the first 5 topics of the specific forum, but I've run into a new problem, the poster Author, date and do not show up on the external page, and the link to the topic only links to the forum index page.

Here is the code:

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;
$size_gen_id = sizeof($gen_id);
}

$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 = 5;

$forum_id = array(6);
$forum_id_where = create_where_clauses($forum_id, 'forum');

$posts_ary = array(
'SELECT' => 'p.*, t.*',

'FROM' => array(
POSTS_TABLE => 'p',
),

'LEFT_JOIN' => array(
array(
'FROM' => array(TOPICS_TABLE => 't'),
'ON' => 't.topic_first_post_id = p.post_id'
)
),

'WHERE' => str_replace( array('WHERE ', 'forum_id'), array('', 't.forum_id'), $forum_id_where) . '
AND t.topic_status <> ' . ITEM_MOVED . '
AND t.topic_approved = 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'];
$topic_author = get_username_string('full', $posts_row['topic_poster'], $posts_row['topic_first_poster_name'], $posts_row['topic_first_poster_colour']);
$topic_date = $user->format_date($posts_row['topic_time']);
$topic_link = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "t=" . $posts_row['topic_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);

$template->assign_block_vars('announcements', array(
'TOPIC_TITLE' => censor_text($topic_title),
'TOPIC_AUTHOR' => $topic_author,
'TOPIC_DATE' => $topic_date,
'TOPIC_LINK' => $topic_link,
'POST_TEXT' => censor_text($post_text),
));
}

page_header('The title of your page goes here');

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

page_footer();
?>
And my template:

Code: Select all

<!-- BEGIN announcements -->
<div id="contentbox">
<div id="avatar" style="background-image: url(/images/Avatar.png);">
				</div>
				<p><span class="title">{announcements.TOPIC_TITLE}</span> | {announcements.POST_DATE} | {announcements.POST_AUTHOR}<br><img src="images/div.png"><br>{announcements.POST_TEXT}<br><br><a href="/forum/{announcements.POST_LINK}">Comment</a></p></span>
			</div>
<!-- END announcements -->
As in my original post, I'd still like the section that says <div id="avatar" style="background-image: url(/images/Avatar.png);"> to display the posters forum Avatar.

any help on these would be appreciated.
User avatar
battye
Site Admin
Site Admin
Posts: 14391
Joined: Sun Jan 11, 2004 8:26 am
Location: Australia
Contact:

Hi Dingo23, welcome to the forum.

Could you please post a screenshot of what the page looks like at the moment?

Thanks
CricketMX.com in 2022: Still the home of bat's, rat's and other farmyard animals!

"OK, life [as you chose to define it] repeats until there are no more lessons to be learned." - nrnoble (June 12, 2005)
"the new forum looks awesome, it's getting bigger & better" - p2p-sharing-rules (11 Jan, 2008)
"Looks like CMX is not only getting bigger...but, also getting better!!" - moongirl (14 Dec, 2007)
Dingo23
Greenhorn
Greenhorn
Posts: 4
Joined: Sat Apr 27, 2013 7:04 am

Thanks for the welcome, here is a screenshot of the website as it appears on my browser, Google Chrome.

http://space.drunkenpirateguild.com/images/website.png

the Issues I'm having is
A) the post should appear like Toborro Story Mode | Topic Post Date | Topic Poster name, instead it appears as Toborro Story Mode | |
B) the link to comment at the bottom of the post should link directly to the topic in question, but it instead goes to the forum index page.
C) the image of the Jawa is a default image that i have worked into the external template, i'd like it to display the users forum avatar.

the website itself is http://space.drunkenpirateguild.com/, I guess I should have provided that in the first post.
User avatar
battye
Site Admin
Site Admin
Posts: 14391
Joined: Sun Jan 11, 2004 8:26 am
Location: Australia
Contact:

Okay, we'll work through them individually:

A) Where you have this

Code: Select all

{announcements.TOPIC_TITLE}</span> | {announcements.POST_DATE} | {announcements.POST_AUTHOR}
Replace with:

Code: Select all

{announcements.TOPIC_TITLE}</span> | {announcements.TOPIC_DATE} | {announcements.TOPIC_AUTHOR}
See the templating variables at the bottom of your PHP code? That needs to align with what you have in the HTML template file :)

B) Similarly, this needs to be TOPIC_LINK in the template file.

C) Do you actually want to display the file images/Avatar.png as the avatar (it implies everyone has the same avatar?) or do you want the avatar of each individual user to be displayed?

Thanks
CricketMX.com in 2022: Still the home of bat's, rat's and other farmyard animals!

"OK, life [as you chose to define it] repeats until there are no more lessons to be learned." - nrnoble (June 12, 2005)
"the new forum looks awesome, it's getting bigger & better" - p2p-sharing-rules (11 Jan, 2008)
"Looks like CMX is not only getting bigger...but, also getting better!!" - moongirl (14 Dec, 2007)
Dingo23
Greenhorn
Greenhorn
Posts: 4
Joined: Sat Apr 27, 2013 7:04 am

Well now, that works and in hindsight was pretty damn obvious.

Thank you.

As for the avatar, it currently uses a default image, but I want the image to instead be the avatar of who ever posted the article in the forum and be their forum avatar.
Post Reply