Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4020810
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:16:28+00:00 2026-05-20T10:16:28+00:00

Can someone help me improve on this? There has to be a better way.

  • 0

Can someone help me improve on this? There has to be a better way. What I am doing is building a local web service for flex to grab in order to populate a tree menu. Flex being the application I’m calling the web service is just some background but has nothing to do with the problem.

In order to create hierarchical data for this tree, I’ve coded the following.

    <cffunction name="getFormsBinMenu" access="remote" returntype="string">
    <cfquery name="getParents" datasource="db_intranet_data">
        SELECT * FROM formsbin_categories WHERE parentid = 1 ORDER BY sortorder ASC
    </cfquery>

    <cfoutput>
        <cfxml variable="formsBinMenu">
            <?xml version='1.0' encoding='utf-8' ?>
            <folder label="Forms Bin">
                <cfloop query="getParents">
                    <folder label="#XMLFormat(getParents.catname)#">
                            <cfquery name="getSubParents" datasource="db_intranet_data">
                                SELECT * FROM formsbin_categories WHERE parentid = #catid# and testonly = 0 and visible = 1 ORDER BY sortorder ASC
                            </cfquery>
                            <cfloop query="getSubParents">
                                <folder label="#XMLFormat(getSubParents.catname)#">
                                          <cfquery name="getNextSubParents" datasource="db_intranet_data">
                                                SELECT * FROM formsbin_categories WHERE parentid = #catid# and testonly = 0 and visible = 1 ORDER BY sortorder ASC
                                            </cfquery>
                                            <cfloop query="getNextSubParents">
                                                <folder label="#XMLFormat(getNextSubParents.catname)#"/>
                                            </cfloop>                        
                                </folder>   
                            </cfloop>                                         
                    </folder>
                </cfloop>
            </folder>
        </cfxml>
    </cfoutput>

    <cfset menu =  #toString(formsBinMenu)#>
    <cfreturn menu>
</cffunction>

As you can see I’m just looping through different queries. While this works for my purpose, how could I code it so that there isn’t so much code repetition?

I thought about checking to see how many levels deep the structure would be and then running it all in an index loop but that still seems like too much overhead.

Any suggestions would be great!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:16:29+00:00Added an answer on May 20, 2026 at 10:16 am

    Take a look at the code I created using xslt transformation, I’m sure you can use the same approach in your code (note that parentID in my query is a foreign key of brandID):

    <cfquery name="queryBrands" datasource="#dsn#">
        SELECT brandID, brand, isAssignable, isnull(parentID, 0) AS parentID, abbreviation
        FROM dbo.BrandTree
    </cfquery>
    
    
    <cfxml variable="rawNodeTree">
        <cfoutput>
            <nodes>
                <cfloop query="queryBrands">
                    <node id="#queryBrands.brandID#"
                          parentID="#queryBrands.parentID#"
                          name="#XmlFormat(queryBrands.brand)#"
                          isAssignable="#queryBrands.isAssignable#"
                          abbreviation="#queryBrands.abbreviation#" />
                </cfloop>
            </nodes>
        </cfoutput>
    </cfxml>
    
    <cfxml variable="xslt">
        <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
            <xsl:template match="/nodes">
                <nodes>
                    <xsl:call-template name="getChildNodes" />
                </nodes>
            </xsl:template>
    
            <xsl:template name="getChildNodes">
                <xsl:param name="parentID" select="0" />
    
                <xsl:for-each select="//node[ @parentID = $parentID ]">
                    <xsl:sort select="@name" />
    
                    <node id="{@id}"
                          parentID="{@parentID}"
                          name="{@name}"
                          isAssignable="{@isAssignable}"
                          abbreviation="{@abbreviation}">
                          <xsl:call-template name="getChildNodes">
                            <xsl:with-param name="parentID" select="@id" />
                          </xsl:call-template>
                    </node>
                </xsl:for-each>
            </xsl:template>
        </xsl:transform>
    </cfxml>
    
    <cfset result = xmlTransform(rawNodeTree, xslt) />
    

    I believe when I was researching how to solve this same issue I used http://www.bennadel.com/blog/1080-Recursive-XSLT-For-Nested-XML-Nodes-In-ColdFusion.htm to help me out.

    Another option is to use CTE for your query then use fields from that to build your xml. (if you’re using ms sql server)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can someone help me re-write this SQL for MySQL database (has both subquery and
Can someone help me understand how to write this case statement properly its not
Can someone help my regex-challenged self with this? The match must start with *.
Can someone help me out on this. I have a problem with the return
Can someone help with this code. what am looking for is for the button
Can someone help me clean up this nested regex (re.sub) in python, please? I
Can someone help me with this piece of code. In a loop I'm saving
Can someone help me i need to sort this array: $report_fields['client_id'] = $row['client_id']; $report_fields['name']
Can someone help me understand the scoping rules in Java? This is clearly not
Can someone please help me with this ? I develop an application for iPhone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.