0

Basically I want to have each child menu only display on the parent menu ID. But so far, all I can get is all of the child li's connecting to any parent menu I set up.

Here is a diagram to explain how the menu is displaying right now:

  • Home

    About > Employment & Mobile (I only want employment to display)

    Services > Employment & Mobile (I only want mobile to display)

  • Contact

So the queries output the following variables from the database

  • pg_LinkName
  • pg_MenuTitle
  • pg_ParentMenu (the name of the parent menu),
  • pg_MenuType (a yes/no to say if it is a parent/sub menu),
  • pg_SubMenu (a yes/no to specify whether the menu is a parent menu or not)

I want to know if there's a way to connect the child to the parent dynamically. eg. If I choose to attach a page to a parent, it only shows on that parent in the drop down.

Here are my queries:

<cfquery name="qry_GetMenu" datasource="#request.dsn#"> 
    SELECT *
    FROM tbl_pages 
    WHERE pg_MenuType = TRUE AND pg_Display = TRUE AND pg_AutoMenu = TRUE AND pg_Horiz_VertMenu = TRUE
    ORDER BY pg_sort
</cfquery>

<cfquery name="qry_GetSubMenus" datasource="#request.dsn#"> 
    SELECT *
    FROM tbl_pages 
    WHERE pg_MenuType = FALSE 
    ORDER BY pg_sort
</cfquery>

<cfquery name="qry_SubMenu" datasource="#request.dsn#"> 
    SELECT *
    FROM tbl_pages
    WHERE pg_SubMenu = TRUE
    ORDER BY pg_sort
</cfquery>

And this is what I have for the menu:

<ul class="menu">     
    <cfoutput query="qry_GetMenu">
        <li <cfif cgi.path_info contains "#pg_LinkName#"> class="current-menu-parent"</cfif>>
        <a href="#systemurl#/index.cfm/#pg_LinkName#/">#pg_MenuTitle#</a>
        <cfif pg_SubMenu gt 0>
            <ul class="sub-menu">
                <cfloop query="qry_GetSubMenus">
                    <li><a href="#systemurl#/index.cfm/#pg_ParentMenu#/#pg_LinkName#/">#pg_MenuTitle#</a></li>
                </cfloop>
            </ul>
        </cfif>
        </li>
    </cfoutput>
</ul>
4

1 回答 1

0

如果我理解正确,您只想在相应的父级下显示分配的子级,而您的代码当前正在显示每个父级下的子元素?

使用您当前的代码,您需要将您的查询之一移动

<ul class="menu">     
<cfoutput query="qry_GetMenu">
    <li <cfif cgi.path_info contains "#pg_LinkName#"> class="current-menu-parent"</cfif>>
    <a href="#systemurl#/index.cfm/#pg_LinkName#/">#pg_MenuTitle#</a>
    <cfif pg_SubMenu gt 0>
        <ul class="sub-menu">
        <!---moved inline to add additional conditional pg_ParentMenu clause--->
        <cfquery name="qry_GetSubMenus" datasource="#request.dsn#">  
            SELECT * FROM q WHERE pg_MenuType = 0 AND pg_ParentMenu = '#pg_ParentMenu#'
        </cfquery>
            <cfloop query="qry_GetSubMenus">
                <li><a href="#systemurl#/index.cfm/#pg_ParentMenu#/#pg_LinkName#/">#pg_MenuTitle#</a></li>
            </cfloop>
        </ul>
    </cfif>
    </li>
</cfoutput>

这将允许将查询专门过滤到当前父菜单,而不是所有父菜单。如果它是一个巨大的数据集,这将变得非常低效。那时有很多选择(执行循环并分配给另一个对象,例如具有父子键的结构数组,子元素是另一个数组,或者重新处理数据库以制作子菜单关系等)。

希望这是朝着正确的方向发展。

于 2013-05-06T15:02:19.923 回答