OK thats not a back pack. You carry it like a briefcase or use the shoulder strap. *You* try strapping an ATX case with power supply and hard drives to your back. hahaha. (OK, that was really la...
Hey, that was too hard for me to understand, could you please express your logic in a C++ program?
Just kidding.
Anyway, wow, Dean! Great job, man. You came very very close to figuring out most of it. Your logic would work fine, but that's just not how I implemented it. I don't have an "Indent" column in my comments table. But you hit the nail on the head with the -1 parent_id for the comments replied to the story. You have everything exactly right up the actual array/indent part.
I'll give you a hint. "recursive"
Any more ideas?
both of you STOP!!! i cant take all this computer jargon
ok hmm I gave you the linear "hack" solution. Now here's how I'd do it in Java using objects and recursion.
**************************************************************************
Have a class called Node that contains all comment specific information plus an Enumeration of Node objects (children Nodes). Initially, go through all elements of the table, creating new Node objects with comment specific information, and *initially empty* Child Node List. For simplicity's sake, we have a method called void addNode(Node x) which adds the specified Node to *this* Node's child List. Put these newly created Nodes into a hash table, with hash keyed on comment number.
Oh, I almost forgot. Create a root Node with comment id = -1.
**************************************************************************
One could populate the Node Tree by going through the hash linearly (1,2,3...) and for each Node found, get a reference to its parent from the hash into a variable named "parent", and calling parent.addNode(this); This will give you a Comment node tree.
**************************************************************************
The tree is then printed recursively. printNodeAndChildren(Node rootNode) is called. It (1) prints current Node's info. (2a)(if children exist) for each child x (in Enumeration) calls printNodeAndChildren(x) and returns. (2b)(no children) just returns.
**************************************************************************
I could do it another way if I do incremental "Selects" to the database, instead of my one "Select * from comment" at the very beginning.
Do you query the database multiple times during the algorithm, or just once at the beginning like I did?
If I had multiple queries, I could forget about hashtables and Node Objects I think. This is how I'd do it that way:
have a function called printComments(int p_id), and call printComments(-1) in your page creation code.
STEP 1) Recordset recs = EXECUTE("Select * from comment where parent_id="+p_id);
STEP 2) For each record in recs:
STEP 2a)Print rec info (comment, date, etc.)
STEP 2b)call printComments(thisrec.comment_id) //pass this record's comment number as the parent_id
STEP 3)If no records, then this comment has no children (actually no comments in the database have this comment as its parent) so just return.
Basically, the difference between my two ideas is database retrieval time verses computation time. One is database-intensive, and the other is computationally-intensive.
You got it, dean. I went the database-intensive route. I'm not exactly sure which is the best way, but for now, it was the easiest way for me to accomplish the task in ColdFusion, which is not object oriented. The recursive call also keeps track of how many times it has been called for a comment, and this is how I output the indent level.
Guys...i know ian is definitely a certified genius, and dean...well, he's proving himself to me as well. But all this java junk doesn't get you nowhere. to be really smart, you have to know calculus and be able to apply it to computer programming, by intergrating the recursive variable functions into to the nodes within the class, then differentiate the entire function into terms of f'(x), thereby being able to graph your results...if you continue to differentiate, you will eventually find that f''(x) gives you your points of inflection, where you can apply the rest of the nodes of the class...whoopdeedooo i don't even know what i'm talking about!
actually the really funny thing is, I have done this. I took a class called Numerical Methods of Computation in College, cause I was a minoring in Applied Mathematics. And we had to write all these programs in C that would produce tables of figures for differential equations (which is like calculus on acid) using different aproximation algorithms. Anyway.
Actually, I wonder if the funny thing is that you had to do it for a class... and I did it for fun. Heh...
ian, you can take all my classes for fun if you want, and I'll take care of ezabel for a while...nobody will be the wiser.
Matt, you'd give up the opportunity to further your C++ knowledge?.......I'm ashamed to know you. :)
ohhh i most definitely would!