Skip to content

Instantly share code, notes, and snippets.

@smrchy
Created January 10, 2013 11:53
Show Gist options
  • Select an option

  • Save smrchy/4501497 to your computer and use it in GitHub Desktop.

Select an option

Save smrchy/4501497 to your computer and use it in GitHub Desktop.
<!---
# Breadcrumbs
Return all parents of an item, typically to create a breadcrumb navigation
This function will bubble up through a tree and return an array with all parents of a nested item excluding the root.
Adjust the query and its field names to your needs
Parameters:
* `id` The id of the item.
Returns:
* An *Array* of parents where the first array element is the topmost item excluding the root.
-->
<cffunction name="breadcrumbs" >
<cfargument name="id" required="yes" type="numeric">
<cfargument name="parents" required="no" type="array" default="#ArrayNew(1)#">
<!--- The is the root. Return what we have until here. --->
<cfif id EQ 0>
<cfreturn Arguments.parents>
</cfif>
<!--- Adjust this query to your needs --->
<cfquery name="local.cat">
SELECT ID, Name, ParentItemID FROM Categories
WHERE ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.id#">
</cfquery>
<cfif cat.Recordcount>
<cfif cat.ParentItemID NEQ 0>
<!--- Make sure you adjust the fields names to your query above --->
<cfset Arguments.parents = breadcrumbs(cat.ParentItemID, Arguments.parents)>
</cfif>
<cfset ArrayAppend(Arguments.parents, {'id'=cat.id,'name'=cat.name})>
</cfif>
<cfreturn Arguments.parents>
</cffunction>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment