MyBB Community Forums

Full Version: Return all fids from mybb_forums table if the setting is set to "0"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm working on a little plugin that has a setting to set which forums the plugin should be executed on. You simply enter the fids of those forums, separated by a comma, in the setting and it works great. But I am wondering how I could return all the fids from the mybb_forums table if the admin sets the setting to "0". I've indicated that in the description, but I'm not being able to get it to work. Can someone please have a look at my code?

global $mybb, $db, $fid;

if ($mybb->settings['example_forums'] != "0")
{
	$affected_forums = explode(",",$mybb->settings['example_forums']);
}
else
{
	$query = $db->query("SELECT fid FROM ".TABLE_PREFIX."forums");
	while($fetch_fids = $db->fetch_array($query))
	{
		$fetch_fids['fid'] .= ",";
		$affected_forums = $fetch_fids['fid'];
		$affected_forums = explode(",",$affected_forums);
	}
}

if (in_array($fid, $affected_forums))
{
	// Run plugin code
}

That unfortunately doesn't work. I believe the problem is within the while statement, because I don't fully understand them yet and I'm probably making a very silly mistake. Blush Is what I'm doing even a good idea? (i.e. run a query to get the fids from the database) If not, is there another way?
Try this;
global $mybb, $db, $fid;

if ($mybb->settings['example_forums'] != "0")
{
    $affected_forums = explode(",",$mybb->settings['example_forums']);
}
else
{
	$query = $db->query("SELECT fid FROM ".TABLE_PREFIX."forums");
	$comma = '';
	while($fetch_fids = $db->fetch_field($query, "fid"))
	{
		$affected_forum = $fetch_fids['fid'];
		$comma = ',';
		$affected_forums .= $affected_forum.$comma;
	}
}

if (in_array($fid, $affected_forums))
{
    // Run plugin code
}
Yaldaram's method will work, but you'll then need to explode it. Life is much simpler using the following:

global $mybb, $db, $fid;

if ($mybb->settings['example_forums'] != "0")
{
    $affected_forums = explode(",",$mybb->settings['example_forums']);
}
else
{
    $query = $db->simple_select('forums', 'fid');
    while($fetch_fids = $db->fetch_array($query))
    {
		$affected_forums[] = $fetch_fids['fid'];
    }
}

if (in_array($fid, $affected_forums))
{
    // Run plugin code
} 

I also swapped $db->query for simple_select just because it's there and should be used IMO
if ($mybb->settings['example_forums'] != "0")
{
    $affected_forums = explode(",",$mybb->settings['example_forums']);
}

if(!$affected_forums || in_array($fid, $affected_forums))
{
    // run plugin code
}

In other words, don't fetch a list of fids if you don't need one.

Besides the forums are cached so you simply don't use the database for that.
Thanks for the help guys! I'm using frostschutz's suggestion as it's a lot simpler. Smile