1

我需要有关循环的帮助,我正在尝试创建一个带有子菜单的菜单,在我的数据库中,我有一个名为 tblMenu 的表,它有五个字段(link_positionlinklink_namelink_titlesub_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>
4

1 回答 1

5

我不知道你为什么两次运行相同的查询。似乎您从第一个 SQL 中获得了所需的所有信息。如果我理解您的问题是正确的,则此代码既简单又快捷。

<%
msql = "SELECT link_position, link, link_name, link_title, IIF(IsNull(sub_menu),'',sub_menu) as sub_menu FROM tblMenu ORDER BY [link_position] ASC"
Set RS = Server.CreateObject("ADODB.Recordset")
RS.Open msql, Conn, 1,3

If Not RS.EOF Then
    response.write "<ul>"
    Do
        Response.Write("<li><a href='" & RS("link") & "' title ='" & RS("link_title") & "'>" & RS("link_name") & "</a>" & vbcrlf)

        If Not trim(RS("sub_menu")) = "" Then
            Response.Write("<ul class='sub-menu'>" & vbcrlf)
            Response.Write(RS("sub_menu") & vbcrlf)
            Response.Write "</ul>" & vbcrlf
        End If
        Response.Write("</li>" & vbcrlf)
        RS.MoveNext()
    Loop Until RS.EOF
    response.write "</ul>"
End If          
%>
于 2013-07-01T06:22:46.623 回答