我正在尝试直接从phpbb3数据库中检索特定用户的未读帖子总数。我正在使用ezsql让我的生活更轻松,我根据以下帖子编写了以下代码:https://www.phpbb.com/community/viewtopic.php?f=46&t=2107403#p12881167
$ unreadposts似乎总是返回一个高于实际未读帖子数的数字。我在过去的12个小时里一直在研究这个问题,它撕裂了我的头发:)
非常感谢任何帮助或建议!
// Step 1: get all topics the user has access to. Assuming all of them are unread until we prove otherwise
$usertopicsallowed = $forumdb->get_results("SELECT DISTINCT t.topic_id, t.forum_id, t.topic_last_post_time FROM $forumdbname.phpbb_users u
INNER JOIN $forumdbname.phpbb_user_group ug ON u.user_id = ug.user_id
INNER JOIN $forumdbname.phpbb_groups g ON g.group_id = ug.group_id
INNER JOIN $forumdbname.phpbb_acl_groups acl ON acl.group_id = ug.group_id
INNER JOIN $forumdbname.phpbb_forums f ON f.forum_id = acl.forum_id
INNER JOIN $forumdbname.phpbb_topics t ON f.forum_id = t.forum_id
WHERE u.user_id = " . $forumuserid . ";");
$usertopicsallowedcnt = count($usertopicsallowed);
// Step 2: Return any topics for this user in topics_track
$usertopicstrack = $forumdb->get_results("SELECT topic_id, mark_time FROM phpbb_topics_track WHERE user_id = " . $forumuserid . " ;");
if (!empty($usertopicsallowed))
{
foreach($usertopicsallowed as $key => $usertopicallowed)
{
if (!empty($usertopicstrack))
{
foreach($usertopicstrack as $key2 => $usertopictrack)
{
if ($usertopicsallowed[$key]->topic_id == $usertopicstrack[$key2]->topic_id)
{
if ($usertopicsallowed[$key]->topic_last_post_time < $usertopicstrack[$key2]->mark_time)
{
unset($usertopicsallowed[$key]);
}
}
}
}
}
}
$usertopicsallowed2 = array_values($usertopicsallowed);
$usertopicsallowed2cnt = count($usertopicsallowed2);
// Step 3a: eturn any topics for this user in forums_track
$userforumstrack = $forumdb->get_results("SELECT forum_id, mark_time FROM phpbb_forums_track WHERE user_id = " . $forumuserid . ";");
// Step 3b: remove all topics before the forum tracks lastmark time
if (!empty($usertopicsallowed2))
{
foreach($usertopicsallowed2 as $key => $usertopicsallow2)
{
if (!empty($userforumstrack))
{
foreach($userforumstrack as $key2 => $userforumtrack)
{
if ($usertopicsallowed2[$key]->forum_id == $userforumstrack[$key2]->forum_id)
{
if ($usertopicsallowed2[$key]->topic_last_post_time < $userforumstrack[$key2]->mark_time)
{
unset($usertopicsallowed2[$key]);
}
}
}
}
}
}
$usertopicsallowed3 = array_values($usertopicsallowed2);
$usertopicsallowed3cnt = count($usertopicsallowed3);
// Step 4: remove all topics before the user's lastmark time
if (!empty($usertopicsallowed3))
{
foreach($usertopicsallowed3 as $key => $usertopicsallow3)
{
if ($usertopicsallowed3[$key]->topic_last_post_time < $forumuserlastmark)
{
unset($usertopicsallowed3[$key]);
}
}
}
$usertopicsallowed4 = array_values($usertopicsallowed3);
$usertopicsallowed4cnt = count($usertopicsallowed4);
$unreadposts = count($usertopicsallowed4);
答案 0 :(得分:1)
好的,正如我在上面的评论中所说的那样,经过12个小时未能将其投入使用后,我在发布后的15分钟内找到答案!注意到这个回复所以也许将来会帮助其他人,我也会在链接上回复。
此链接提供答案(https://www.phpbb.com/community/viewtopic.php?f=46&t=2092813#p12800435)但不允许权限,因此您需要添加代码我还在下面写了。因此,完整的代码是:
// Step 1: get all topics the user has access to. Assuing all of them are unread until we prove otherwise
$usertopicsallowed = $forumdb->get_results("SELECT DISTINCT t.topic_id, t.forum_id, t.topic_last_post_time FROM $forumdbname.phpbb_users u
INNER JOIN $forumdbname.phpbb_user_group ug ON u.user_id = ug.user_id
INNER JOIN $forumdbname.phpbb_groups g ON g.group_id = ug.group_id
INNER JOIN $forumdbname.phpbb_acl_groups acl ON acl.group_id = ug.group_id
INNER JOIN $forumdbname.phpbb_forums f ON f.forum_id = acl.forum_id
INNER JOIN $forumdbname.phpbb_topics t ON f.forum_id = t.forum_id
WHERE u.user_id = " . $forumuserid . ";");
// Step 2: Calculate unread posts in all forums (regardless of permissions)
$phpbbv2 = $forumdb->get_results("SELECT t.topic_id, t.topic_last_post_time, tt.mark_time as topic_mark_time, ft.mark_time as forum_mark_time FROM (phpbb_topics t)
LEFT JOIN phpbb_topics_track tt ON (tt.user_id = " . $forumuserid . " AND t.topic_id = tt.topic_id)
LEFT JOIN phpbb_forums_track ft ON (ft.user_id = " . $forumuserid . " AND t.forum_id = ft.forum_id)
WHERE ( (tt.mark_time IS NOT NULL AND t.topic_last_post_time > tt.mark_time)
OR (tt.mark_time IS NULL AND ft.mark_time IS NOT NULL AND t.topic_last_post_time > ft.mark_time)
OR (tt.mark_time IS NULL AND ft.mark_time IS NULL AND t.topic_last_post_time > " . $forumuserlastmark . ") ) AND t.topic_moved_id = 0 AND t.topic_approved = 1 ORDER BY t.topic_last_post_time DESC;");
// Step 3: Loop through step 2 and only increment the counter for every topic the user has permission to view
$unreadcounter=0;
if (!empty($phpbbv2))
{
foreach($phpbbv2 as $key => $phpbbv2a)
{
if (!empty($usertopicsallowed))
{
foreach($usertopicsallowed as $key2 => $usertopicallowed)
{
if ($phpbbv2[$key]->topic_id == $usertopicsallowed[$key2]->topic_id)
{
$unreadcounter++;
}
}
}
}
}
$unreadpost=$unreadcounter;
我确信它可能会更好,我不是最有经验的PHP,但它为我工作。