Skip to:
Content
Pages
Categories
Search
Top
Bottom

Getting forum page number


  • SerpentGuard1701
    Member

    @serpentguard1701

    If I know the forum ID and the topic ID, how would I get the page that the topic is on? As in, let’s say I have 10 threads per page, and I need to generate a link to the forum page that displays the 14th topic. Is there already a function for this, or will I need to use a custom query?

Viewing 12 replies - 1 through 12 (of 12 total)

  • John James Jacoby
    Keymaster

    @johnjamesjacoby

    Hmm… This is a tough one, because it involves reverse engineering the query… To the best of what I can tell there isn’t anything in the core of bbPress that delivers this functionality…

    Because topics are sorted by date, it would involve a custom query which is then divided by the number of threads you choose to display per page…

    @the developers: Do bbPress themes accommodate for a functions.php file, similar to WordPress? This is the type of situation that a plug-in is a little bit much for, but would be perfect for an add on function.


    kevinjohngallagher
    Member

    @kevinjohngallagher

    Hey JJJ,

    its my understanding that all functions, including those that have the template hardcoded into it, require a plug-in to overwrite.

    This sadly cant be kept in the template folder, nor turned on as default.

    Yes functions.php works for bbPress.


    SerpentGuard1701
    Member

    @serpentguard1701

    Well, I found this function that’s used by the rss.php file:

    function get_post_link( $post_id = 0 ) {
    $bb_post = bb_get_post( get_post_id( $post_id ) );
    $page = get_page_number( $bb_post->post_position );

    return apply_filters( 'get_post_link', get_topic_link( $bb_post->topic_id, $page ) . "#post-$bb_post->post_id", $bb_post->post_id );
    }

    Is there a forum version fo the italicized portion of this function?


    _ck_
    Participant

    @_ck_

    This is also straightforward to do, but I sure would like to know why someone would do such a thing.

    You can’t mimic the post_position trick because no positioning is tracked by bbPress for topics. bbPress shouldn’t even use post_position anyway but it does it to accelerate the alternative looking up the position by date.

    You’d have to do a mysql query that simulates the get_latest_topics but returns only topic_id’s and then find the position of the desired topic_id in the resulting array. Then you divide by number of topics per page and there’s your page number.


    SerpentGuard1701
    Member

    @serpentguard1701

    @_ck_ – The reason is, I have some breadcrumb links in the header of my edit_post.php file. It displays something like this:

    The Volturi’s Castle Forum » Forum Games » Count to 10,000 » Edit Post

    I have the topic link pointing to the exact position of the post being edited, and I want the forum link to point to the exact page of the topic as well. The code I have is this:

    <?php
    $forum_id = $bbdb->get_var("SELECT forum_id FROM $bbdb->posts WHERE post_id=" . get_post_id() . " LIMIT 1");
    $forum_name = get_forum_name($forum_id);
    /*$topic_array = $bbdb->get_var("SELECT topic_id FROM $bbdb->topics WHERE forum_id=" . $forum_id . " ORDER BY topic_time ASC");
    while ($topic_id = current($topic_array))
    if ($topic_id == get_topic_id())
    $topic_index = key($topic_array);
    $forum_page = get_page_number($topic_index);*/
    $forum_link = '<a href="' . get_forum_link($forum_id, $forum_page) . '">' . $forum_name . '</a>';
    ?>
    <a href="<?php bb_option('uri'); ?>"><?php bb_option('name'); ?></a> &raquo; <?php echo $forum_link; ?> &raquo; <a href="<?php post_link(); ?>"><?php topic_title(); ?></a> &raquo; <?php _e('Edit Post','rag'); ?>

    The commented stuff is what’s giving me trouble. When executed, it issues an error saying that $topic_array isn’t an object or array. I inserted an echo $topic_array;, and it spit out 3. The topic_id is 13, the forum_id is 6, and no matter what post I edit, the same number is echoed, so I have no idea where it’s getting 3.


    _ck_
    Participant

    @_ck_

    That’s because get_var only gets one item – the first and only.

    You want get_results which returns an array.

    Actually, even better in this case would be get_col which should return a flat array.

    Another trick to use on that query is possibly to include the topic_time as the end in the WHERE clause so that it doesn’t bother to pull up 10,000 topic id’s if the topic you are looking for is in the first 100

    global $topic;
    $query="SELECT topic_id FROM $bbdb->topics WHERE forum_id=$forum_id and topic_time<=$topic->topic_time ORDER BY topic_time DESC";
    $topic_array = $bbdb->get_col($query);

    We could take a huge shortcut and a bit of a gamble that no other topic has the same exact topic_time as this one and simply get the position via count, assuming it’s the very last one. Occasionally you might be a page short, or a page too many.

    global $topic;
    $query="SELECT count(topic_id) as position FROM $bbdb->topics WHERE forum_id=$forum_id and topic_time<=$topic->topic_time ORDER BY topic_time DESC";
    $position = $bbdb->get_var($query);

    Then you divide position by topics per page and you have the page it’s on.

    $page=ceil($position/bb_get_option('page_topics'));
    $link=get_topic_link(0,$page);


    SerpentGuard1701
    Member

    @serpentguard1701

    I tried the shortcut method, it returned an error: `bbPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '01:46:48 ORDER BY topic_time DESC' at line 1]

    SELECT count(topic_id) as position FROM tvcforum_topics WHERE forum_id=6 and topic_time<=2008-12-09 01:46:48 ORDER BY topic_time DESC`

    Regarding the last bit of code you posted – there’s actually a function already present in one of the bb-includes files that does the same thing – get_page_number().


    _ck_
    Participant

    @_ck_

    Oh that’s a simple bug, I forgot the quotes around the time:

    $query="SELECT count(topic_id) as position FROM $bbdb->topics WHERE forum_id=$forum_id and topic_time<='$topic->topic_time' ORDER BY topic_time DESC";
    $position = $bbdb->get_var($query);


    SerpentGuard1701
    Member

    @serpentguard1701

    I had to change topic_time<='$topic->topic_time' to topic_time>='$topic->topic_time', but it works beautifully now.

    I’m thinking about applying this mod to the regular topic view. You told me in a different thread that it should be fine if I’m only using this extra DB query in the Edit screen – do you think it would be unwise to use it in regular topic views?


    _ck_
    Participant

    @_ck_

    You mean on the regular topic page? Yeah it would just add one query.

    On a low to medium activity site it should be okay.

    I think there’s an index on topic_time so mysql shouldn’t have too much of a workout.

    The >= switch aroo may be logic confusion on my part between whether to use DESC or ASC.

    One makes the list start with the current topic time, the other way makes it end with the time.

    In theory the count should be easier with <= and if you have to change DESC to ASC.


    SerpentGuard1701
    Member

    @serpentguard1701

    I first tried switching DESC to ASC, and it didn’t change the result, which was 1.

Viewing 12 replies - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.