Forum stats on external page.

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
User avatar
tumba25
Supporter
Supporter
Posts: 1052
Joined: 24 Oct 2007, 13:10
Real name: Jari Kanerva
Location: Kokkola, Finland.
Contact:

Forum stats on external page.

Post by tumba25 »

This is a really simple way to show some forum data on external sites. There is of course more advanced ways to get the data, but no simpler.

Put this in a file in your forum root and name it stat_api.php

Code: Select all

<?php
/**
 * API to get some forum data to a external page.
 *
 * @copyright (c) 2010 phpbbmodders.net
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 *
 * This one returns number of users, topics and posts.
 */

/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

echo $config['num_users'] . '|' . $config['num_topics'] . '|' , $config['num_posts'];
?>


Then on your external site.

Code: Select all

<?php
$str = @file_get_contents('http://phpbbmodders.net/board/stat_api.php');

$arr = explode('|', $str);

echo $arr[0] . '<br />'; // Total users.
echo $arr[1] . '<br />'; // Total topics.
echo $arr[2] . '<br />'; // Total posts.
?>

Of course you can replace the data with whatever you want to send to the external site. Don't forget to change the url to your own.

Have fun.
c3dric
New member
New member
Posts: 1
Joined: 15 Oct 2010, 15:42

Re: Forum stats on external page.

Post by c3dric »

Hello everyone,

Could you tell me how to display the latest posts or topics with this API

Thank you for your help

Regards c3dric
User avatar
Peetra
Supporter
Supporter
Posts: 26
Joined: 29 Sep 2010, 04:12

Re: Forum stats on external page.

Post by Peetra »

I would use the normal Atom feed buildt in phpBB itself for such a thing. :)
robra
New member
New member
Posts: 2
Joined: 09 Mar 2012, 00:17

Re: Forum stats on external page.

Post by robra »

Hi, tumba25.
Could you help me to add "who is online" in this statistic ?

In stat_api.php I put:

Code: Select all

echo $config['num_users'] . '|' . $config['num_topics'] . '|' . $config['num_posts'] . '|' . echo $config['newest_username'] . '|' . $config['load_online_guests'] . '|' , $config['load_online'];

And in external page:

Code: Select all

echo $arr[0] . '<br />'; // Total users.
echo $arr[1] . '<br />'; // Total topics.
echo $arr[2] . '<br />'; // Total posts.
echo $arr[3] . '<br />'; // The last user registered.
echo $arr[4] . '<br />'; // Numbers of guests online.
echo $arr[5] . '<br />'; // Numbers of all users online.

But the 2 last lines didn't catch the numbers of the total users online, users registered online or guests online.
How can I do that ?

Thanks. [] [img]http://www.gsmfans.com.br/Smileys/GSMFans/cool.gif[/img]
User avatar
RMcGirr83
Supporter
Supporter
Posts: 6242
Joined: 30 Nov 2006, 14:23
Real name: Rich McGirr

Re: Forum stats on external page.

Post by RMcGirr83 »

There is no config for guests or all users online.

That is retrieved from the sessions table and is returned via the page_header function within the includes/functions.php file.
robra
New member
New member
Posts: 2
Joined: 09 Mar 2012, 00:17

Re: Forum stats on external page.

Post by robra »

RMcGirr83 wrote:There is no config for guests or all users online.

That is retrieved from the sessions table and is returned via the page_header function within the includes/functions.php file.
Hi, RMcGirr83
Thanks to answer me.
Then, is very complicated to show "WHO IS ONLINE" like is in the forum index page on an external page ?
Would be:
In total there are 238 users online :: 70 registered, 2 hidden and 166 guests (based on users active over the past 5 minutes)
Most users ever online was 680 on Mon Mar 05, 2010 2:47 pm.

[]. [img]http://www.gsmfans.com.br/Smileys/GSMFans/cool.gif[/img]
User avatar
RMcGirr83
Supporter
Supporter
Posts: 6242
Joined: 30 Nov 2006, 14:23
Real name: Rich McGirr

Re: Forum stats on external page.

Post by RMcGirr83 »

Read this

http://blog.phpbb.com/2009/11/09/how-to ... nal-pages/

pay attention to the last code block.
User avatar
EY
Supporter
Supporter
Posts: 204
Joined: 05 Nov 2006, 23:13
Real name: Elias
Location: Montreal

Re: Forum stats on external page.

Post by EY »

robra wrote:Could you help me to add "who is online" in this statistic ?

I currently went ahead and tested the following out:

To add the board stats + Who Is online:

PHP file:

Code: Select all

// Set some stats, get posts count from forums data if we... hum... retrieve all forums data
$total_posts   = $config['num_posts'];
$total_topics   = $config['num_topics'];
$total_users   = $config['num_users'];

$l_total_user_s = ($total_users == 0) ? 'TOTAL_USERS_ZERO' : 'TOTAL_USERS_OTHER';
$l_total_post_s = ($total_posts == 0) ? 'TOTAL_POSTS_ZERO' : 'TOTAL_POSTS_OTHER';
$l_total_topic_s = ($total_topics == 0) ? 'TOTAL_TOPICS_ZERO' : 'TOTAL_TOPICS_OTHER';

