Page 1 of 1

Displaying posts on external page from selected forums only

PostPosted: Tue Jul 10, 2012 2:38 pm
by allanshr
Hi Battye,

My name's Stewart, I'm currently working on a website for a Scout group in the South West of England. I recently came across your post regarding displaying phpBB posts on an external page - a perfect solution for what I was wanting to do! However, there is just one small problem that I can't figure out - probably due to having no PHP knowledge whatsoever. If you could give some help, it would be very much appreciated!

I'm working with example 3, displaying the last 10 posts from specific forums only. If I use the code:
$forum_id = array(3);
$forum_id_where = create_where_clauses($forum_id, 'forum');

Then the error 'A list of topics/forums has not been created' is returned, which is correct (Forum 3 doesn't refer to any forum in existance on the board I'm using.) So, this shows me that the code is able to 'ignore' all non-relevant forums if an irrelevant number is selected in the array. I then try using the code:

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

This time, Forum 19 does exist. This is where the code runs into a problem for me - the script will select the relevant posts from Forum 19, which is fine, but it will also take the posts from the rest of the board too. It doesn't read solely from 19. On the upside, it does still adhere to only retrieving posts that the user is allowed to see (so it hides certain posts of the user isn't logged in.)

As I say, I don't have the knowledge to be able to scrutinise the code, I can only make the more obvious changes here and there. I can't see where the error is occuring, nor have I been able to find anyone with a similar problem online.

Any help would be great, though I appreciate you've probably got a lot of other things to do!

Kind regards,

Stewart Allan

Re: Displaying posts on external page from selected forums o

PostPosted: Sun Jul 15, 2012 1:37 pm
by battye
Hi Stewart, sorry for the delayed reply.

Could you please help me find the cause of this problem by making the following change. Where in your code you have this:

Code: Select all
$forum_id = array(19);
$forum_id_where = create_where_clauses($forum_id, 'forum');


Can you add this below it:

Code: Select all
echo $forum_id_where;


Then refresh the page. The echo line will display the contents of the forum id where clause. At the top of the page you should see a line or two starting with: "WHERE forum_id = 19". At least that is what it should say :)

Copy and paste what you see into your reply here. You can then remove the echo line from your code again.

Thanks

--

Edit, I've been thinking about this a little bit more. Something to try could be this:

Replace this in your code:

Code: Select all
'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',


With this:

Code: Select all
'WHERE' => str_replace( array('WHERE ', 'forum_id'), array('', 't.forum_id'), $forum_id_where) . ',


Let me know if that works.

Re: Displaying posts on external page from selected forums o

PostPosted: Mon Jul 16, 2012 8:22 pm
by allanshr
Hi Battye,

Thanks for your reply! I've tried the modifications as given, but to no joy. Here's a rundown of results:

With the echo function used as first suggested, I get the following at the top of my page -
WHERE forum_id = 19 [phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4668: Cannot modify header information - headers already sent by (output started at /homepages/12/d421831733/htdocs/preview/News.php:103)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4670: Cannot modify header information - headers already sent by (output started at /homepages/12/d421831733/htdocs/preview/News.php:103)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4671: Cannot modify header information - headers already sent by (output started at /homepages/12/d421831733/htdocs/preview/News.php:103)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4672: Cannot modify header information - headers already sent by (output started at /homepages/12/d421831733/htdocs/preview/News.php:103)


I had a look at the code after the 'WHERE' function - it would appear I was using the code from Example 4, as opposed to Example 3. If I correct this to
Code: Select all
'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',


I get the error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND t.topic_status <> 2 AND t.topic_approved = 1 ORDER BY p.post_id DESC LIMIT ' at line 2 [1064]

on a phpBB style error page

If I use the suggested correction to
Code: Select all
'WHERE' => str_replace( array('WHERE ', 'forum_id'), array('', 't.forum_id'), $forum_id_where) . ',


I get
Parse error: syntax error, unexpected T_STRING, expecting ')' in /homepages/12/d421831733/htdocs/preview/News.php on line 128

on a blank white page.

If it's of any use to you, here is the full code I'm using for the page
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 : './phpBB3/';
$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 = 10;

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

$topic_id = array();
$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) . ',

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

         $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('The title of your page goes here');

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

page_footer();
?>


Many thanks again for all your help - it's much appreciated!

Stewart

Re: Displaying posts on external page from selected forums o

PostPosted: Thu Jan 03, 2013 5:43 pm
by Croc442
*bump*