Skip to main content
March 11, 2009
Question

nested query for menu

  • March 11, 2009
  • 3 replies
  • 1283 views
I have a table that has id, category_parent and category_name. How can I write a query so that I can get nested <ul> as follwing:


<ul id="nav">
<li> <a href="#" >first Level menu</a>
<ul>
<li><a href="#" >second Level menu</a></li>
<li><a href="#" >second Level menu</a>
<ul>
<li><a href="#" >third Level menu</a></li>
</ul>

</li>
</ul>
</li>
</ul>


Please help!!
-A
This topic has been closed for replies.

3 replies

Participant
April 13, 2009

Hi,

I think Ken Ford's solution will work for 2 levels only.

We need to use recursive function. I made the sampe. It will work for n levels.

I used a static data for this example. I assumed first level menu contain parent id as 0.

My code:

<!---<cfquery name="request.menu" datasource="#dsn#">
  select id, category_parent, category_name from menu menuTable
</cfquery>--->

<cfscript>
  q = QueryNew('id,category_parent,category_name');
  newRow = QueryAddRow(q, 4);
 
  QuerySetCell(q, "id", "1", 1);
  QuerySetCell(q, "category_parent", "0", 1);
  QuerySetCell(q, "category_name", "First Level -1", 1);
 
  QuerySetCell(q, "id", "2", 2);
  QuerySetCell(q, "category_parent", "0", 2);
  QuerySetCell(q, "category_name", "First Level -2", 2);
 
  QuerySetCell(q, "id", "3", 3);
  QuerySetCell(q, "category_parent", "0", 3);
  QuerySetCell(q, "category_name", "First Level -3", 3);
 
  QuerySetCell(q, "id", "4", 4);
  QuerySetCell(q, "category_parent", "2", 4);
  QuerySetCell(q, "category_name", "second Level -1", 4);
 
  request.menu = q;
 
</cfscript>


<cffunction name="getChilds">
  <cfargument name="pId">

  <cfquery name="qChilds" dbtype="query">
      select * from request.menu
      where category_parent = <cfqueryparam value="#arguments.pId#" cfsqltype="cf_sql_bigint">
    </cfquery>
 
  <cfif qChilds.recordCount gt 0>
    <cfoutput query="qChilds">
      <ul>
        <li><a href="##">#category_name#</a></li>
        #getChilds(id)#
      </ul> 
    </cfoutput>
  <cfelse>
    <cfreturn>
  </cfif>
 
</cffunction>

<cfoutput>#getChilds(0)#</cfoutput>

August 24, 2009

I just wanted to say thank you for this post, it works perfectly!

Inspiring
March 11, 2009
"Amber_j" <webforumsuser@macromedia.com> wrote in message news:gp7gn7$6ab$1@forums.macromedia.com...
>I have a table that has id, category_parent and category_name. How can I write
> a query so that I can get nested
  • as follwing:
    >
    >
    > <ul id="nav">
    >
  • <a href="#" >first Level menu</a>
    >

    • >
    • <a href="#" >second Level menu</a></li>
      >
    • <a href="#" >second Level menu</a>
      >

      • >
      • <a href="#" >third Level menu</a></li>
        >

      >
      > </li>
      >

    > </li>
    >

>
>
> Please help!!
> -A
>


Here is how I do one:

<cfquery name="rsProducts" datasource="mydatasource">
SELECT p.ProductID, p.ProductName, c.CategoryName
FROM products p
LEFT JOIN categories c
ON p.CategoryID = c.CategoryID
ORDER BY c.CategoryName, p.ProductName
</cfquery>

<ul>
<cfoutput query="rsProducts" group="CategoryName">
<li>#rsProducts.CategoryName#
<ul>
<cfoutput>
<li>#rsProducts.ProductName#</li>
</cfoutput>
</ul>
</li>
</cfoutput>
</ul>


--
Ken Ford
Adobe Community Expert - Dreamweaver/ColdFusion
Adobe Certified Expert - Dreamweaver CS3
Adobe Certified Expert - ColdFusion 8
Fordwebs, LLC
http://www.fordwebs.com
http://www.cfnoob.com

Inspiring
March 11, 2009
> I have a table that has id, category_parent and category_name. How can I write
> a query so that I can get nested
as follwing:

Difficult to answer without knowing what DB you're using.

That's a pretty inefficient way of storing hierarchical relationships. Are
you in the position to change you approach here?

--
Adam