<?php
    $_id = isset($_GET['i']) ? trim($_GET['i']) : '';

    $community = mysqli_query_logged("SELECT * FROM community WHERE community_id = " . sq($_id));
    if (!$community_row = mysqli_fetch_assoc($community))    
    {
        header('Location: ./?s=communities');
        die;        
    }
    
    $community_sections = mysqli_query_logged("SELECT * FROM community_sections WHERE community_id = '" . $community_row['community_id'] . "' AND section_deleted = '0'");
    while ($community_sections_row = mysqli_fetch_assoc($community_sections))
    {
        $community_forums = mysqli_query_logged("SELECT * FROM community_forums WHERE section_id = '" . $community_sections_row['section_id'] . "' AND forum_deleted = '0'");
        while ($community_forums_row = mysqli_fetch_assoc($community_forums))
        {
            $community_threads = mysqli_query_logged("SELECT * FROM community_threads WHERE forum_id = '" . $community_forums_row['forum_id'] . "' ORDER BY thread_sticky DESC, thread_last_posted_on DESC LIMIT 0, " . COMMUNITY_THREADS_PER_PAGE * 2);
            while ($community_threads_row = mysqli_fetch_assoc($community_threads))
            {
                //if ($community_threads_row['thread_sticky'] == 0)
                //{
                    mysqli_query_logged("REPLACE INTO community_threads_pointers SET thread_id = '" . $community_threads_row['thread_id'] . "', user_id = '" . $GLOBALS['auth']['id'] . "', message_id = '" . $community_threads_row['thread_last_message_id'] . "'");
                //}
            }
        }
    }
        
    header('Location: ./?s=community&i=' . $community_row['community_id']);
    die;
?>