Posts on external pages

Discussion forum for MOD Writers regarding MOD Development.
User avatar
SpongeWeb
Registered User
Posts: 766
Joined: Thu Jan 27, 2005 2:57 pm
Location: End of the Universe
Contact:

Posts on external pages

Post by SpongeWeb »

As per the phpbb article here:
http://blog.phpbb.com/2009/11/09/how-to ... nal-pages/

I thought this was brilliant and had it working to display the first post of the topic I selected, now I have made it slightly different and want the first post from 5 selected topics of my choosing, I had this:

Code: Select all

$search_limit = 5;
$forum_id = array(2,3);
$forum_id_where = create_where_clauses($forum_id, 'forum');
$topic_id = array(10258,10554,10322, 10922);
$topic_id_where = create_where_clauses($topic_id, 'topic');
now this totally ignores the topid_id array and just posts the last 5 topics from forum 2 and 3 ?? why is topic_id there if it doesnt allow it? I then removed the forum_id lines, so it would just show topic_id since this is unique to the database, it threw an error so I put it back,

Im following the code , line for line from the article... what am i doing wrong? thanks...
A friend is someone who knows the song in your heart, and will sing, when you forget the words..
User avatar
tbackoff
Former Team Member
Posts: 7064
Joined: Thu Jun 04, 2009 1:41 am
Location: cheerleading practice
Name: Tabitha Backoff

Re: Posts on external pages

Post by tbackoff »

Are you using the sample code from Example 3?

Code: Select all

$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 ', 'topic_id'), array('', 't.topic_id'), $topic_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'];
$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),
));
}
Flying is the second best thrill to cheerleaders; being caught is the first.
User avatar
SpongeWeb
Registered User
Posts: 766
Joined: Thu Jan 27, 2005 2:57 pm
Location: End of the Universe
Contact:

Re: Posts on external pages

Post by SpongeWeb »

yep!
A friend is someone who knows the song in your heart, and will sing, when you forget the words..
User avatar
tbackoff
Former Team Member
Posts: 7064
Joined: Thu Jun 04, 2009 1:41 am
Location: cheerleading practice
Name: Tabitha Backoff

Re: Posts on external pages

Post by tbackoff »

Can you post the whole code you are using? I would have to see what exactly you are using to see where it may be failing. ;)
Flying is the second best thrill to cheerleaders; being caught is the first.
User avatar
SpongeWeb
Registered User
Posts: 766
Joined: Thu Jan 27, 2005 2:57 pm
Location: End of the Universe
Contact:

Re: Posts on external pages

Post by SpongeWeb »

Code: Select all

<?php
    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 = 2;

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

    $topic_id = array(1, 3);
    $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 ', 'topic_id'), array('', 't.topic_id'), $topic_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'];
    $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),
    ));
    }
?>
this code works, but not how its supposed to, I want to select the topic_ids and grab the posts from them,
A friend is someone who knows the song in your heart, and will sing, when you forget the words..
User avatar
tbackoff
Former Team Member
Posts: 7064
Joined: Thu Jun 04, 2009 1:41 am
Location: cheerleading practice
Name: Tabitha Backoff

Re: Posts on external pages

Post by tbackoff »

Well first off, you are missing all this at the begining of your file:

Code: Select all

/**
* @ignore
*/
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();
I'm not sure about the rest of it. I have the exact same code, but without topic_ids and it works fine. :?
Flying is the second best thrill to cheerleaders; being caught is the first.
User avatar
SpongeWeb
Registered User
Posts: 766
Joined: Thu Jan 27, 2005 2:57 pm
Location: End of the Universe
Contact:

Re: Posts on external pages

Post by SpongeWeb »

I dont need any of that as I include this file from index.php so the headers are allready there, and its fine, but I just want to pull the topics I specify, does yours just show the post from the topics you specify like this:

Code: Select all

    $topic_id = array(1, 3);
    $topic_id_where = create_where_clauses($topic_id, 'topic');
why does it need forum id, if you are specifying a unique topic id?
A friend is someone who knows the song in your heart, and will sing, when you forget the words..
User avatar
tbackoff
Former Team Member
Posts: 7064
Joined: Thu Jun 04, 2009 1:41 am
Location: cheerleading practice
Name: Tabitha Backoff

Re: Posts on external pages

Post by tbackoff »

Mine pulls the first post of every topic from a specified forum (the same way phpBB.com shows that last 5 topic titles on the front page).
Flying is the second best thrill to cheerleaders; being caught is the first.
User avatar
SpongeWeb
Registered User
Posts: 766
Joined: Thu Jan 27, 2005 2:57 pm
Location: End of the Universe
Contact:

Re: Posts on external pages

Post by SpongeWeb »

Code: Select all

 $search_limit = 3;

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

 $topic_id = array(1, 3, 4);
 $topic_id_where = create_where_clauses($topic_id, 'topic');
