MyBB Community Forums

Full Version: Count how many threads use a specific prefix
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

Let's say I have a prefix called "Videogames" and I want to display on my homepage how many threads use the prefix "videogames".
How would I approach this? Is it possible by MyBB?

Kind regards
You will need some kind of plugin or core modification. I personally would use the Hooks plugin to hook at index_end and cache the value for some minutes.

Untested but around the lines of the following:
function index_end()
{
	global $db, $cache, $videogames_count;

	$cached_data = $cache->read('videogames_count');

	if(empty($cached_data['time']) || $cached_data['time'] > TIME_NOW - 300) // 5 minutes cache
	{
		$cached_data['time'] = TIME_NOW;

		$query = $db->simple_select('threads', 'COUNT(tid) as total_threads', "prefix='1'"); // this doesn't account for permissions, visibility, etc

		$cached_data['threads'] = (int)$db->fetch_field($query, 'total_threads');

		$cache->update('videogames_count', $cached_data);
	}

	$videogames_count = my_number_format($cached_data['threads']);
	// now use {$videogames_count} in your index template.
}
(2020-09-29, 08:19 PM)OmarĀ G. Wrote: [ -> ]You will need some kind of plugin or core modification. I personally would use the Hooks plugin to hook at index_end and cache the value for some minutes.

Untested but around the lines of the following:
function index_end()
{
	global $db, $cache, $videogames_count;

	$cached_data = $cache->read('videogames_count');

	if(empty($cached_data['time']) || $cached_data['time'] > TIME_NOW - 300) // 5 minutes cache
	{
		$cached_data['time'] = TIME_NOW;

		$query = $db->simple_select('threads', 'COUNT(tid) as total_threads', "prefix='1'"); // this doesn't account for permissions, visibility, etc

		$cached_data['threads'] = (int)$db->fetch_field($query, 'total_threads');

		$cache->update('videogames_count', $cached_data);
	}

	$videogames_count = my_number_format($cached_data['threads']);
	// now use {$videogames_count} in your index template.
}

Thank you, this gave me some good insight and now I managed to make it work.