Topic: How to fetch posts from different forums using AP_News_Generator??

Okey here is the code:

<?php
include('new.php');
// The forum from which we'll pull the news bits
$forum_id = 2;

// Number of news posts to include in the index
$num_posts_index = 5;

// Path to news item template
$template_path = PUN_ROOT.'plugins/AP_News_Generator/news.tpl';

// Directories in which plugin will save generated markup (must end with slash)
$output = '';
$output_dir_archive = PUN_ROOT.'plugins/AP_News_Generator/archive/';



    if (!isset($archive))
    {
    // Generate front page news
    $result = $db->query('SELECT id, subject FROM '.$db->prefix.'topics WHERE forum_id='.$forum_id.' ORDER BY sticky DESC, posted DESC LIMIT 0, '.$num_posts_index) or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error());
    if (!$db->num_rows($result))
        message('There are no topics to generate news based on in forum with ID = '.$forum_id.'.');

    

    $news_tpl = file_get_contents($template_path) or error('Unable to open new item template '.$template_path.'. Make sure $template_path is correct', __FILE__, __LINE__);

/*    $fh = @fopen($output_dir_latest.'news.html', 'wb');
    if (!$fh)
        error('Unable to write news to '.$output_dir_latest.'news.html. Please make sure PHP has write access to the directory '.$output_dir_latest, __FILE__, __LINE__);
*/
    while ($cur_topic = $db->fetch_assoc($result))
    {
        $result2 = $db->query('SELECT posted, poster, message, hide_smilies FROM '.$db->prefix.'posts WHERE topic_id='.$cur_topic['id'].' ORDER BY posted ASC LIMIT 1') or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error());
        $cur_post = $db->fetch_assoc($result2);
        $num_comments = $db->result($db->query('SELECT COUNT(id)-1 AS num FROM '.$db->prefix.'posts WHERE topic_id='.$cur_topic['id']));
        
        $search = array('<news_subject>', '<news_posted>', '<news_poster>', '<news_message>', '<news_comments>');
        $replace = array(pun_htmlspecialchars($cur_topic['subject']), date('Y-m-d H:i', $cur_post['posted']), pun_htmlspecialchars($cur_post['poster']), parse_message($cur_post['message'], $cur_post['hide_smilies']), '<a href="'.$pun_config['o_base_url'].'/viewtopic.php?id='.$cur_topic['id'].'">Kommentarer ('.$num_comments.')</a>');

        $output .=  str_replace($search, $replace, $news_tpl);
    }
    
    $output .= '';
    
    //display news
    echo $output;
    
}    
 else 
    {    
// Generate monthly archives
    $year_end = intval(date('Y'));
    $month_end = intval(date('n'));

    $year_start = ($month_end != 1) ? $year_end : $year_end-1;
    $month_start = ($month_end != 1) ? $month_end-1 : 12;

    // How far back should we go?
    $result = $db->query('SELECT MIN(posted) FROM '.$db->prefix.'topics WHERE forum_id='.$forum_id.'') or error('Unable to fetch earliest topic', __FILE__, __LINE__, $db->error());
    $history_limit = $db->result($result);

    $year_limit = intval(date('Y', $history_limit));
    $month_limit = intval(date('n', $history_limit));

    while ($year_end > $year_limit || $month_end > $month_limit)
    {
        $result = $db->query('SELECT id, subject FROM '.$db->prefix.'topics WHERE forum_id='.$forum_id.' AND posted>=UNIX_TIMESTAMP(\''.$year_start.'-'.$month_start.'-01\') AND posted<UNIX_TIMESTAMP(\''.$year_end.'-'.$month_end.'-01\') ORDER BY posted DESC') or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error());
        if ($db->num_rows($result))
        {
            $news_tpl = file_get_contents($template_path) or error('Unable to open new item template '.$template_path.'. Make sure $template_path is correct', __FILE__, __LINE__);

            $fh = @fopen($output_dir_archive.$year_end.'-'.($month_start > 9 ? $month_start : '0'.$month_start).'.html', 'wb');
            if (!$fh)
                error('Unable to write news archive to '.$output_dir_archive.$year_end.'-'.($month_end > 9 ? $month_end : '0'.$month_end).'.html Please make sure PHP has write access to the directory '.$output_dir_archive, __FILE__, __LINE__);

            while ($cur_topic = $db->fetch_assoc($result))
            {
                $result2 = $db->query('SELECT posted, poster, message, hide_smilies FROM '.$db->prefix.'posts WHERE topic_id='.$cur_topic['id'].' ORDER BY posted ASC LIMIT 1') or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error());
                $cur_post = $db->fetch_assoc($result2);
                $num_comments = $db->result($db->query('SELECT COUNT(id)-1 AS num FROM '.$db->prefix.'posts WHERE topic_id='.$cur_topic['id']));
                
                $search = array('<news_subject>', '<news_posted>', '<news_poster>', '<news_message>', '<news_comments>');
                $replace = array(pun_htmlspecialchars($cur_topic['subject']), date('Y-m-d H:i', $cur_post['posted']), pun_htmlspecialchars($cur_post['poster']), parse_message($cur_post['message'], $cur_post['hide_smilies']), '<a href="'.$pun_config['o_base_url'].'/viewtopic.php?id='.$cur_topic['id'].'">Kommentarer ('.$num_comments.')</a>');

                fwrite($fh, str_replace($search, $replace, $news_tpl));
            }

            fclose($fh);
        }

        $year_end = $year_start;
        $month_end = $month_start;
        $year_start = ($month_end != 1) ? $year_end : $year_end-1;
        $month_start = ($month_end != 1) ? $month_end-1 : 12;
    }

