MyBB Community Forums

Full Version: MyBB database
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone, first post here  Smile  Not sure in which section to post, so I am leaving my question here.

I am working on a project, using the MyBB forum platform. I need certain information regarding the posts of the different forums (categories), such as total amount of posts (threads + replies) in the forum and total views of the posts. For the amount of posts per forum, I am using the code below.

My question is how to get the views data (to sum up all of the views from all the threads in the forum)? Can't see "view" field in any of the tables of the MyBB database.

$pid = 10; //pid of the forum of interest
$data1 = 0; //just variable for accumulating the amount of posts

//getting the posts from the forum
$query = $db->query("SELECT posts FROM mybb_forums WHERE fid='".$pid."'");
	
while($result = $db->fetch_array($query))
{
	$post1 += $result['posts'];
}

//getting the posts from the sub-forums	
$query = $db->query("SELECT posts FROM mybb_forums WHERE pid='".$pid."'");

while($result = $db->fetch_array($query))
{
	$post1 += $result['posts'];
}

It is a bit of a work around, so that I get the total amount of posts (threads + replies) from the current forum and all it's sub-forums. I know it's ugly written but it's working, but I would still accept if some of you has a better solution Smile  Don't judge me too hard, it is my first project with coding for the web.
The table mybb_threadsread is what you want because it does a unique entry for each user + thread combination. If uniqueness doesn't matter, you can use mybb_threads and there is a column called views.

If you aren't using uniqueness, this would be much easier on your server to run:
$views = $replies = 0;
$query = $db->query("SELECT views, replies FROM " . TABLE_PREFIX . "threads WHERE fid IN(" . $fids . ")");
while($thread = $db->fetch_array($query))
{
$views += $thread['views'];
$replies += $thread['replies'] +1;
}