Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple CF Menu

Status
Not open for further replies.

countdrak

Programmer
Jun 20, 2003
358
US
I am trying to create a simple DB driven menu. Initially only the parent items with parentid = 0 are displayed. When the user clicks on one of the parent title links I refresh the page and display the children. Pretty straight forward.

I have a recursive function, but the problem is this opens all levels upfront. Can anyone help me tweak this function so it takes the parentid in a url and displays the child while keeping the whole tree open.

Here is the code


Code:
The qryNavigation is generated using a cfquery on top of this function.

<cfset variables.menustring = "">
<cfset variables.navigation = "">
<cffunction name="GenerateNav" access="public" returntype="string">
    <CFARGUMENT name="parentID" type="numeric" required="yes" default=0 >
    <CFARGUMENT name="level" type="numeric" required="yes" default=0 >
    <!--- scoping the variables that need to have their values kept private
    to a particular instance of the function call... --->
    <CFSET var checkForKids = ""><!--- used to hold temporary check for children --->
    <CFSET var objNav = ""><!--- used to hold temporary subqueries --->
   
    <!--- On our initial call to this function, we will purge the menustring and grab our navigation source query. --->
        <CFIF arguments.level eq 0>
            <CFSET variables.navigation = qryNavigation>
            <CFSET variables.menustring = "">
        </CFIF>
       
        <!--- Retrieve all nav records from variables.navigation who are the children of our current parent --->
        <CFQUERY name="objNav" dbtype="query">
            select * from variables.navigation where parentid = <CFQUERYPARAM value="#arguments.parentid#" cfsqltype="CF_SQL_INTEGER">
            order by sortorder
        </CFQUERY>
        <!--- write our current parent to the menustring... --->
        <CFSET variables.menustring = variables.menustring & "<UL>">
        <!--- loop through this parent's children... --->
        <CFLOOP query="objNav">
            <!--- check for children. If there are any, call this function recursively --->
				
                <CFQUERY name="checkForKids" dbtype="query">
                    select * from variables.navigation where parentid = <CFQUERYPARAM value="#objNav.cntid#" cfsqltype="CF_SQL_INTEGER">
                </CFQUERY>
				
                <CFIF checkForKids.recordcount gt 0><!--- this child has kids too! add it to the menustring, then make the recursive call... --->
                    <CFSET variables.menustring = variables.menustring & "<a class='leftnavigation' href='test.cfm?cntid=#cntid#&parentid=#cntid#'> <LI>" & objNav.NavDescription >
						
						<CFSET GenerateNav(parentID = objNav.cntID, level = arguments.level + 1) >
                <CFELSE><!--- this child is childless...just add it to the menustring... --->
                    <CFSET variables.menustring = variables.menustring & "<a class='leftnavigation' href='test.cfm?cntid=#cntid#&parentid=#cntid#'> <LI>" & objNav.NavDescription >
                </CFIF>
                <!--- close the list item --->
                <CFSET variables.menustring = variables.menustring & "</LI>">
        </CFLOOP>
        <!--- close the UL tag --->
        <CFSET variables.menustring = variables.menustring & "</UL>">
        <!--- return final variable to the caller... --->
        <CFIF arguments.level eq 0>
            <CFRETURN variables.menustring>
        </CFIF>
</cffunction>


Thanks for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top