Topic: Forums moderators

How are the moderators' IDs stored for each forum?
Let's say I want to retrieve the moderators for forum with ID 10, where should I look at?

TIA.

2

Re: Forums moderators

They're stored in serialized form in the moderators column in the forums table.
The code in index.php to unserialize is:

$mods_array = unserialize($cur_forum['moderators']);
while (list($mod_username, $mod_id) = @each($mods_array))

Re: Forums moderators

Thanks svl.

Re: Forums moderators

I have problem now.

I'm trying to put the query into a function
so I have

$fid = some number
$result = $db->query('SELECT moderators FROM '.$db->prefix.'forums WHERE id = '.$fid) or die();
if($db->num_rows($result))
{
    while ($cur_forum = $db->fetch_assoc($result))
    {
        $mods_array = unserialize($cur_forum['moderators']);
        $moderators = array();

        while (list($mod_username) = @each($mods_array))
            $moderators[] = pun_htmlspecialchars($mod_username);

        $moderators = '<p>'.implode(', ', $moderators).'</p>'."\n";
    }
    echo $moderators;
}

And it works

but if I try to make it a function, like this:

function get_mods($fid)
{
    $result = $db->query('SELECT moderators FROM '.$db->prefix.'forums WHERE id = '.$fid) or die();
    if($db->num_rows($result))
    {
        while ($cur_forum = $db->fetch_assoc($result))
        {
            $mods_array = unserialize($cur_forum['moderators']);
            $moderators = array();

            while (list($mod_username) = @each($mods_array))
                $moderators[] = pun_htmlspecialchars($mod_username);

            $moderators = '<p>'.implode(', ', $moderators).'</p>'."\n";
        }
        echo $moderators;
    }
}

get_mods("some number");

I get this error

Fatal error: Call to a member function query() on a non-object in... etc.

Any hint?

5 (edited by svl 2008-01-18 23:33)

Re: Forums moderators

You need to learn about variable scoping in PHP. A variable like $db declared outside a function isn't available (by default) inside the function.

The fix is to do this:

function get_mods($fid)
{
    $db = $GLOBALS['db'];
    $result = $db->query('SELECT moderators FROM '.$db->prefix.'forums WHERE id = '.$fid) or die();

6

Re: Forums moderators

To keep it in sync with the rest of the PunBB code, that line:

$db = $GLOBALS['db'];

would be:

global $db;

Re: Forums moderators

Sweet, thanks a lot both of you.