Welcome!

ColdFusion Authors: Yakov Fain, Pat Romanski, Liz McMillan, Maureen O'Gara, Greg Ness

Related Topics: ColdFusion

ColdFusion: Article

Ask the Training Staff

Ask the Training Staff

Two questions I received this month made me realize just how often we overlook the power of Lists and the many List functions in CF.

Many times, we look at a string or other piece of data and don't even realize that what we're looking at is actually a list. Once we realize that something can be treated as a list, CF gives us many ways of using or manipulating that data. Here are two perfect examples:

Q: We have an e-mail news-letter that we send out to those who subscribed at our Web site. All they have to do is give us their e-mail address. I want to find out how many unique domains are represented in our subscription list. In other words, for "john.smith@somecompany.com", how can I parse out the domain ("somecompany.com") from the user ("john.smith") in an e-mail address?
A: There are several easy ways to do this. The easiest way, perhaps, is to treat the e-mail address as a list that's delimited by the "@" character. Most people think of lists as being delimited only by commas or tabs and usually containing more than two data elements. CF, however, allows you to specify any character as the delimiter, so virtually any data string can become a list in the eyes of CF. I'll show you how to count the number of unique domains quickly, but I'll also encourage you to consider changing the way you store your e-mail address in your database to make this easier in the future.

First (see Listing 1), run a query to retrieve all your e-mail addresses. Next, create an empty list variable to hold the unique domain names. Next, loop over the query, treat each e-mail address as a list, and pull out the second element (the domain) in the list. If that domain isn't already in the list, append it to the list. Last, output the count of items (using ListLen()) in the domain list.

Now for my other recommendation: most of us simply create a column in our database called "E-mail" into which we stick our e-mail addresses. However, most of us also want some kind of reporting, as you've mentioned, so we know how many domains are represented in our list or, more likely, how many people there are from each domain (you'll quickly see how many of your subscribers are from AOL!). For this reason, you might want to consider breaking your one "E-mail" column into two database columns: "E-mail_Name" and "E-mail_Domain". You don't have to change anything on your Web form - let users enter their e-mail address in a single field. It's still very easy to insert into the database (see Listing 2), and it also makes it easy to pull your report using a query (see Listing 3).

Q: My application needs to add data to another company's application (it's written in PHP). I understand how to post the data to their site (I use the CFHTTP tag to post the data), but the site gives me back return codes that are one long string with the variables separated by the "&" character and I'm not sure how to get the data I need out of the string since it often comes back in a different order for each post. Their tech support people haven't been very helpful. Any suggestions for how I can get this data?
A: Yes. This is a common way of giving return codes back to a remote application, and getting the data can be very easy. What you want to do is break the single string down into the individual variable/value pairs that make up the string. Then you want to turn those pairs into CF variables that you can use in the logic of your application. Your return string may look something like this: "id=28734 &status=success&message=record was added successfully&add_date= 3/1/02".

Here are the steps to take (see Listing 4): first, initialize an empty structure to hold the variables/values returned by the remote application. Since you're using CFHTTP to post to the other site, their return string is coming back to CF as a variable named CFHTTP.FileContent. As you indicated, that string is a list delimited by the "&" character. Therefore, you need to loop over this list to find each variable/value pair. As you loop, you need to break each pair into its appropriate part (a variable name and its value). This is nothing more than another list delimited by a "=" sign. The value in front of the equal sign (position 1 of the list) is the variable name. The value in position 2 is the actual value of that variable. Set each one into its own variable and then dynamically name keys in your structure with the appropriate name/value. Now, regardless of the order in which they came back to you, you can test for the existence/value of the variables you were expecting.

.  .  .

Please send your questions about ColdFusion (CFML, CF Server, or CF Studio) to AskCFDJ@sys-con.com. And please visit our archive site at www.NetsiteDynamics.com/AskCFDJ.

More Stories By Bruce Van Horn

Bruce Van Horn is president of Netsite Dynamics, LLC, a certified ColdFusion developer/instructor, and a member of the CFDJ International Advisory Board.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.