//Archive display
        

    $dir = "./archive/";
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
       $file_arr = array(); $f = 0;
       
        while (false !== ($file = readdir($dh))) {
            if ($file != "." && $file != ".." && $file != "index.php" && $file[0] != ".") {
                 $file_arr[$f++] = $file;
             }
        }
        closedir($dh);
    }
      sort( $file_arr ); reset( $file_arr );
    for( $i=1; $i < count( $file_arr ) +1 ; $i++ ) {
        echo "<a href=\"'.PUN_ROOT.'news.php?month='.$i.'&archive=true\">".str_replace('.html', '', $file_arr[$i-1])."</a><br>";    
         }
        
}

    if(isset($_GET['month']) && (file_exists('./archive/'.$year_end.'-0'.$month.'.html') || file_exists('./archive/'.$year_end.'-0'.$month.'.html') ) )
    {
        if($month < 10)        
        include './archive/'.$year_end.'-0'.$month.'.html';
        else 
        include './archive/'.$year_end.'-'.$month.'.html';
    }
    else if(isset($_GET['month']) && (!file_exists('./archive/'.$year_end.'-0'.$month.'.html') || !file_exists('./archive/'.$year_end.'-0'.$month.'.html') ) )
    message($lang_common['Bad request']);
    
}


?>

Where can i change so it fetches news from multiple forums?? wink

Re: How to fetch posts from different forums using AP_News_Generator??

1. Change

$forum_id = 2;

to

$forum_id = '2,4,10';


2. Search and replace all

forum_id='.$forum_id.'

with

forum_id IN('.$forum_id.')


That should do it. Haven't tried it though.

"Programming is like sex: one mistake and you have to support it for the rest of your life."

3 (edited by novasie 2005-11-24 17:11)

Re: How to fetch posts from different forums using AP_News_Generator??

Id did as you wrote but i only get this when i try to load it.. "
An error was encountered
Error: Unable to fetch topic list.
" what shell i do? I did ofc change "$forum_id = '2,4,10';" to the right forums numbers but it still cant fetch it. sad

Re: How to fetch posts from different forums using AP_News_Generator??

Enable DEBUG mode and try again. I need the full error message in order to help you out.

"Programming is like sex: one mistake and you have to support it for the rest of your life."

5 (edited by novasie 2005-11-30 16:32)

Re: How to fetch posts from different forums using AP_News_Generator??

Well i have enabled debug mode but it doesnt matter cuz i cant even see the site. I only get "Parse error: parse error, unexpected T_STRING in /hsphere/local/home/huset/hus1.se/news.php on line 62"

line 62 consists of

$result = $db->query('SELECT MIN(posted) FROM '.$db->prefix.'topics WHERE forum_id IN('.$forum_id.') or error('Unable to fetch earliest topic', __FILE__, __LINE__, $db->error());

where

forum_id IN('.$forum_id.')

is one of those that i changed.

Re: How to fetch posts from different forums using AP_News_Generator??

You didn't replace the way I told you to. The line should be:

$result = $db->query('SELECT MIN(posted) FROM '.$db->prefix.'topics WHERE forum_id IN('.$forum_id.')') or error('Unable to fetch earliest topic', __FILE__, __LINE__, $db->error());

"Programming is like sex: one mistake and you have to support it for the rest of your life."