1 (edited by Sensei Eggwoah 2008-03-13 21:13)

Topic: Script to get news/blog entries from the topics in a specified forum

Punbb News Script - by Jesse Spillane (http://www.jessespillane.com)

edits:
March 8th  Fixed the comment link.  Sorry if this caused any trouble.
March 13th Now shows poster name below post title.

Motivation

My motivation was to create a news or blog system which takes the the latest topics in a specified forum and uses them as news entries on my site.  For example, I have a forum which the permissions are set so only administrators can post topics.   Each of these topics are treated as news entries. I wanted my index page to display the latest X posts in the news forum.  I didn't want to use the admin news generator plugin as I wanted this system to be dynamic.  I also didn't want to sort through bloated portal system mods and rip them apart to get a few features I wanted.

I searched for about an hour for a script which did this, but I eventually gave up and decided to write one myself.  I figure someone may get some use out of it.

Instructions

The script is very simple to use.  This file will output the html for the news.  Below is a description of the variables which you should set:

$news_forum_id - Set this to the forum which contains your news
$limit - This is the number news entries to display
$forum_path - Set this to the path of your punbb install.

<?php

/**
Punbb News Script
Written by: Jesse Spillane (http://www.jessespillane.com)
*/

$news_forum_id = 5;
$limit = 5;
$forum_path = './forum/';

define('PUN_ROOT', $forum_path);
require PUN_ROOT.'include/common.php';

define('PUN_TURN_OFF_MAINT', 1);
define('PUN_QUIET_VISIT', 1);

require PUN_ROOT.'include/parser.php';

echo "<h2>News</h2>";

$result = $db->query('select id, subject, num_replies, posted from '.$db->prefix.'topics WHERE forum_id='.$news_forum_id.' ORDER BY -posted LIMIT '.$limit.';')or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error());
while ($row = $db->fetch_assoc($result))
{
    $id = $row['id'];
    $subject = $row['subject'];
    $num_replies = $row['num_replies'];
    $posted = date("F j, Y, g:i a", $row['posted']);

    $result2 = $db->query('select message, poster from '.$db->prefix.'posts WHERE topic_id='.$id.' ORDER BY id LIMIT 1 ')or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error());
    
    $post = $db->fetch_assoc($result2);
    $message = parse_message($post['message'], 1);
    $poster = $post['poster'];

?>


<div style='margin-bottom: 30px;'>    
  <h3>
    <?php echo $subject; ?>
  </h3> 

  <hr />

  <div>
    <i> by <?php echo $poster; ?> </i>
  </div>

  <div>
    <i> <?php echo $posted; ?> </i>
  </div>

  <div>
    <?php echo $message; ?>
  </div>

  <div style='float: right;'>
    <?php
      echo '<a href='.$forum_path.'viewtopic.php?id='.$id.'>Comments ('.$num_replies.')</a>';
    ?>      
  </div>

</div>
<br />

<?php
  

}

?>

Sorry if this has been done.  It is quite simple.

2 (edited by Goldex 2008-03-11 15:48)

Re: Script to get news/blog entries from the topics in a specified forum

