The menu continues on the article
In the previous article we have implemented a dynamic menu in the Web page. We are using the JS script to achieve.
But the menu content is hand written, now we want to use ASP program to read out the data from the database, and
Generate these menus in your program.
A recursive way to generate menus
When it comes to reading data from a database and generating a menu, let's recall the first part of the article that mentions
Database structure. Datasheet (Treemenu), we record the ID of the parent menu for each menu, if this
ID is 0, then it is the root menu, and the number of submenus for each menu is recorded: chi_id means
For any menu, we can use
SELECT * from Treemenu where par_id= current ID
Such an SQL statement to find its submenu. To find the parent menu of any menu, you can use:
SELECT * from Treemenu where id= current par_id
Understanding these two SQL statements, we say recursive thinking, recursive expression is a function or subroutine
Called itself again. Now we define a subroutine called Distree (ID), which is the function of displaying ID ID
Menu and all submenus. Then we call Distree (0) to display all the root menus. Look at this subroutine.
Simple method of thinking:
<%
Sub Distree (ID)
' Find all records with ID field ID
' Show this record and write down its ID (recorded as ID2)
' Show submenu and submenu: Disptree (ID2) Here's a recursive
' Til the End
End Sub
%>
Second, the completion of recursive functions
Now we're going to complete this recursive function. Here is the completed function, which is written in the note.
<%
Sub Distree (ID) ' defines a subroutine
' Define variables, note that these variables must be defined, is a local variable, otherwise it will be a global variable
Dim sql,rs,chid,i
' The following sentence defines the SQL query statement and gets the submenu with the parent menu ID
Sql= "SELECT * from Treemenu where par_id=" &id
searchtable my,sql,rs ' query table
If Rs.eof then Exit Sub ' if there is not a record, quit the subroutine
If Id=0 then ' if it is a root menu, it has to be handled separately because it has no previous connection diagram
Response.Write "<table width= ' 100% ' border=0 cellspacing=0 ' cellpadding=0>"
Else
' If not the root menu, show all. Where Cellid is the name of the table on which the menu is located, the named method presses the previous
Response.Write "<table id= '" "&cellidc&" ' style= ' Display=none ' "&_
"Width= ' 100% ' border= ' 0 ' cellspacing= ' 0 ' cellpadding= ' 0 ' >" &br
End If
Totalrec=rs.recordcount ' Gets the total number of records, here is the actual number, also can use RS ("chi_id")
For I=1 to Totalrec ' use a loop to show all the records
If Id=0 then ' if it is a root menu, use another icon
ico= "Images/home.gif"
Else ' Otherwise, just use the folder icon
ico= "Images/fc.gif"
End If
Chid=rs ("chi_id") ' Gets the number of submenus it has
If Chid=0 then ' if it does not have a submenu, the connection graph will use a "+" number.
If I=totalrec then ' if it's the last submenu, use a 90-degree connection graph
Ph= "Images/line_cco.gif"
Else
Ph= "Images/line_co.gif" or use T-shaped connection diagram
End If
Response.Write "<tr height=" > "&br" Output row to display a record (menu)
If Id<>0 then ' if it is not the root menu, show the connection graph, ph is the file name of the connection diagram generated above
Response.Write "<td width= ' height= ' >" &br
Response.Write "Response.Write "</td>" &br
End If
' Output this record below (menu)
Response.Write "<td height= ' >" &br
Response.Write " '
Response.Write "<a href= '" &rs ("link") & "' target= ' main ' >" &rs ("TXT") & "</a>"
' Response.Write ' <a href= ' "&scr&" action=add&id= "&rs (" id ") &" > Add </a> "
' Response.Write ' <a href= ' "&scr&" action=del&id= "&rs (" id ") &" ' > Delete </a> "
' Response.Write ' <a href= ' "&scr&" action=mod&id= "&rs (" id ") &" > Change </a> "&br
Response.Write "</td>" &br
Response.Write "</tr>" &br
Else ' If it has a submenu, use the ' + ' connection diagram
If I=totalrec then ' if it's the last submenu, use a connection diagram with no subsequent menus
Ph= "Images/ctc.gif"
Else
Ph= "Images/cc.gif" or use a connection diagram with a subsequent menu
End If
L=l+1 ' L represents a line,
R=r+1 ' R represents a column
Cellid= "L" &l& "R" &r ' Generate name
Cellida= "L" &l& "R" &r ' generate the name of the connection graph
cellidb= "L" &l& "R" &r+1 ' Generate icon Name
Cellidc= "L" &l+1& "R" &r+1 ' Generate submenu name
If Id=0 then ' if it's a root menu, just output this submenu directly
Response.Write "<tr height= ' width= ' 100% ' >" &br
Else ' Otherwise, to output the connection graph first
Response.Write "<tr height= ' >" &br
End If
If Id<>0 then ' is not a root menu, output connection graph
Response.Write "<td width= ' height= ' >" &br
Response.Write "CloseTable RS
End Sub
%>
Now the last HTML file to rewrite the ASP program, change the filename, and then the original menu part of the deletion, add the above subroutine
You can add the following statements. Remember not to delete the JS script!
<!--#include file= "operation$db.asp"-->
<%
Opendb my
Dim i,l,r,cellid,ph
Dim cellida,cellidb,cellidc
L=0
R=0
Br=vbcrlf
Distree (0)
%>
Here, the menu can already be displayed, of course, if you want to change, now can only go to the database to change. Just add
Simple function, you can manage it. The next article we add to it, delete, modify the function.