HomeMySQL Page 6 - Building a Simple ColdFusion Content Management System with MySQL
Breadcrumb List - MySQL
This tutorial lays out the basics for creating a content management system in ColdFusion and MySQL. The System leverages hierarchical data to automatically build breadcrumb lists, a side navigation menu, as well as a site map. The goal of this tutorial is to provide a framework upon which to build a content management system that is easy to customize and maintain, leaving the web author more time to create content than maintain code.
Breadcrumb lists are useful for many people that may be navigating your site. As they click deeper in your site, they often would like to go back up the hierarchy of it or simply go back to the home page. We are going to create a new page (you can call it whatever you want but for this tutorial I will name if Breadcrumb.cfm) for the breadcrumb code, then CFINCLUDE it into the top of the page after the CFQUERY for the page. So on a new ColdFusion template add the following.
Now let's walk through it. Line 2 sets a variable that will contain the list. Line 3 starts the CFLOOP that will loop through the HIERARCHY field that we have built on the record add action previously discussed. Since we already ran the CFQUERY for the current page, we can use the results in the CFLOOP. We are going to loop through the hierarchy list to query out the title of all the pages that will be shown on the breadcrumb list. Lines 4 through 8 query the records every time through the loop using the value in the hierarchy list to query out the ID of the record. Lines 9 through 11 add to the breadcrumb list variable with all the HTML markup so people can click on the link to bring them to the page.
Lines 14 through 18 set a variable called "Baseid". This variable will be used for the menu that will be explained in the next section. The menus I will be building next will be based on the HIERARCHY field. The menu may get rather long if it displays all of the pages within the site (if the site you are building is going to get large). What I am trying to accomplish with the Baseid variable is to determine what section of the site you are at. I am trying to get the ID of the second level page, which would be pages that are the children of the home page. Line 14 starts a CFIF statement that checks to see if the hierarchy list is greater than 1 (not the home page). If the CFIF is true, then it CFSET's the baseid variable to the second number in the hierarchy list. Line 16 and 17 catch the false value and CFSETS the baseid to 1 because it is the homepage. Line 20 simply outputs the breadcrumb list on the page.