Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8898809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:42:23+00:00 2026-06-15T00:42:23+00:00

I’m trying to retrieve a specific users unread posts total from a phpbb3 database

  • 0

I’m trying to retrieve a specific users unread posts total from a phpbb3 database directly. I’m using ezsql to make my life easier, and I’ve written the following code based on the following post: https://www.phpbb.com/community/viewtopic.php?f=46&t=2107403#p12881167

$unreadposts always seems to return a number thats higher than the actual number of unread posts. I’ve been working on this the last 12 hours and its tearing my hair out 🙂

Any help or suggestions would be most appreciated!

        // 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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:42:24+00:00Added an answer on June 15, 2026 at 12:42 am

    OK so as I said in my comment above, after 12 hours failing to get this to work I found an answer within 15 minutes of posting here! Noting this reply so maybe it will help someone else in the future, and I’ll also reply on the link as well.

    This link gives the answer (https://www.phpbb.com/community/viewtopic.php?f=46&t=2092813#p12800435) but doesn’t allow for permissions, so you need to add the code I wrote below in addition. The full code is therefore:

    // 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;
    

    I’m sure it could be better, I’m not the most experienced in php but its working for me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a bunch of posts stored in text files formatted in yaml/textile (from
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.