phpBB Next topic and Previous topic function

Discussion in 'PHP' started by stevenswing, Oct 16, 2009.

  1. #1
    Dear Guys,

    We are running an old, very heavily modified version pre 3.0 version of PHPBB wrapped in other site code. Please refrain from "Upgrade to 3.0" comments, this is not an option at the moment. :)

    Currently, the "View previous topic" and "View next topic " links append a "&view=next" parameter to the URL. For example:
    $view_prev_topic_url = append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . "=$topic_id&view=previous");
    $view_next_topic_url = append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . "=$topic_id&view=next");
    Code (markup):
    The code that seems to get the post is this:
            else if ( $HTTP_GET_VARS['view'] == 'next' || $HTTP_GET_VARS['view'] == 'previous' )
            {
                    $sql_condition = ( $HTTP_GET_VARS['view'] == 'next' ) ? '>' : '<';
                    $sql_ordering = ( $HTTP_GET_VARS['view'] == 'next' ) ? 'ASC' : 'DESC';
    
                    $sql = "SELECT t.topic_id
                            FROM " . TOPICS_TABLE . " t, " . TOPICS_TABLE . " t2, " . POSTS_TABLE . " p, " . POSTS_TABLE . " p2
                            WHERE t2.topic_id = $topic_id
                                    AND p2.post_id = t2.topic_last_post_id
                                    AND t.forum_id = t2.forum_id
                                    AND p.post_id = t.topic_last_post_id
                                    AND p.post_time $sql_condition p2.post_time
                                    AND p.topic_id = t.topic_id
                            ORDER BY p.post_time $sql_ordering
                            LIMIT 1";
                    if ( !($result = $db->sql_query($sql)) )
                    {
                            message_die(GENERAL_ERROR, "Could not obtain newer/older topic information", '', __LINE__, __FILE__, $sql);
                    }
    
                    if ( $row = $db->sql_fetchrow($result) )
                    {
                            $topic_id = intval($row['topic_id']);
                    }
                    else
                    {
                            $message = ( $HTTP_GET_VARS['view'] == 'next' ) ? 'No_newer_topics' : 'No_older_topics';
                            message_die(GENERAL_MESSAGE, $message);
                    }
            }
    Code (markup):
    Basically, instead of this we would like to simply include the full URL to the next and previous topic without paramaters. So the function would lookup the next or previous topic and simply return the post number. I'm hoping that I could modify and use this code above into a function and call it from the first bit of code I posted.

    Any ideas? Thank you for your help.

    Best Regards,

    Steven
     
    stevenswing, Oct 16, 2009 IP
  2. superdav42

    superdav42 Active Member

    Messages:
    125
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #2
    It shouldn't be too hard just write two functions something like this:
    
    next_topic_id($topic_id) {
    {
        
                    $sql_condition = '>'
                    $sql_ordering =  'ASC';
    
                    $sql = "SELECT t.topic_id
                            FROM " . TOPICS_TABLE . " t, " . TOPICS_TABLE . " t2, " . POSTS_TABLE . " p, " . POSTS_TABLE . " p2
                            WHERE t2.topic_id = $topic_id
                                    AND p2.post_id = t2.topic_last_post_id
                                    AND t.forum_id = t2.forum_id
                                    AND p.post_id = t.topic_last_post_id
                                    AND p.post_time $sql_condition p2.post_time
                                    AND p.topic_id = t.topic_id
                            ORDER BY p.post_time $sql_ordering
                            LIMIT 1";
                    if ( !($result = $db->sql_query($sql)) )
                    {
                           return false;
                    }
    
                    if ( $row = $db->sql_fetchrow($result) )
                    {
                            $topic_id = intval($row['topic_id']);
                    }
                    else
                    {
                            return false;
                    }
            
    }
    prev_topic_id($topic_id) {
    {
        
                    $sql_condition = '<'
                    $sql_ordering =  'DESC';
    
                    $sql = "SELECT t.topic_id
                            FROM " . TOPICS_TABLE . " t, " . TOPICS_TABLE . " t2, " . POSTS_TABLE . " p, " . POSTS_TABLE . " p2
                            WHERE t2.topic_id = $topic_id
                                    AND p2.post_id = t2.topic_last_post_id
                                    AND t.forum_id = t2.forum_id
                                    AND p.post_id = t.topic_last_post_id
                                    AND p.post_time $sql_condition p2.post_time
                                    AND p.topic_id = t.topic_id
                            ORDER BY p.post_time $sql_ordering
                            LIMIT 1";
                    if ( !($result = $db->sql_query($sql)) )
                    {
                           return false;
                    }
    
                    if ( $row = $db->sql_fetchrow($result) )
                    {
                            $topic_id = intval($row['topic_id']);
                    }
                    else
                    {
                            return false;
                    }
            
    }
    
    
    PHP:
    Then use it like:
    
    $view_prev_topic_url = append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . "=".prev_topic_id($topic_id));
    $view_next_topic_url = append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . "=".next_topic_id($topic_id));
    
    PHP:
     
    superdav42, Oct 16, 2009 IP