Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 13th, 2002, 06:17 AM
supudo supudo is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Location: Sofia, Bulgaria
Posts: 3 supudo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to supudo
"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) ) {
        return 
false;
    }
    
$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;
        }
    }
    return 
$newarray;


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.

Reply With Quote
  #2  
Old April 28th, 2002, 11:57 AM
csaba csaba is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: NYC
Posts: 79 csaba User rank is Private First Class (20 - 50 Reputation Level)csaba User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
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.

Csaba
Comments on this post
JimmyGosling agrees!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > "forum-messages" sorting


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway