$sql = 'SELECT c.id AS cid, categoryName, f.id AS fid, f.*
FROM categories AS c
INNER JOIN forums AS f ON c.id = f.categoryID
ORDER BY c.position, c.id, f.position';
$result = $DB->query($sql);
$categories = array();
//Set the current category to 'nothing'
$category = NULL;
$totalPosts = 0;
$totalThreads = 0;
while($row = $result->fetch_assoc())
{
$cid = $row['cid'];
//New category since last iteration?
if ($category != $cid)
{
$categories[$cid]['name'] = $row['categoryName'];
$categories[$cid]['forums'] = array();
$category = $cid; //Change the current category
}
$icon = array('img' => 'none-l', 'alt' => 'No new posts');
if ($userinfo->loggedIn AND $row['lastPost'] > $userinfo->f_lastVisit)
$icon = array('img' => 'new-posts-l', 'alt' => 'New posts');
//Push the forum onto the current category
$categories[$cid]['forums'][] = array(
'id' => $row['fid'],
'name' => $row['forumName'],
'desc' => $row['forumDesc'],
'numThreads' => $row['numThreads'],
'numPosts' => $row['numPosts'],
'lastPost' => formatTime($row['lastPost']),
'lastPostID' => $row['lastPostID'],
'lastPoster' => $row['lastPoster'],
'lastPosterID' => $row['lastPosterID'],
'icon' => $icon
);
$totalPosts += $row['numPosts'];
$totalThreads += $row['numThreads'];
}
//Destroy the result and free up the memory used by it
$result->close();
$inner->set_vars(array(
'categories' => $categories,
'totalPosts' => $totalPosts,
'totalThreads' => $totalThreads
));
$row = $DB->queryFirst('SELECT count(id) AS total FROM account');
$inner->set('totalAccounts', $row->total);
$row = $DB->queryFirst('SELECT id, name FROM account ORDER BY datecreated DESC LIMIT 0, 1');
$inner->set_vars(array('latestMember' => $row->name, 'latestMemberID' => $row->id));
$totalGuests = 0;
$users = array();
$result = $DB->query('SELECT a.name, a.permissionLevel, s.aid FROM session AS s LEFT JOIN account AS a ON s.aid = a.id WHERE f_lastActivity IS NOT NULL');
while ($user = $result->fetch_assoc())
{
if ($user['aid'] != 0)
{
if ($user['permissionLevel'] >= 3)
$users[] = ''.$user['name'].'';
elseif ($user['permissionLevel'] == 2)
$users[] = ''.$user['name'].'';
elseif ($user['permissionLevel'] == 1)
$users[] = ''.$user['name'].'';
else
$users[] = ''.$user['name'].'';
}
else
++$totalGuests;
}
$inner->set_vars(array(
'totalGuests' => $totalGuests,
'totalUsers' => sizeof($users),
'totalOnline' => $totalGuests+sizeof($users),
'users' => implode(", ", $users)
));
////////////////////////////////////////////////////////////////
$outer->set('pageTitle', 'Forum Index');
$outer->set('body', $inner->fetch('forumindex.tpl'));