// Grab group details for legend display
if ($auth->acl_gets('a_group', 'a_groupadd', 'a_groupdel'))
{
   $sql = 'SELECT group_id, group_name, group_colour, group_type
      FROM ' . GROUPS_TABLE . '
      WHERE group_legend = 1
      ORDER BY group_name ASC';
}
else
{
   $sql = 'SELECT g.group_id, g.group_name, g.group_colour, g.group_type
      FROM ' . GROUPS_TABLE . ' g
      LEFT JOIN ' . USER_GROUP_TABLE . ' ug
         ON (
            g.group_id = ug.group_id
            AND ug.user_id = ' . $user->data['user_id'] . '
            AND ug.user_pending = 0
         )
      WHERE g.group_legend = 1
         AND (g.group_type <> ' . GROUP_HIDDEN . ' OR ug.user_id = ' . $user->data['user_id'] . ')
      ORDER BY g.group_name ASC';
}
$result = $db->sql_query($sql);

$legend = array();
while ($row = $db->sql_fetchrow($result))
{
   $colour_text = ($row['group_colour']) ? ' style="color:#' . $row['group_colour'] . '"' : '';
   $group_name = ($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name'];

   if ($row['group_name'] == 'BOTS' || ($user->data['user_id'] != ANONYMOUS && !$auth->acl_get('u_viewprofile')))
   {
      $legend[] = '<span' . $colour_text . '>' . $group_name . '</span>';
   }
   else
   {
      $legend[] = '<a' . $colour_text . ' href="' . append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=group&amp;g=' . $row['group_id']) . '">' . $group_name . '</a>';
   }
}
$db->sql_freeresult($result);

$legend = implode(', ', $legend);

// Assign index specific vars
$template->assign_vars(array(
   'TOTAL_POSTS'   => sprintf($user->lang[$l_total_post_s], $total_posts),
   'TOTAL_TOPICS'   => sprintf($user->lang[$l_total_topic_s], $total_topics),
   'TOTAL_USERS'   => sprintf($user->lang[$l_total_user_s], $total_users),
   'NEWEST_USER'   => sprintf($user->lang['NEWEST_USER'], get_username_string('full', $config['newest_user_id'], $config['newest_username'], $config['newest_user_colour'])),

   'LEGEND'      => $legend,

   'S_LOGIN_ACTION'         => append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=login'),
   'S_DISPLAY_BIRTHDAY_LIST'   => ($config['load_birthdays']) ? true : false,

   'U_MARK_FORUMS'      => ($user->data['is_registered'] || $config['load_anon_lastread']) ? append_sid("{$phpbb_root_path}index.$phpEx", 'hash=' . generate_link_hash('global') . '&amp;mark=forums') : '',
   'U_MCP'            => ($auth->acl_get('m_') || $auth->acl_getf_global('m_')) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=main&amp;mode=front', true, $user->session_id) : '')
);


And here is the HTML file:

Code: Select all

<!--DISPLAY ONLINE LIST-->
      <li style="text-align: center;" style="padding: 0.60cm">
         <!-- IF S_DISPLAY_ONLINE_LIST -->
   <!-- IF U_VIEWONLINE --><h3><a href="{U_VIEWONLINE}">{L_WHO_IS_ONLINE}</a></h3><!-- ELSE --><h3>{L_WHO_IS_ONLINE}</h3><!-- ENDIF -->
   {TOTAL_USERS_ONLINE} ({L_ONLINE_EXPLAIN})<br />{RECORD_USERS}<br /> <br />
   {LOGGED_IN_USER_LIST}<!-- IF LEGEND --><br /><em>{L_LEGEND}: {LEGEND}</em><!-- ENDIF --></p>
    <!-- ENDIF --></li>



<!--DISPLAY USER ONLINE-->
<li style="text-align: center;" style="padding: 0.60cm">
<!-- IF NEWEST_USER -->
   <h3>{L_STATISTICS}</h3>
   <p>{TOTAL_POSTS} &bull; {TOTAL_TOPICS} &bull; {TOTAL_USERS} &bull; {NEWEST_USER}</p>
<!-- ENDIF --></li>


You must have your HTML file in the /template/ folder and the external page in your phpBB3's root.

Thanks
Never Say Never!
bigbigfan
New member
New member
Posts: 2
Joined: 01 Jan 2013, 16:46

Re: Forum stats on external page.

Post by bigbigfan »

Awesomely simple solution but I need just a *little* more.

Total posts
Total topics
Total members
Total visits today <---- is this stored in the database as well? It is over the past 5 mins but can be set to daily, no?

...and I need it from multiple forums, e.g. I have three different forums and I need this information posted on a 4th non-forum site.
User avatar
tumba25
Supporter
Supporter
Posts: 1052
Joined: 24 Oct 2007, 13:10
Real name: Jari Kanerva
Location: Kokkola, Finland.
Contact:

Re: Forum stats on external page.

Post by tumba25 »

Todays visits is not stored in the DB. You would need to store it in the DB. It would take to much resources otherwise.
bigbigfan
New member
New member
Posts: 2
Joined: 01 Jan 2013, 16:46

Re: Forum stats on external page.

Post by bigbigfan »

That's fine, i can pull the total visits from piwik, but how about the other part of the question, how could I merge the stats from 3 different forums onto the homepage of a 4th site?

While I could probably hack it together I'm not sure about needing 3 different databases to respond, performance, etc.
User avatar
tumba25
Supporter
Supporter
Posts: 1052
Joined: 24 Oct 2007, 13:10
Real name: Jari Kanerva
Location: Kokkola, Finland.
Contact:

Re: Forum stats on external page.

Post by tumba25 »

You need to query each DB and put the result on your 4th site. You could have all 3 forums in the same DB to not have to connect to different DBs.
Post Reply