我需要有关循环的帮助,我正在尝试创建一个带有子菜单的菜单,在我的数据库中,我有一个名为 tblMenu 的表,它有五个字段(link_position、link、link_name、link_title、sub_menu)
link_position字段用于对菜单项的位置进行排序,
链接字段存储菜单项的链接,
link_name保存菜单项的名称,
link_title保存菜单项的链接标题,
sub_menu存储菜单的子项(下拉菜单)。
这是 tblMenu 的预览
这是循环的代码
<ul>
<%
msql = "SELECT * FROM tblMenu ORDER BY [link_position] ASC"
Set RS = Server.CreateObject("ADODB.Recordset")
Set RSsub = Server.CreateObject("ADODB.Recordset")
RS.Open msql, Conn, 1,3
SET RSsub = Conn.execute ("SELECT * FROM tblMenu ORDER BY [link_position] ASC")
If Not RS.EOF Then
Do
Response.Write("<li><a href=""" & RS("link") & """ title =""" & RS("link_title") & """>" & RS("link_name") & "</a>" & vbcrlf)
If Not RSsub.EOF Then
Response.Write("<ul class=""sub-menu"">" & vbcrlf)
Do While Not RSsub.EOF
Response.Write(RSsub("sub_menu") & vbcrlf)
RSsub.MoveNext()
Loop
Response.Write "</ul>" & vbcrlf
End If
' RSsub.Close
Response.Write("</li>" & vbcrlf)
RS.MoveNext()
Loop Until RS.EOF
End If
%>
</ul>
输出
<ul>
<li><a href="/asp/" title ="Main Page">Home</a>
<ul class="sub-menu">
<li><a href="#">Inquiry</a></li>
<li><a href="#">Inquiry2</a></li>
<li><a href="#">ELS</a></li>
<li><a href="#">AP</a></li>
</ul>
</li>
<li><a href="/asp/page.asp?pID=1" title ="About us">About</a></li>
<li><a href="/asp/page.asp?pID=2" title ="E-Learning System">E-Learning</a></li>
<li><a href="/asp/page.asp?pID=5" title ="Academic Policies">Academic Policies</a></li>
<li><a href="/asp/page.asp?pID=6" title ="Facilities">Facilities</a></li>
<li><a href="/asp/events.asp" title ="Events Gallery">Events</a></li>
</ul>
问题
sub_menus 的循环将所有记录抛出主循环的第一个 Recordset 下,我期望的输出应该是这样的
<ul>
<li><a href="/asp/" title ="Main Page">Home</a></li>
<li><a href="/asp/page.asp?pID=1" title ="About us">About</a>
<ul class="sub-menu">
<li><a href="#">Inquiry</a></li>
<li><a href="#">Inquiry2</a></li>
</ul>
</li>
<li><a href="/asp/page.asp?pID=2" title ="E-Learning System">E-Learning</a>
<ul class="sub-menu">
<li><a href="#">ELS</a></li>
</ul>
</li>
<li><a href="/asp/page.asp?pID=5" title ="Academic Policies">Academic Policies</a>
<ul class="sub-menu">
<li><a href="#">AP</a></li>
</ul>
</li>
<li><a href="/asp/page.asp?pID=6" title ="Facilities">Facilities</a></li>
<li><a href="/asp/events.asp" title ="Events Gallery">Events</a></li>
</ul>