1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Sofia, Bulgaria
    Rep Power

    "forum-messages" sorting

    it may sound a little stupid or easy to be achieved, but it's a major problem to me (maybe i'm stupid).
    suppose i want to make a forum. like forums in the world anyone can post new topics, and everybody can reply to them. the replies must be sorted in a tree-like structure visually. for example www.phorum.org has such a structre. everything i could achieve is - i set 3 variables to each message which characterize it - id, thread and parent. id is standart unique id (as you make it in mysql), thread value tells me that certain message belongs to certian message (to sertain main topic, "conversation") - for example i post new topic which takes in mysql id = 1. so every reply to that messgae has a thread = 1.
    with parent i can see which is the "parent", the "father" of a certain message - for example : i have e topic with id - 1. i have a reply to this topic with id 2, AND thread = 1, AND parent = 1. but i have a third reply with id=3 to message with id=2, so it will has thread 1 - because it belongs to the thread of message with id=1, but will have parent=2, because is a reply to message with id=2.
    so my question is - how can i sort all the messages in tree-like structure (see the phorum.org link above)?
    i found a function which sortes 2 dimensional arrays by 1 dimension. it works fine with simple integers :
    PHP Code:
    // recursive function for merging two arrays
    function array_merge_clobber($a1$a2) {
        if ( !
    is_array($a1) || !is_array($a2) ) {
    $newarray $a1;
        while ( list(
    $key$val) = each($a2) ) {
            if ( 
    is_array($val) && is_array($newarray[$key]) ) {
    $newarray[$key] = array_merge_clobber($newarray[$key], $val);
            else {
    $newarray[$key] = $val;

    it works fine when i use only these tree variables - id, thread and parent. but when adding and message specific variables like title, author, and message_body the arrya becomes 4 or 5 or more dimensional and bugs appear.
    also the existing php function array_merge_recursive can help a little here, but with some pretty tricky loops and after all again bugs appear and the final result is not what i look for.
    ahy help would be HIGHLY appreciated.
    also, because i think i can't explain the problem very good ... some questions about the problem is also appreciated - maybe they will help me ... who knows :)
    10x in advance

    Comments on this post

    • JimmyGosling agrees
    Last edited by supudo; April 13th, 2002 at 06:19 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Rep Power
    Perhaps I don't understand what you are asking, but what I do in this case is to have an array ($aPosts) of records, each record corresponding to a message. Each record is itself an array:
    [id, thread, parent, sibling, title, author, ...]
    Next, I have an array ($aIndex) which indexes into $aPosts. Thus, $aIndex starts out as [0, 1, 2, ...numberOfPosts-1]. This way, there is no worry about recursive merges, etc.

    Sorting is also straightforward because I never touch $aPosts, only $aIndex. In particular, even multisorts are easy, because I first sort $aIndex on the least important index, then the next most important one, up to the most important.


    Comments on this post

    • JimmyGosling agrees

IMN logo majestic logo threadwatch logo seochat tools logo