Thanks! This is infact easier working (I'm gonna use this for recently posted topics), but I do find myself bumping against one problem.

When I use it with an PHP-include (so I get index.php?page=news) it gives me an error

Fatal error: Cannot redeclare check_cookie() (previously declared in .mywebsite/forum/include/functions.php:28) in .mywebsite/forum/include/functions.php on line 28

Any ideas on how to fix this?

EDIT: Never mind! I found the problem. This was in my index.php:

<?php define('PUN_ROOT', 'forum/'); ?>
<?php include_once PUN_ROOT.'include/common.php'; ?>

Again, thank you! This script is just what I needed! smile Haven't been to these forums in like 2 months and I happen to find exactly what I was looking for.

Re: Script to get news/blog entries from the topics in a specified forum

You're welcome. smile

Re: Script to get news/blog entries from the topics in a specified forum

Oh and btw, is there a way to include the author of the post, so you can get something like "Posted by: <username>?

Again, thank you very much for this useful script!

Re: Script to get news/blog entries from the topics in a specified forum

I edited the code in my first post to add the author.

Re: Script to get news/blog entries from the topics in a specified forum

Script works great! I'm trying to let the script load the recent news from multiple forum ID's, but nothing worked. I highly doubt that it's possible, but do you know a way? ^^;

7

Re: Script to get news/blog entries from the topics in a specified forum

cool!

Re: Script to get news/blog entries from the topics in a specified forum

I didn't test it, but I'm almost certain this should work. 

find:

$news_forum_id = 5;

add after:

$news_forum_id2 =6;

find this line:

$result = $db->query('select id, subject, num_replies, posted from '.$db->prefix.'topics WHERE forum_id='.$news_forum_id.' ORDER BY -posted LIMIT '.$limit.';')or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error());

Replace with:

$result = $db->query('select id, subject, num_replies, posted from '.$db->prefix.'topics WHERE forum_id='.$news_forum_id.' OR forum_id='.$news_forum_id2.' ORDER BY -posted LIMIT '.$limit.';')or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error());

All this will load news from 2 forums.  If you need more, just make similar changes in the WHERE clause. For example if you want to limit to 4 forums do something along the lines of:

WHERE forum_id='.$news_forum_id.' OR forum_id='.$news_forum_id2.' OR forum_id='.$news_forum_id3.' OR forum_id='.$news_forum_id4.'

for the WHERE clause.  Also define the variables for each of the $news_forum_id*
-------------------

Alternately if you want to load from all forums, just don't specify the WHERE clause which currently limits the SQL query to specific forum_ids.

Query for getting all forums:

$result = $db->query('select id, subject, num_replies, posted from '.$db->prefix.'topics ORDER BY -posted LIMIT '.$limit.';')or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error());

Hope this helps

Re: Script to get news/blog entries from the topics in a specified forum

Thanks a lot - it works unbelievable great!

But I have to questions / requests?
- How is it possible to link the profile of the author?
- Is it possible to show the name of the subboard? My idea: I want to create a category News with boards of many subjects. So if I post, i'll show the name of forum at the news page ...

Thanks a lot!

10

Re: Script to get news/blog entries from the topics in a specified forum

seem like reinventing the wheel again... there are a few index mod out there.. I know i designed one .....


Q

My stuff or my style might sux, but atleast I'm willing to help when I can.
Don't be stupid and help ! We are the stupid one's !!!

Re: Script to get news/blog entries from the topics in a specified forum

OK, I think you're right.
But I have another question: Is it possible to ignore sticky-threads?

12

Re: Script to get news/blog entries from the topics in a specified forum

Great script, very customizable too which I like. Is it possible to display the results reversed? For example, I have a band website where we have the concert dates posted in order, but I'd like to reverse that order (if that makes sense)

Thanks,
Dan

13

Re: Script to get news/blog entries from the topics in a specified forum

ORDER BY -posted

Change that to either:

ORDER BY posted DESC

or:

ORDER BY posted ASC

One of those will flip the order.

14

Re: Script to get news/blog entries from the topics in a specified forum

Klappspaten wrote:

But I have another question: Is it possible to ignore sticky-threads?

Belated reply, but change this:

WHERE forum_id='.$news_forum_id.'

to this:

WHERE forum_id='.$news_forum_id.' AND sticky=0

15

Re: Script to get news/blog entries from the topics in a specified forum

Thanks for the super quick reply MattF, that worked flawlessly.

Re: Script to get news/blog entries from the topics in a specified forum

This looks like a cool script.

Having trouble using it though, error says /forums/include/common.php is not found.

My forum path declaration is:
$forum_path = './forums/';

and the .php file is in the root forums folder mysite.com/forums

Does this work with v1.3.1? (Using v1.3.1)