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

Posted on 2008-11-14
Last Modified: 2013-12-24
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

Question by:sfareed
    LVL 2

    Assisted Solution

    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

    LVL 16

    Accepted Solution

    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

    LVL 2

    Expert Comment

    Simpler is better.  Nice modification.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now