Tree structures are still very common in our applications, such as file directories, BBS, permission settings, department settings, etc. These
data
information all adopt a hierarchical structure, which is difficult to express clearly in our current relational database. Sohow to deal
with tree structure problems in the program
?Recently, the author easily solved this problem through an ASP rights management program, and now I will sort it out for readers.
First, the hierarchical data model must be converted into a relational data model. In other words, how to design this data structure in our ACCESS, SQL SERVER
, ORACLE and other relational databases.
Let’s take an example, such as the following data:
Document Management 1
|----New document 2
|----Document modification 3
|----Document Archive 4
| |----View archived information 5
| |----Delete archived information 6
| | |----Delete historical documents 7
| | |----Delete official document 8
|----System Management 9
|----User Management 10
Personnel Management 11
Administration 12
Financial Management 13
This is a very typical hierarchical structure data, so think about it, how to express it in the form of a two-dimensional table?
It seems difficultat first
, right? But after careful consideration, there is still a way to exploit it.
In this way, all the above permissions can be regarded as one permission field, then this permission field must have an ID value.
We
then forcibly add another field to this relational data table - the subordinate ID field, which indicates which level of permission this permission belongs to
, that is, which ID value this ID value belongs to. For example: the "View Archive Information" permission ID value is "5", and it is subordinate to the "
Document
Archiving" permission, then the value of its subordinate ID field should be "4".OK, if this can be understood, then
our
relationship transformation work is basically completed.Let's start designing this relational data table (taking Sql Server 7.0 as an example):
+-----------+-----------+----- ------+-----------+----------+
| Field name | Field meaning | Field type | Field size | Field attributes |
+-----------+-----------+-----------+-----------+- ---------+
| SelfID | Permission ID | Int | 4 | PK |
| PowerName | Permission Name | Varchar | 50 | Not Null |
| PowerInfo | Permission Information | Varchar | 500 | |
| BelongID | Belong ID | Int | 4 | |
+-----------+-----------+-----------+-----------+- ---------+
Okay, once the structure is designed, you can easily enter your test data.
Then, we will focus on how to imitate the hierarchical structure in the web page to display the ASP program with this function. This is also the most critical step.
Program list: powerlist.asp
<%
'Database connection
set conn=Server.CreateObject("ADODB.Connection")
conn.open "driver={SQL Server};server=chaiwei;DATABASE=chaiwei;UID=sa;PWD="
'Open all parent layer data
set rs=Server.CreateObject("ADODB.Recordset")
rs.Open "select * from powers where belongid is null order by powerid",conn,1,3
'Assign initial value to level number expression variable
format_i=1
'List main program section
do while not rs.eof
'Print parent layer data information
response.write "<a href='powerlist.asp?SelfID=" & rs("powerid") & "&BelongID=" & rs("belongid") & "'>" & rs("powername") & "< /a>"
response.write "<br>"
'Subroutine call, sub-layer data processing
Call ListSubPower(rs("powerid"))
rs.movenext
loop
'Close the parent layer data set
rs.close
set rs=nothing
'Sub-layer data processing subroutine
Sub ListSubPower(id)
'Open all sub-layer data information belonging to the upper powerid
set rs_sub=Server.CreateObject("ADODB.Recordset")
rs_sub.Open "select * from powers where belongid=" & id & " order by powerid",conn,1,3
'Column sub-layer data
do while not rs_sub.eof
'Level number expression variable progressive accumulation
format_i=format_i+1
'Loop indentation format control, because the top and second levels do not require indentation, so this program segment is referenced from the third level
for i=format_i to 3 step -1
response.write "|"
response.write " "
next
'Print sub-layer data information
response.write " |----"
response.write "<a href='powerlist.asp?SelfID=" & rs_sub("powerid") & "&BelongID=" & rs_sub("belongid") &"'>" & rs_sub("powername") & "< /a>"
response.write "<br>"
'Recursively call the subroutine itself to gradually process the sub-layer data
ListSubPower(rs_sub("powerid"))
rs_sub.movenext
loop
'The number of levels indicates that the variable is progressively decreasing
format_i=format_i-1
'Close the sub-layer data set
rs_sub.close
set rs_sub=nothing
End Sub
%>
In the powerlist.asp program, we first open the top-level data and display it in the loop; then we design a subroutine ListSubPower, which is called in the loop through a recursive algorithm to open the subroutine. layer data information, and repeatedly calls itself in the inner loop of the subroutine to expand the deep data layer by layer.
In addition, a static variable format_i is used in the program to control the indentation display format.
This article makes a simple attempt at data design and program control in the tree structure. The purpose is to inspire others. I hope readers can get more inspiration from this article.