Server Time:
Sunday May 11 2008 07:07 PM  
Your Time:
  
HostMySite.Com is sponsoring this tutorial, please visit their site today!
This tutorial is sponsored by HostMySite.Com - ColdFusion Hosting

Yet Another Dynamic Drop Down Tutorial
by: Louis Stowasser
Email this tutorial to a friend Display Printer Friendly Format
[Download in PDF Format] [Download in FlashPaper Format]

Yet Another Dynamic Drop Down Tutorial

Yet Another Dynamic Drop Down Tutorial

Hey guys. You are probably sick to death of this type of tutorial, but I have found a method that is better than all of them! It creates a multidimensional array and fills in a select box depending on the other select boxes value. I must mention Megan in here for whom I used some aspects of her tutorial: http://tutorial95.easycfm.com/
The benefits of this method is it builds an array from the database and inputs that into the select form. So no need for using switch.

To see this in action go here: http://gcm.syr.edu/cfml/louis/multid.cfm


<cfquery name="getLocal" datasource="#request.dsn#">
SELECT sub.catID,sub.catName,topc.catID AS topID,topc.catName AS topName
FROM subCat sub INNER JOIN topCat topc ON sub.parentID = topc.catID
ORDER BY topName asc,sub.catName asc
</cfquery>

So here we join 2 tables. The sub category table and the top level category. If you use 1 table like that Recursion Tutorial sugests, you can simply join the table onto itself.

<script language="JavaScript1.2">
function populate(obj) {
var subcats = new Array(
<cfoutput query="getLocal" group="topName">
<cfoutput>new Array(#currentrow-1#,"#catID#","#catName#","#topID#")<cfif currentrow NEQ recordcount>,</cfif></cfoutput>
</cfoutput>);

// so the above code is creating a multidimensional array and putting in the values from our query

obj.subcat.options.length = 0;
obj.subcat[0] = null;
var optc = 0;

// here we set the vars.We make sure the subcat select has nothing in it before we add to it. We also create our loop counter called optc

for(i=0;i<subcats.length; i++) {
      if(subcats[i][3] == obj.topcat.value) {
            obj.subcat[optc] = new Option(subcats[i][2],subcats[i][1]);
            optc++;
      } else {
            obj.subcat[0] = new Option("- Please select a category -","null");
      }
}
}


// the above code loops over the array and does an if statement to filter out the array and only fill the select box with subcategories from the selected category. It then increases our counter by 1.
</script>

<form name="myform" action="myactionpage.cfm" method="post">

<select name="topcat" onchange="populate(this.form)"><!-- This is the category selector that will call the function -->
<option>- Select a Category -</option>
<cfoutput query="getLocal" group="topName">
<option value="#topID#">#topName#</option>
</cfoutput>
</select>

<!-- we group the query results by the top level category so that only the top level categories are outputted -->

<select name="subcat">
<option>- Please select a category -</option>
</select>

<!-- This is the sub category select box that gets filled in -->

</form>


Hopefully that worked for you. Contact me if you have any problems at louisstow AT hotmail DOT com (antispam method. im not crazy)


Date added: Wed. January 16, 2008
Posted by: Louis Stowasser | Views: 2243 | Tested Platforms: CFMX8 | Difficulty: Intermediate
Categories Listed: Code Snipplets

HostMySite.Com is sponsoring this tutorial, please visit their site today!
This tutorial is sponsored by HostMySite.Com - ColdFusion Hosting

This author's other tutorials:
Making a Forum
In this tutorial we will learn how to make a forum from scratch. - Date added: Wed. January 16, 2008
Creating a Random String
This tutorial will teach you how to create a random string. - Date added: Fri. September 21, 2007
Please rate this tutorial:
5 Stars 4 Stars 3 Stars 2 Stars 1 Stars
Comments on this tutorial
Read previous comments on this particular tutorial
Great Job!
Great Job Louis! - I really like this :D - Megan
Posted by: Megan
Posted on: 01/16/2008 09:51 PM
WTG!
you do great work!
this is very cool, and useful!
Posted by: Greg
Posted on: 01/17/2008 06:21 AM
Looks good...but
Looks good Louis. One problem I'm having is that I'm not using MSSQL, I'm using MySQL and this part confuses me......

subCat sub INNER JOIN topCat topc

There are 2 tables, right? One named "Sub" and one named "topc"....so where does "subCat and "topCat" come from?

These are subqueries I know, but I still don't know where subCat and topCat come from.

Thanks!
Posted by: Charles
Posted on: 01/26/2008 03:34 PM
Tables please :-)
Louis,
In Megen's example, she showed the tables and if you would add that to your example it would be VERY helpful, especially to SQL newbies like myself :-)

Thanks!
Posted by: Charles
Posted on: 01/26/2008 04:00 PM
Fantastic!!
Works like a charm! VERY handy.
Posted by: Charles
Posted on: 01/26/2008 10:41 PM
Table
I didn't include an example of the tables for the following reasons:

a) This is pretty much an alternative to using switch in megans tutorial
b) Your database will be different and you shouldn't design your database around my example. You need to adapt your own database.

Thanks
Louis
Posted by: Louis Stowasser
Posted on: 02/01/2008 02:05 AM
Link to example is broken
Not Found

The requested URL /cfml/louis/multid.cfm was not found on this server.
Posted by: Nate
Posted on: 02/12/2008 04:28 AM
Great Job
This was a very very useful tutorial. It solved a lot of my problems. Thank you so much
Posted by: Donboe
Posted on: 04/25/2008 03:28 AM
Post a new comment on this tutorial
post a new comment on this particular tutorial
Your Name:
Your Email:
Comment Title:
Comments:
Key Phrase:
 
Skyscrapper Banner Advertisement
Daily Razor - ColdFusion Hosting

You are 1 of 627 active sessions! | Privacy | Company
Copyright © 2002 EasyCFM.Com, LLC. (Easy ColdFusion Tutorials) All Rights Reserved
All other trademarks and copyrights are the property of their respective holders.
ColdFusion Hosting ColdFusion Hosting
ADD TO:
Blink
Del.icio.us
Digg
Furl
Google
Simpy
Spurl
Y! MyWeb