The tree structure is still very common in our application, such as file directory, BBS, permission setting, department settings, etc. These numbers
The information is used to use a hierarchical structure, and it is difficult to express in our current relational database. Then you have to encounter a tree in the program.
How to deal with structural problems?
Recently, the author has solved a problem through an ASP permission management, and now it is now sorted out to readers.
First, the hierarchical data model is to be transformed into a relational data model. That is to say, how to be in our access, SQL Server
This data structure is designed in the Oracle and other relational databases. Let's take an example, such as one of the following data:
Document Management 1 | ---- New Document 2 | ---- Document Modification 3 | ---- Document Archive 4 | ---- View Archive Information 5 | | - Delete Archive Information 6 | | | ---- Delete Historical Documentation 7 | | | ---- Delete Official Document 8 | ---- System Management 9 | ---- User Management 10 Personnel Management 11 Administrative Management 12 Financial Management 13
This is a typical hierarchical structure data, then everyone wants to think, how to express it through two-dimensional tables? early
It's hard to look, yes. But carefully scrutinize or have a doorway drilled.
It can be described, regarding all the permissions above as a permission field, then this permission field is definitely there is an ID value. I
Then give this relational data sheet to add a field - subordinate ID field, that is, this permission is to belong to which level permission
Under, this ID value is part of which ID value. For example: "Viewing the Archive Information" permission ID value is "5", it is a "article"
Under the file archive, then the value of its membership ID field should be "4" .ok, if this can understand, then I
Their relationship transformation work is basically completed.
Below we start designing this relational data sheet (as described as SQL Server 7.0):
--------- ----------- --------- --------- --------- | Field Name | Field Meaning | Field Type | Field Size | Field Properties | ----------- --------- --------- ----------- -------- | Selfid | Permissions ID | INT | 4 | PK || PowerName | Valcha | 50 | NOT NULL || PowerInfo | Permission Information | VARCHAR | 500 | || BelongID | Particular ID | INT | 4 | | ---------- ---- ------- ----------- ---------- ----------
Ok, the structure is designed, you can easily enter your test data.
Then, we are the most critical steps for how to imitate the hierarchy in the web page.
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 = server.createObject ("adodb.recordset") rs.open "Select * from Power Where Belongid is Null Order By Powerid", CONN, 1, 3' Hierarchical State Variables Format_i = 1
'List main block DO while not rs.eof
'Printing parent data information response.write "
rs.movenext loop 'Close the parent table data set rs.closset = Nothing 'Sub-data processing subroutine SUB LISTSUBPOWER (ID) 'Open all sub-data information of the upper layer PowerID SET RS_SUB = Server.createObject ("AdoDb.Recordset") rs_sub.open "Select * from PowerS where belongid =" & id "order by power", conn, 1, 3 'List Data Do WHILE NOT RS_SUB.EOF 'Hierarchical State Variable Increte Format_i = Format_i 1 'Circulating the format control, because the top layer and the second layer do not need to be indented, so reference this block for i = format_i to 3 step -1Response.write "from the third layer" | "response.write" NEXT 'Print sub-data information response.write | ---- "response.write" " & Rs_sub ("powername") & "" response.write " 'Recurrent calling subprogram, gradually handle listsubpower (RS_SUB ("PowerID")) RS_SUB.MOVENEXT loop 'Hierarchical State Variable Remove Format_i = Format_i-1 'Close sublayer data set rs_sub.closeset rs_sub = Nothing end sub%> In the PowerList.asp program, let's open the top-level data, display it in the loop; then design a subroutine listSubpower, call the sub-layer data information by calling in the loop through the recursive algorithm, and in the subroutine internal cycle It is repeatedly to call yourself to deploy deep data by layer. In addition, a static variable Format_i is also used in the program to control the indent display format. This article makes a simple attempt in data design and program control in this paper, the purpose is to throw the brick, I hope the reader will get more inspiration through this article.
"