Get topics from announcement forum

A perfect place for code snippets to small to be a MOD or a library.
Or for examples on how to use our libraries.
Post Reply
Elglobo
Past Contributor
Past Contributor
Posts: 119
Joined: 15 Jul 2008, 19:42

Get topics from announcement forum

Post by Elglobo »

This post was originally posted by Kenny at 6 String MODs.

Basically, create a functions_announce.php file with the following content:

Code: Select all

<?php
/**
*
*&nbsp;@package&nbsp;phpBB3
*&nbsp;@version&nbsp;$Id:&nbsp;functions_announce.php&nbsp;0013&nbsp;17:07&nbsp;17/08/2008&nbsp;kenny
*&nbsp;@copyright&nbsp;(c)&nbsp;2005&nbsp;phpBB&nbsp;Group
*&nbsp;@license&nbsp;http://opensource.org/licenses/gpl-license.php&nbsp;GNU&nbsp;Public&nbsp;License
*
*/

/**
*&nbsp;@ignore
*/
if&nbsp;(!defined('IN_PHPBB'))
{
&
nbsp;&nbsp;&nbsp;&nbsp;exit;
}

/**
*&nbsp;Pull&nbsp;info&nbsp;for&nbsp;latest&nbsp;announcements&nbsp;//&nbsp;cherokee&nbsp;red&nbsp;-&nbsp;08/13/2008
*&nbsp;
*&nbsp;$forum_id&nbsp;is&nbsp;the&nbsp;forum&nbsp;you&nbsp;want&nbsp;to&nbsp;pull&nbsp;info&nbsp;from
*&nbsp;$topic_limit&nbsp;is&nbsp;the&nbsp;amount&nbsp;of&nbsp;topics&nbsp;to&nbsp;display
*&nbsp;$title&nbsp;is&nbsp;the&nbsp;name&nbsp;you&nbsp;want&nbsp;to&nbsp;give&nbsp;to&nbsp;the&nbsp;block&nbsp;i.e.&nbsp;Announcements,&nbsp;or&nbsp;Latest&nbsp;Posts
*
*/
function&nbsp;get_announcements($forum_id,&nbsp;$topic_limit,&nbsp;$title)
{

&
nbsp;&nbsp;&nbsp;&nbsp;global&nbsp;$db,&nbsp;$template;
&
nbsp;&nbsp;&nbsp;&nbsp;global&nbsp;$phpbb_root_path,&nbsp;$phpEx;

&
nbsp;&nbsp;&nbsp;&nbsp;$an_sql&nbsp;=&nbsp;'SELECT&nbsp;topic_id,&nbsp;forum_id,&nbsp;topic_title
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FROM&nbsp;'
&nbsp;.&nbsp;TOPICS_TABLE&nbsp;.&nbsp;'
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;forum_id&nbsp;=&nbsp;'
&nbsp;.&nbsp;$forum_id&nbsp;.&nbsp;'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ORDER&nbsp;BY&nbsp;topic_id&nbsp;DESC'
;
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&
nbsp;&nbsp;&nbsp;&nbsp;//query&nbsp;the&nbsp;db
&nbsp;&nbsp;&nbsp;&nbsp;$result&nbsp;=&nbsp;$db->sql_query_limit($an_sql,&nbsp;$topic_limit);

&
nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Lets&nbsp;build&nbsp;a&nbsp;page&nbsp;...
&nbsp;&nbsp;&nbsp;&nbsp;$template->assign_vars(array(
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'U_FORUM'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=>&nbsp;append_sid("{$phpbb_root_path}viewforum.$phpEx",&nbsp;'f='&nbsp;.&nbsp;$forum_id),
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'L_TITLE'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=>&nbsp;$title)
&
nbsp;&nbsp;&nbsp;&nbsp;);

&
nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;($announce&nbsp;=&nbsp;$db->sql_fetchrow($result))&nbsp;
&
nbsp;&nbsp;&nbsp;&nbsp;{
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$template->assign_block_vars('announce_row',&nbsp;array(
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'TOPIC_TITLE'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=>&nbsp;$announce['topic_title'],
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'U_ANNOUNCE'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=>&nbsp;append_sid("{$phpbb_root_path}viewtopic.$phpEx",&nbsp;'f='&nbsp;.&nbsp;$announce['forum_id']&nbsp;.&nbsp;'&amp;'&nbsp;.&nbsp;'t='&nbsp;.&nbsp;$announce['topic_id']),
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;));
&
nbsp;&nbsp;&nbsp;&nbsp;}
&
nbsp;&nbsp;&nbsp;&nbsp;$db->sql_freeresult($result);

&
nbsp;&nbsp;&nbsp;&nbsp;$template->set_filenames(array(
&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'content'&nbsp;=>&nbsp;'announce_body.html',
&
nbsp;&nbsp;&nbsp;&nbsp;));

&
nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Thanks&nbsp;Vic&nbsp;^_^
&nbsp;&nbsp;&nbsp;&nbsp;$content&nbsp;=&nbsp;$template->assign_display('content',&nbsp;'',&nbsp;true);

&
nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$content;
}
?>

Here is the HTML you'll need. Create a file called announce_body.html and place it in your templates folder with the following code.

Code: Select all

   <div class="navbar">
         <div class="inner"><span class="corners-top"><span></span></span>
      <h3><a href="{U_FORUM}" title="{L_TITLE}">{L_TITLE}</a></h3>
      <span style="font-size: 1.1em;">
         <!-- BEGIN announce_row -->
               <a href="{announce_row.U_ANNOUNCE}" title="{announce_row.POST_SUBJECT}" style="font-size: 1.1em;">{announce_row.TOPIC_TITLE}</a><br />
         <!-- END announce_row -->
      </span>
         <span class="corners-bottom"><span></span></span></div>
   </div>
   <br />

Ok, now in the php file that will be serving the template, you need to include the function file.
So look at the top of the php file for the file includes and add a new one underneath

Code: Select all

include($phpbb_root_path . 'includes/functions_announce.' . $phpEx);

And look for where the file assigns variable to the template

Code: Select all

$template->assign_vars(array(

and in there add our function :)

Code: Select all

   'ANNOUNCEMENT'    => get_announcements('2', '5', 'Insert Block Title Here'),

The numbers in the brackets corresponds to our function file. The 1st number is the forum id we want to take the announcements from and the 2nd number is the amount of announcements. The last part is the title you want to show at the top of the block. It's seriously that simple.

Lastly, you need to edit the template file that corresponds to the php file you are using to show the announcements i.e. index_body.html for index.php, viewtopic_body.html for viewtopic.php
Simply add {ANNOUNCEMENT} wherever you want your announcement to show and you're set 8-)
phpBB-Services.com: L'hébergement et l'assistance de votre forum phpBB en toute tranquillité.

Myff.fr - My First forum, créer votre forum phpBB3 facilement !
Post Reply