sWG community

What's going on:
I'd rather have a monkey in... in General discussions by ContessaQ
Lapland closed in General discussions by Superduper
Games Tournaments in General discussions by Superduper






Advertisement




Go Back   sWG community > Webmaster forums > Tutorials


Reply
 
Thread Tools Display Modes
Old 11-07-2008, 04:51 AM   #1
Josh@SWG
Forum admin
 
Josh@SWG's Avatar
 
Join Date: Jun 2008
Location: Hampshire
Posts: 816
Credits: 1,520,460,678
Josh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of light
Send a message via MSN to Josh@SWG



Default Latest topics in phpBB3

Recent topics in phpBB3
This tutorial/phpbb hack will allow you display recent topics from your forum OUTSIDE of your phpBB3 root folder. The file however MUST still be on the same server!

At current this tutorial is for mysql databases only. This tutorial does not use phpBB3's DBAL in order to improve load times.

1. Create a new file called recent-topics.php
Paste the below code into your new file:

PHP Code:
<?

// change to the path to your forum
$path './demoforum/';

// amount of topics to show
$amount 5;

// forum ids to NOT show topics forum, separated by commas
$exclude '';

// ------ no need to edit below ------ //

require_once("{$path}config.php");

$conid mysql_connect($dbhost$dbuser$dbpasswd);
$db    mysql_select_db($dbname$conid);

unset(
$dbpasswd);

$sql_where 'WHERE `forum_id` > -1 '

