Stack expansion of the Forum Tree record table

Source: Internet
Author: User
Tags datetime empty
As a result of work, involves a tree-like record of the table, requires the program to expand the tree table, and output the corresponding data content. Because there are many places involved in such operations, such as the \\\ "forum" on the network is typical of the use of tree-like records of the table, hereby sorted out to share with you.
In many materials there is a program to expand the tree-like record, but many of them are recursive methods. We know that the recursive method of logic is relatively simple, the actual operation is relatively easy. But one of the biggest drawbacks of recursion is that it takes up too much resources and is too slow. If this method is used on the Internet's "forum", it will be a very serious problem if the tables are recorded in many cases. The following procedures in the author's work to solve a big problem and will be applied to my small station (yuking.126.com) forum.
I use a very fast stack here to do this, but it's relatively difficult to operate. We will take the forum record as an example to explain this process, first understand the table structure:

I. Table structure (article)
Self int ' This record ID number
Father Int ' parent record ID number
Title char ' post header
Author Char ' posts author
DateTime Date ' posting datetime
Hits int ' Click Number
reply int ' Back plural
..... ' Depending on the situation, we can also have more fields
1 Key Note: Self is the record ID number, this should be an automatic growth of the field, not allowed to repeat. Father is the field where the parent node ID number of the node is recorded, and if the record is "reply to", the field value should be the ID number of the "master". If the record is "master" then its parent node should be "0".
2) The following two records are parent-child records:
Self Father title author .....
1 0 ' Who can help me ' three legged cats '
2 1 ' What can I do for you? ' Online Fly '

Ii. Methods of operation
First, we read all the records where the parent node is "0", which is the "Master Paste," which is put into the stack. It then outputs a record at the top of the stack ("master") data and empties the top of the stack, moving the stack down one grid. Then find all the parent nodes ("paste") of the Stack item node ("POST"), and all on the top of the stack. Notice that no, in these two steps we have completed the work of outputting a parent record and expanding its child nodes. The rest of the work is just an analogy, until the stack is empty.
Of course, there may be some requirements in the actual operation, such as the number of layers required to be recorded, the number of replies, and so on, we need a little improvement can be achieved.

Third, this is the original code after the collation
<%
Set Rs=server. CreateObject ("ADODB.") Recordset ")

sql_text= "SELECT * from article where father=0 ORDER by datetime DESC"
Rs. Open sql_text,bbs_connectionstring ' finds all nodes that have parent nodes of ' 0 ', that is, the main paste.

Dim Stack (100,8) ' definition stack, I'm here 100 (' 8 ' means that each stack element holds 8 field values). ' The stack size can be defined according to the actual situation, and the maximum number of layers * is calculated by this formula.
While not rs.eof
Stack (0,0) =rs ("self")
Stack (0,1) =rs ("Father")
Stack (0,2) =rs ("title")
Stack (0,3) =rs ("author")
Stack (0,4) =rs ("datetime")
Stack (0,5) =rs ("hits")
Stack (0,6) =rs ("Reply")
Stack (0,7) =0
Top=1
Do Until Top=0
sql_text= "SELECT * from article where father=" &StackTop& "
RS1. Open Sql_text,myconn ' Find all records where the parent node is the top node of the stack
' Output stack item element, I here is the direct output, actually does when can produce the table and so on effect
For I=0 to 7
Response.Write Stack (top-1,i)
Next

Parentlevel=stack (top-1,7) ' Gets the number of layers of the parent record before emptying the stack
For i=0 to 7 ' empty stack Item Department
Stack (top-1,i) = ""
Next
Top=top-1 ' stack top Move down one grid
' All child records that derive from the found stack item node are stored on the stack
While not rs1.eof
Stack (top,0) =rs1 ("self")
Stack (top,1) =rs1 ("Father")
Stack (top,2) =rs1 ("title")
Stack (top,3) =rs1 ("author")
Stack (top,4) =rs1 ("datetime")
Stack (top,5) =rs1 ("hits")
Stack (top,6) =rs1 ("Reply")
Stack (top,7) =parentlevel+1
Rs1.movenext
Top=top+1 ' Stack item moves up
Wend
Rs1.close
Loop
Rs.movenext ' move to next record
Wend
Rs.close
Set rs=nothing
Set rs1=nothing
%>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.