我正在尝试直接从 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);