if(
$exclude != ''
{     
    
$exclude_forums explode(','$exclude); 
    foreach (
$exclude_forums as $id
    { 
        if (
$id 0
        { 
            
$sql_where .= ' AND `forum_id` <> ' trim($id); 
        } 
    } 


$sql "SELECT `topic_id`, `topic_title` FROM `{$table_prefix}topics`  {$sql_where} ORDER BY `topic_time`DESC LIMIT 0, {$amount}";
$result mysql_query($sql$conid);

$topics_html '';
while(
$topic mysql_fetch_array($result))
{
    
$topics_html .= "<a href=\"{$path}viewtopic.php?t={$topic['topic_id']}\">{$topic['topic_title']}</a><br />";
}
mysql_close($conid);

echo 
$topics_html;
?>
What it does...
PHP Code:
$path './demoforum/'
This the path to your phpBB3 file relative to the file which will display the most recent topics

PHP Code:
$amount 5
This is the amount of topics to show

PHP Code:
$exclude ''
Here you can define any forums to exclude. Topics from these forums will not show. This is a list separated by commas, e.g.:
PHP Code:
$exclude '3,8,98'
The numbers are simply the forum IDs of the forums you wish to exclude. These can be obtained by looking at the URL when viewing a forum. the URL will be something like: domain.com/forum/viewforum.php?f=9 where, in this case, 9 is the forum ID

PHP Code:
require_once("{$path}config.php"); 
This line will include the contents of your phpBB3 config.php file in order to connect to the database.

PHP Code:
$conid mysql_connect($dbhost$dbuser$dbpasswd);
$db    mysql_select_db($dbname$conid); 
These two lines connect to the mysql database running your server using the details from the config file.

PHP Code:
unset($dbpasswd); 
This line unsets the database password variable for security reasons.

PHP Code:
$sql_where 'WHERE `forum_id` > -1 '
This starts the where clause of our SQL query, we make sure the forum_id is above -1 to make sure the topic has a forum associated with it.

PHP Code:
if($exclude != ''
{     
    
$exclude_forums explode(','$exclude); 
    foreach (
$exclude_forums as $id
    { 
        if (
$id 0
        { 
            
$sql_where .= ' AND `forum_id` <> ' trim($id); 
        } 
    } 

This part then excludes any forums. Firstly an if statement is used to check if we have any excluded forums, if it's empty this step is skipped. If not we use the explode function to create an array from the list given. We then use a foreach loop to loop through all the items of this array. Each loop the current value is assigned to the variable $id. We then use an if statement to check the $id is 'valid', i.e. greater than 0. If so we add an extra part to the WHERE clause to not include topics which has this forum ID

PHP Code:
$sql "SELECT `topic_id`, `topic_title` FROM `{$table_prefix}topics`  {$sql_where} ORDER BY `topic_time`DESC LIMIT 0, {$amount}"
This the SQL query we will be performing. This query will select the topic id and topic title fields from our topics database. We will order them by the time posted in descending order to get the most recent. We want to limit the results to the amount we have specified in the $amount variable.

PHP Code:
$result mysql_query($sql$conid); 
This performs the above query and stores the result in a variable called $result

PHP Code:
$topics_html '';
while(
$topic mysql_fetch_array($result))
{
    
$topics_html .= "<a href=\"{$path}viewtopic.php?t={$topic['topic_id']}\">{$topic['topic_title']}</a><br />";

This is where we start to create the topic list. The while loop loops around the rows returned from our query. The mysql_fetch_array function returns an array for the current row. In this case the array will have two items - the topic id and topic title for the current row. This array is stored in the variable $row.

We then create the hyperlink. We use
PHP Code:
$topics_html .= 
in order to append each hyperlink to the current html we have created. Simply using an equals sign alone would result in the $topics_html variable being overwritten every loop leaving us with only the last fetched topic.

Putting {} around variables in a PHP stirng using double quotes allows for variable interpolation. This means the variables are replaced by their value when the string is parsed.

PHP Code:
mysql_close($conid); 
This closes our connection to the database.

PHP Code:
echo $topics_html
This line outputs the list of topics!

2. Include the file!
In order to display the topics all you need to do is include the recent-topics.php file wherever you wish. Remember though that the $path variable in the recent-topics.php file need to be relative to the location of the file you are including it in, rather than the location of recent-topics.php itself.

To include the file on your page:
PHP Code:
<? include('recent-topics.php'); ?>
To give an idea of how you could output this...
PHP Code:
<h1>Recent topics</h1>
<p>These are the most recent topics from our forum...</p>
<? include('recent-topics.php'); ?>
Save all files and upload!

Any questions or support please ask in this thread

Last edited by Josh@SWG; 12-07-2008 at 07:30 AM.
Josh@SWG is offline   Reply With Quote

Advertisement - login or register to remove this ad!


Old 12-07-2008, 06:02 AM   #2
Josh@SWG
Forum admin
 
Josh@SWG's Avatar
 
Join Date: Jun 2008
Location: Hampshire
Posts: 816
Credits: 1,520,460,678
Josh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of light
Send a message via MSN to Josh@SWG



Default Re: Latest topics in phpBB3

For a demo of the result of this tutorial: http://awesomestyles.com/demoforum/recent-topics.php
Josh@SWG is offline   Reply With Quote
Old 12-07-2008, 07:19 AM   #3
Josh@SWG
Forum admin
 
Josh@SWG's Avatar
 
Join Date: Jun 2008
Location: Hampshire
Posts: 816
Credits: 1,520,460,678
Josh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of light
Send a message via MSN to Josh@SWG



Default Re: Latest topics in phpBB3

You can now exclude forums
Josh@SWG is offline   Reply With Quote
Old 12-07-2008, 09:54 AM   #4
Sir HP
Grand Vizier
 
Sir HP's Avatar
 
Join Date: Jan 2007
Posts: 4,236
Credits: 10,190
Sir HP is a splendid one to beholdSir HP is a splendid one to beholdSir HP is a splendid one to beholdSir HP is a splendid one to beholdSir HP is a splendid one to beholdSir HP is a splendid one to beholdSir HP is a splendid one to behold



Default Re: Latest topics in phpBB3

Originally Posted by Josh@SWG View Post
Recent topics in phpBB3
This tutorial/phpbb hack will allow you display recent topics from your forum OUTSIDE of your phpBB3 root folder. The file however MUST still be on the same server!

At current this tutorial is for mysql databases only. This tutorial does not use phpBB3's DBAL in order to improve load times.

1. Create a new file called recent-topics.php
Paste the below code into your new file:

PHP Code:
<?

// change to the path to your forum
$path './demoforum/';

// amount of topics to show
$amount 5;

// forum ids to NOT show topics forum, separated by commas
$exclude '';

// ------ no need to edit below ------ //

require_once("{$path}config.php");

$conid mysql_connect($dbhost$dbuser$dbpasswd);
$db    mysql_select_db($dbname$conid);

unset(
$dbpasswd);

$sql_where 'WHERE `forum_id` > -1 '

if(
$exclude != ''
{     
    
$exclude_forums explode(','$exclude); 
    foreach (
$exclude_forums as $id
    { 
        if (
$id 0
        { 
            
$sql_where .= ' AND `forum_id` <> ' trim($id); 
        } 
    } 


$sql "SELECT `topic_id`, `topic_title` FROM `{$table_prefix}topics`  {$sql_where} ORDER BY `topic_time`DESC LIMIT 0, {$amount}";
$result mysql_query($sql$conid);

$topics_html '';
while(
$topic mysql_fetch_array($result))
{
    
$topics_html .= "<a href=\"{$path}viewtopic.php?t={$topic['topic_id']}\">{$topic['topic_title']}</a><br />";
}
mysql_close($conid);

echo 
$topics_html;
?>
What it does...
PHP Code:
$path './demoforum/'
This the path to your phpBB3 file relative to the file which will display the most recent topics

PHP Code:
$amount 5
This is the amount of topics to show

PHP Code:
$exclude ''
Here you can define any forums to exclude. Topics from these forums will not show. This is a list separated by commas, e.g.:
PHP Code:
$exclude '3,8,98'
The numbers are simply the forum IDs of the forums you wish to exclude. These can be obtained by looking at the URL when viewing a forum. the URL will be something like: domain.com/forum/viewforum.php?f=9 where, in this case, 9 is the forum ID

PHP Code:
require_once("{$path}config.php"); 
This line will include the contents of your phpBB3 config.php file in order to connect to the database.

PHP Code:
$conid mysql_connect($dbhost$dbuser$dbpasswd);
$db    mysql_select_db($dbname$conid); 
These two lines connect to the mysql database running your server using the details from the config file.

PHP Code:
unset($dbpasswd); 
This line unsets the database password variable for security reasons.

PHP Code:
$sql_where 'WHERE `forum_id` > -1 '
This starts the where clause of our SQL query, we make sure the forum_id is above -1 to make sure the topic has a forum associated with it.

PHP Code:
if($exclude != ''
{     
    
$exclude_forums explode(','$exclude); 
    foreach (
$exclude_forums as $id
    { 
        if (
$id 0
        { 
            
$sql_where .= ' AND `forum_id` <> ' trim($id); 
        } 
    } 

This part then excludes any forums. Firstly an if statement is used to check if we have any excluded forums, if it's empty this step is skipped. If not we use the explode function to create an array from the list given. We then use a foreach loop to loop through all the items of this array. Each loop the current value is assigned to the variable $id. We then use an if statement to check the $id is 'valid', i.e. greater than 0. If so we add an extra part to the WHERE clause to not include topics which has this forum ID

PHP Code:
$sql "SELECT `topic_id`, `topic_title` FROM `{$table_prefix}topics`  {$sql_where} ORDER BY `topic_time`DESC LIMIT 0, {$amount}"
This the SQL query we will be performing. This query will select the topic id and topic title fields from our topics database. We will order them by the time posted in descending order to get the most recent. We want to limit the results to the amount we have specified in the $amount variable.

PHP Code:
$result mysql_query($sql$conid); 
This performs the above query and stores the result in a variable called $result

PHP Code:
$topics_html '';
while(
$topic mysql_fetch_array($result))
{
    
$topics_html .= "<a href=\"{$path}viewtopic.php?t={$topic['topic_id']}\">{$topic['topic_title']}</a><br />";

This is where we start to create the topic list. The while loop loops around the rows returned from our query. The mysql_fetch_array function returns an array for the current row. In this case the array will have two items - the topic id and topic title for the current row. This array is stored in the variable $row.

We then create the hyperlink. We use
PHP Code:
$topics_html .= 
in order to append each hyperlink to the current html we have created. Simply using an equals sign alone would result in the $topics_html variable being overwritten every loop leaving us with only the last fetched topic.

Putting {} around variables in a PHP stirng using double quotes allows for variable interpolation. This means the variables are replaced by their value when the string is parsed.

PHP Code:
mysql_close($conid); 
This closes our connection to the database.

PHP Code:
echo $topics_html
This line outputs the list of topics!

2. Include the file!
In order to display the topics all you need to do is include the recent-topics.php file wherever you wish. Remember though that the $path variable in the recent-topics.php file need to be relative to the location of the file you are including it in, rather than the location of recent-topics.php itself.

To include the file on your page:
PHP Code:
<? include('recent-topics.php'); ?>
To give an idea of how you could output this...
PHP Code:
<h1>Recent topics</h1>
<p>These are the most recent topics from our forum...</p>
<? include('recent-topics.php'); ?>
Save all files and upload!

Any questions or support please ask in this thread
I seeeeee......

Well I tried this and all I got was this appearing on my forum

Sir HP is offline   Reply With Quote
Old 16-07-2008, 08:54 AM   #5
Josh@SWG
Forum admin
 
Josh@SWG's Avatar
 
Join Date: Jun 2008
Location: Hampshire
Posts: 816
Credits: 1,520,460,678
Josh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of light
Send a message via MSN to Josh@SWG



Default Re: Latest topics in phpBB3

Josh@SWG is offline   Reply With Quote
Old 31-07-2008, 04:49 AM   #6
des1017
Junior Member
 
Join Date: Jul 2008
Posts: 1
Credits: 120
des1017 is on a distinguished road



Default Re: Latest topics in phpBB3

What about displaying the actual text in the post?

Such as using the posts table instead of topics table.
substr($topic['post_text'],0,20)

This outputs nothing. I noticed in the database the information is stored as BLOB
des1017 is offline   Reply With Quote
Old 02-08-2008, 11:35 PM   #7
Josh@SWG
Forum admin
 
Josh@SWG's Avatar
 
Join Date: Jun 2008
Location: Hampshire
Posts: 816
Credits: 1,520,460,678
Josh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of lightJosh@SWG is a glorious beacon of light
Send a message via MSN to Josh@SWG



Default Re: Latest topics in phpBB3

This script gets the information from the topics table whereas the post content is stored in the posts table. So you'd have to carry out another query every topic to get the post content for that topic.
Josh@SWG is offline   Reply With Quote
Old 03-08-2008, 12:05 AM   #8
Superduper
Spelling and Grammar Queen !!!
 
Superduper's Avatar
 
Join Date: Jan 2007
Location: Between Heaven and Hell
Posts: 5,478
Credits: 10,656
Superduper is a splendid one to beholdSuperduper is a splendid one to beholdSuperduper is a splendid one to beholdSuperduper is a splendid one to beholdSuperduper is a splendid one to beholdSuperduper is a splendid one to behold


Parking Zone Champion
Tourney winnerTournaments Won: 1

Default Re: Latest topics in phpBB3

HP loving your sig its fab sorry OT
__________________
NEVER EXPLAIN YOURSELF TO ANYONE
BECAUSE THE PERSON WHO LIKES YOU DOESN'T NEED IT
AND THE PERSON WHO DISLIKES YOU WONT BELIEVE IT
Superduper is offline   Reply With Quote
Old 04-08-2008, 12:56 AM   #9
x-linked
Junior Member
 
Join Date: Aug 2008
Posts: 1
Credits: 20
x-linked is on a distinguished road



Default Re: Latest topics in phpBB3

What are the limitations of storing the file. I am having a lot of problemgs getting the result to display.

My forums: http://forums.alphabluetech.com/kjhanlon/index.php
Test Display Page: http://www.alphabluetech.com/kjhanlon/testrecent.html

Any ideas. The subdomain is different but the server and host info should be the same. Is this limited to an PHP homepage instead of HTML? Any help would be greatly appreciated.

-kevin
x-linked is offline   Reply With Quote
Old 04-08-2008, 01:04 AM   #10
mr. bond
Member
 
Join Date: Dec 2007
Location: Plotting Evil on FreeForums.org!
Posts: 81
Credits: 24
mr. bond



Default Re: Latest topics in phpBB3

Try renaming that file from .html to .php.... subdomain does not matter..

Also, don't use the full URL when including, only use the relative URL IE

<?php include('http://mysite.com/file.php'); ?>
Use....
<?php include('./file.php'); ?>
__________________
www.TheJamesBond.org



:w00t:

Last edited by mr. bond; 04-08-2008 at 01:08 AM.
mr. bond is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hot Topics Forum Mental Minnie General discussions 52 13-10-2007 02:54 PM
phpBB3 RC3 plutomedia General discussions 5 13-07-2007 06:30 PM
can we converse by the medium of thread topics? scarlett o'hara General discussions 0 03-07-2007 11:55 AM