MyBB Community Forums

Full Version: Easy MyBB Stats on Your Website
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I'm not using the portal but I wanted away to include some statistics about my forums on the front page of my site. I decided to write some simple code that would do this for me and share it with all of you. I'm sure many of you already know how to do this but I figured I would post it for everyone to use. I'm thinking of adding more to it in the future so if I do I'll post the updated code here for all to use.

<?php
// Host
    $DBhost="localhost";

// Database User Name
    $DBuser="FILL-ME-IN";

// Database Password
    $DBpass="FILL-ME-IN";

// Database Name
    $DBname="FILL-ME-IN";

$connect = mysql_connect($DBhost, $DBuser, $DBpass);
mysql_select_db($DBname,$connect) or die ("Could not select database");
mysql_query($connect);

$topic_result = mysql_query("SELECT COUNT(*) as count FROM mybb_threads");
$topic_count = mysql_fetch_array($topic_result);
$topic_count = $topic_count['count'];
$post_result = mysql_query("SELECT COUNT(*) as count FROM mybb_posts");
$post_count = mysql_fetch_array($post_result);
$post_count = $post_count['count'];
$user_result = mysql_query("SELECT COUNT(*) as count FROM mybb_users");
$user_count = mysql_fetch_array($user_result);
$user_count = $user_count['count'];
$forum_result = mysql_query("SELECT COUNT(*) as count FROM mybb_forums");
$forum_count = mysql_fetch_array($forum_result);
$forum_count = $forum_count['count'];

echo $topic_count." Threads";
echo "<br>";
echo $post_count." Posts";
echo "<br>";
echo $user_count." Users";
echo "<br>";
echo $forum_count." Boards";

// this will add the last 3 threads started on your forums

$sql = mysql_query("SELECT * FROM mybb_threads ORDER BY dateline DESC LIMIT 3");
while ($row = mysql_fetch_array($sql)) {
		   $tid = $row['tid'];
		   $fid = $row['fid'];
		   $subject = $row['subject'];
		   $replies = $row['replies'];
		   $views = $row['views'];
$sql_query2 = "SELECT * FROM mybb_forums WHERE fid='$fid'";
$results = mysql_query($sql_query2);
$row = mysql_fetch_array($results);
echo "<a href='forums/showthread.php?tid=".$tid."&action=lastpost'><strong>".$subject."</strong></a><br><a href='forums/forumdisplay.php?fid=".$fid."'>".$row[name]."</a><br>".$replies." replies, ".$views." views<br><hr>";
}
?>

All you have to do is modify the database information (where it says FILL-ME-IN) and include this code anywhere you want the statistics to be displayed. If you want to format the results to look a different way but have problems doing this just let me know and I'll help you out. Right now the only statistics being displayed are total threads, posts, users, and boards. Enjoy!

EDIT:
Code optimized for efficiency: Thanks DennisTT
Screen shots please
Here's the code more optimized for efficiency:

<?php
// Host
	$DBhost="localhost";

// Database User Name
	$DBuser="FILL-ME-IN";

// Database Password
	$DBpass="FILL-ME-IN";

// Database Name
	$DBname="FILL-ME-IN";

$connect = mysql_connect($DBhost, $DBuser, $DBpass);
mysql_select_db($DBname,$connect) or die ("Could not select database");
mysql_query($connect);

$topic_result = mysql_query("SELECT COUNT(*) as count FROM mybb_threads");
$topic_count = mysql_fetch_array($topic_result);
$topic_count = $topic_count['count'];
$post_result = mysql_query("SELECT COUNT(*) as count FROM mybb_posts");
$post_count = mysql_fetch_array($post_result);
$post_count = $post_count['count'];
$user_result = mysql_query("SELECT COUNT(*) as count FROM mybb_users");
$user_count = mysql_fetch_array($user_result);
$user_count = $user_count['count'];
$forum_result = mysql_query("SELECT COUNT(*) as count FROM mybb_forums");
$forum_count = mysql_fetch_array($forum_result);
$forum_count = $forum_count['count']

echo $topic_count." Threads";
echo "<br>";
echo $post_count." Posts";
echo "<br>";
echo $user_count." Users";
echo "<br>";
echo $forum_count." Boards";

?>

And @lapsetur: the result would be like this:

Quote:203 Threads
4643 Posts
244 Users
5 Boards
Thanks DennisTT, I've updated the code with your modifications.
Nice work dennis
You can view my code at http://www.zerovision.net/ on the left navigation if you want to see it put to use on a website.
Nice, I'll be using this. Smile
Strange stats show if using FireFox but do not show with IE7
Their shouldnt be a difference with browsers. PHP is not something that only works with Firefox, or IE, or Netscape, ect... Its globally supported.
PeterT I'm guessing you are using CSS with the code. I'm sure if you check your html/CSS you will find a solution. I'm getting ready to add latest threads and latest posts as well so check back for the updated code. Smile
Pages: 1 2