Solved

How to set a variable based on a user selected folder / directory location of .cfm file

Posted on 2007-03-27
4
173 Views
Last Modified: 2013-12-16
Hello CF Experts,

I have a CF application that I would like to dynamically set an ID variable based on the folder location of the .cfm file.

So, in my application, I have folders named Category1, Category2, Category3 and so on.

In those folders, I have index.cfm.

I would like to set a (known) category ID based on the folder (category) that the user chooses.

The logic would go something like this.

If folder name Category1 is detected in URL, set this:
<cfif (detect folder name for Category1 in url)>
<cfset sectionID = "123456">
</cfif>
<cfif (detect folder name for Category2 in url)>
<cfset sectionID = "123457">
</cfif>

Again, we already know what the ID is going to be, which will corelate to the chosen category, we just need to have a way to detect the folder name on the site that they choose.  Of course, we could always hard code the categoryID in the URL on the category selection page, but we don't really want to do that here.  Our main page is not dynamic in nature, and we are fine with that.

Thanks in advance for any help!
0
Comment
Question by:dstjohnjr
  • 2
  • 2
4 Comments
 
LVL 39

Accepted Solution

by:
gdemaria earned 500 total points
ID: 18802337

this function will give you your folder location..
  getBaseTemplatePath()

This will tell you the folder

 <cfset dir =  ListLast(getDirectoryFromPath(getCurrentTemplatePath()),"/")>

Then use a switch

 <cfswitch expression="#dir#">
  <cfcase value="category1">
      <cfset sectionID = 12343>
  </cfcase>

 etc...

 
0
 

Author Comment

by:dstjohnjr
ID: 18802701
Ok, in this example, #dir# returns the full path (i.e. c:\cfusionmx7\wwwroot\category1\) to the location being browsed.

How do I test for a portion of the path in the cfcase code?  It works if I enter:

<cfcase value="c:\cfusionmx7\wwwroot\category1\">

but not just

<cfcase value="category1">

Also, you mention:

"this function will give you your folder location..
  getBaseTemplatePath()"

how do I implement this function?

Thanks for your help!
0
 

Author Comment

by:dstjohnjr
ID: 18803349
Ok, I actually figured this out on my own.  Here is the solution:

<cfset dir =  ListLast(getDirectoryFromPath(getCurrentTemplatePath()),"\")>
<!--- Start parsing and testing for the various cases based on our folder  --->
<!--- structure and then set the categoryID and variable based on our folder location --->
<!--- NOTE:  This code MUST be executed from within each folder you want to test for and NOT in an includes folder --->
 <cfswitch expression="#dir#">
<cfcase value="Category1" delimiters="\"  >
<cfset categoryID = 1001>
</cfcase>
<cfcase value="Category2 delimiters="\" >
<cfset categoryID = 1002>
</cfcase>
...and so on...
</cfswitch>

The problem was that initially, I did not have the delimiters property setup in the cfcase statement.  Once I added that, all is good!  Thanks for your help!  Although I ended up figuring it out on my own, you provided a great start and led me down the right path.  Thanks!
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 18803499

 glad you got it.

 The problem was the direction of the slash...
<cfset dir =  ListLast(getDirectoryFromPath(getCurrentTemplatePath()),"/")>

 should have been
 <cfset dir =  ListLast(getDirectoryFromPath(getCurrentTemplatePath()),"\")>
                                                                                                               ^^^^

 Glad you caught that small problem.


 However, in your cfcase statement, the delimiters parameter should be removed

  <cfcase value="Category1" delimiters="\">

 That parameter indicates the delimiter in the value clause, if there is a list such as this..

 <cfcase value="A|B|C|D" delimiters="|">

 Since you only have one value "Category1" the delimiter does nothing.

0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dreamweaver server behavior gone 6 159
WCAG (Web Content Accessibility Guidelines) levels 3 116
Gulp not seeing Changes 4 91
coldfusion upload spreadsheet into the databse 2 57
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
The purpose of this video is to demonstrate how to connect a WordPress website to Google Analytics. This will be demonstrated using a Windows 8 PC Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php :…
The purpose of this video is to demonstrate how to create a Printer Friendly PDF on a WordPress Page. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome Screenshot” Google Chrome Extension, and SmallPDF.com Log…

679 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