Developing a Left Nav menu according to the deparment and their web links by using database

There are six departments in our office and every department contian 6 or more web links.
I am trying make a left navigation bar on my web application.
All clickable links and their names are coming from the  following table
The following code works fine but Now I have to display the department name on the left nav and then below each department name display the links belongs to the department
I have another table by the name of
The columns of **Department** Table are DepID, DepName (DepID is a primary Key)
The columsn of Main Menu Table are  MenuID, DepID, MenuName, Link (DepID is a Foriegn Key of Departement table)
Some thing liks a Yahoo left nav bar

<a href=order.htm>Order Form</a>
 <a href=Security.htm>Security</a>
<a href=work.htm>Work Hours</a>

<a href=service.htm>IT Service </a>
 <a href=Events.htm>IT Events </a>
<a href=orders.htm>IT orders </a>


 <a href=forms.htm> All forms</a>
 <a href=application.htm>Work App </a>
 <a href=work.htm>New App </a>
<cfquery name=qMainMenu" datasource="rmdata">
Select *
From MainMenu
<cfoutput query=qMainMenu">
<a href=#qMainMenu.Link#>#MenuName#</a>

Open in new window

Who is Participating?
duncancummingConnect With a Mentor Commented:
A simpler way to do it would just be to group the output on the department name.  The following code (thanks Bob!) will print out each department name with links grouped underneath.
<!--- join mainmenu to department to get depnames --->
<cfquery name=qMainMenu datasource="rmdata">
	SELECT DepName, MenuName, Link
	FROM MainMenu, Department
	WHERE MainMenu.DepID = Department.DepID
	ORDER by DepName
<cfoutput query="qMainMenu" group="DepName">
	<!--- print each link within the department group --->
		<a href="#qMainMenu.Link#">#qMainMenu.MenuName#</a>		

Open in new window

bchesleyConnect With a Mentor Commented:
Here is one approach.  I didn't run this code so check for typos.


<!--- store blank depname to start --->
<cfset curDepName = "">
<!--- join mainmenu to department to get depnames --->
<cfquery name=qMainMenu datasource="rmdata">
Select DepName, MenuName, Link
From MainMenu, Department
Where MainMenu.DepID = Department.DepID
Order by DepName
<!--- output query results and only print department if it does not match prior value --->
<cfoutput query="qMainMenu">
	<!--- if current record DepName != curDepName output DepName --->
    <cfif #qMainMenu.DepName# NE #curDepName#>
	<!--- store depname to variable --->
	<cfset curDepName = #qMainMenu.DepName#>
    <!--- print each link within the deparment group --->
	<a href=#qMainMenu.Link#>#MenuName#</a> 

Open in new window

Simpler is better.  Nice modification.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.