this ^ in my eyes should pull the first post from topic 1, first post from topic 3 and first post from topic 4..
it doesnt, it posts first post from topic 4, then the last 2 posts from topic 3??? and nothing from topic 1
A friend is someone who knows the song in your heart, and will sing, when you forget the words..
User avatar
tbackoff
Former Team Member
Posts: 7064
Joined: Thu Jun 04, 2009 1:41 am
Location: cheerleading practice
Name: Tabitha Backoff

Re: Posts on external pages

Post by tbackoff »

Ah, OK. There is a pitfall in the code. Change this:

Code: Select all

$topics = 'SELECT * FROM ' . TOPICS_TABLE . '
	' . $forum_id_where . '
		AND topic_status <> ' . ITEM_MOVED . '
		AND topic_approved = 1
	ORDER BY topic_id DESC';
to this:

Code: Select all

$topics = 'SELECT * FROM ' . TOPICS_TABLE . '
	' . $topic_id_where . '
		AND topic_status <> ' . ITEM_MOVED . '
		AND topic_approved = 1
	ORDER BY topic_id DESC';
You can then leave your forum_id array blank. This will then pull the first post from the specified topics.
Flying is the second best thrill to cheerleaders; being caught is the first.
User avatar
SpongeWeb
Registered User
Posts: 766
Joined: Thu Jan 27, 2005 2:57 pm
Location: End of the Universe
Contact:

Re: Posts on external pages

Post by SpongeWeb »

those two blocks of code are identical?? and that block of code doesnt exist?
A friend is someone who knows the song in your heart, and will sing, when you forget the words..
User avatar
tbackoff
Former Team Member
Posts: 7064
Joined: Thu Jun 04, 2009 1:41 am
Location: cheerleading practice
Name: Tabitha Backoff

Re: Posts on external pages

Post by tbackoff »

SpongeWeb wrote:those two blocks of code are identical??
No, have a look at the second line. Also, I forgot that that part of the code is not in your file (we are using different parts of code).

I will be honest, looking at your code, I'm not sure where it is failing. While mine pulls the first post of the specified topics, it doesn't show the post text or anything (like I said, it is exactly the same as phpbb.com).
Flying is the second best thrill to cheerleaders; being caught is the first.
User avatar
SpongeWeb
Registered User
Posts: 766
Joined: Thu Jan 27, 2005 2:57 pm
Location: End of the Universe
Contact:

Re: Posts on external pages

Post by SpongeWeb »

t_backoff wrote:
SpongeWeb wrote:those two blocks of code are identical??
No, have a look at the second line. Also, I forgot that that part of the code is not in your file (we are using different parts of code).

I will be honest, looking at your code, I'm not sure where it is failing. While mine pulls the first post of the specified topics, it doesn't show the post text or anything (like I said, it is exactly the same as phpbb.com).
I got this code from phpbb its posted on the link, yours must be different, I have my own code that pulls the last 10 topics, thats easy enough, this is just not pulling the right info for some reason, thanks for trying though
A friend is someone who knows the song in your heart, and will sing, when you forget the words..
User avatar
tbackoff
Former Team Member
Posts: 7064
Joined: Thu Jun 04, 2009 1:41 am
Location: cheerleading practice
Name: Tabitha Backoff

Re: Posts on external pages

Post by tbackoff »

Here is my (modified) file if you want to have a look. It pulls the topic title, date, and link. Hopefully it will help you.

Code: Select all

<?php
/**
* @ignore
*/
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();

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

	$out_where = '';

	if ($size_gen_id > 0)
	{
		$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))
			{
				$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();
$forum_id_where = create_where_clauses($forum_id, 'forum');

$topic_id = array(1,2,4,5,7,8);
$topic_id_where = create_where_clauses($topic_id, 'topic');

$topics = 'SELECT * FROM ' . TOPICS_TABLE . '
	' . $topic_id_where . '
		AND topic_status <> ' . ITEM_MOVED . '
		AND topic_approved = 1
	ORDER BY topic_id DESC';

	$topics_result = $db->sql_query_limit($topics, $search_limit);

	while ($topics_row = $db->sql_fetchrow($topics_result))
	{
		$topic_title = $topics_row['topic_title'];
		$topic_date = $user->format_date($topics_row['topic_time']);
		$topic_link = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "t=" . $topics_row['topic_id']);

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

page_header('Latest Announcements');

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

page_footer();

?>
Flying is the second best thrill to cheerleaders; being caught is the first.
User avatar
SpongeWeb
Registered User
Posts: 766
Joined: Thu Jan 27, 2005 2:57 pm
Location: End of the Universe
Contact:

Re: Posts on external pages

Post by SpongeWeb »

thanks anyway, but this doesnt produce the post text or author, it does pull the correct topic ids though! how fustrating! thanks though
A friend is someone who knows the song in your heart, and will sing, when you forget the words..
Locked

Return to “[3.0.x] MOD Writers Discussion”