PDA

View Full Version : Displaying VB Variables in external page - particularly forum stats


eviljoker7075
01-22-2013, 05:51 PM
Hi all, I am attempting to create a php file that displays my forum stats, to use as an include in a wordpress themplate.

So far I have the following code, saved as a file in my forum root directory:

<?php

require_once('./global.php');

echo {vb:raw numbermembers};

?>

I know it's the echo line that's causing a problem, the thing is I'm new to php so don't really know how this should be formatted.


Can anyone help?

kh99
01-22-2013, 05:56 PM
The {vb:raw numbermembers} is something that can only be used in vbulletin templates. You could do something like echo $numbermembers, but you need to calculate $numbermembers first. If you look in forum.php and search for "BOARD STATISTICS" you can find the section that calculates it.

Also, for that code to work you would need to get the 'userstats' loaded from the datastore, so you'd need
$specialtemplates = array('userstats');


before you include global.php.

Edit: now that I look at it more, there isn't much of a calculation for numbermembers, it just formats the number.

eviljoker7075
01-22-2013, 06:19 PM
What an incredibly helpful answer, thanks you! So I have the following working code, that gives me the total number of members:

<?php
$specialtemplates = array('userstats');
require_once('./global.php');


$numbermembers = vb_number_format($vbulletin->userstats['numbermembers']);
echo $numbermembers;


?>


However, I also want to add in the total number of posts and threads, so I tried the below:

<?php
$specialtemplates = array('userstats');
$specialtemplates = array('forumcache');
require_once('./global.php');


$numbermembers = vb_number_format($vbulletin->userstats['numbermembers']);
echo $numbermembers;




$totalthreads = 0;
$totalposts = 0;

if (is_array($vbulletin->forumcache)){
foreach ($vbulletin->forumcache AS $forum)

{
$totalthreads += $forum['threadcount'];
$totalposts += $forum['replycount'];
}
}

$totalthreads = vb_number_format($totalthreads);
$totalposts = vb_number_format($totalposts);

echo $totalthreads;
echo $totalposts;



?>

This doesn't work, and gives me '0' for each of the echos. I'm guessing the issue is that I'm not calling in the specialtemplates correctly now that there's more than one - how should these be formatted?

Thanks

kh99
01-22-2013, 08:48 PM
The forum info always gets loaded, so you don't have to include that in $specialtemplates (in fact you really want to remove that second $specialtemplates line because it's overwriting what the first one does). But the forumcache doesn't include the thread and post count info. There's another function that adds that info, called cache_ordered_forums(). So if you add a call to that function like:
cache_ordered_forums(1, 1);


after global.php is included and before you do the calculations, then it should work.

eviljoker7075
01-23-2013, 06:09 AM
Perfect! Thank you very much. For anyone also wishing to do the same,

HERE IS THE FINAL WORKING CODE:

File: 'forumStats.php' - saved in the root directory of my forum
<?php
$specialtemplates = array('userstats');
require_once('./global.php');
cache_ordered_forums(1, 1);


$numbermembers = vb_number_format($vbulletin->userstats['numbermembers']);
echo 'Members: ';
echo $numbermembers;


$totalthreads = 0;
$totalposts = 0;

if (is_array($vbulletin->forumcache)){
foreach ($vbulletin->forumcache AS $forum)

{
$totalthreads += $forum['threadcount'];
$totalposts += $forum['replycount'];
}
}

$totalthreads = vb_number_format($totalthreads);
$totalposts = vb_number_format($totalposts);

echo ' Thread: ';
echo $totalthreads;

echo ' Posts: ';
echo $totalposts;

?>


However (there's always a but, right), when I add this as an include on my website homepage (running wordpress) I get an error. I have placed the following line into my template:
<?php include('./forum/forumStats.php'); ?>

And on the page I get this error:
Warning: require_once(./global.php) [function.require-once]: failed to open stream: No such file or directory in /home/sitedir/public_html/forum/forumStats.php on line 3

I am assuming that it's looking for global.php in the root of my site, rather than in the forum directory - but is there a way round this?

kh99
01-23-2013, 12:32 PM
You're right. Try this for the beginning of the code:

<?php
$specialtemplates = array('userstats');
chdir('./forum');
require_once('./global.php');
cache_ordered_forums(1, 1);



If you wanted you could chdir() to the absolute path instead. And also you could save the current directory and chdir back after including gobal.php, but if that's the entire code then it doesn't look like it's necessary.

eviljoker7075
01-23-2013, 04:51 PM
That did the trick nicely! Thank you so much for your help, I've learnt a lot.