Solved

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

Posted on 2007-03-27
4
174 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CFFILE upload help 98 207
Detailed steps to upload 6 106
Restore of mysql database from .SQL file - using Coldfusion 5 55
transfer content to new site design 7 49
PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

734 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