2006-02-22, 08:03 AM
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.
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
<?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