Home
Username:
Password:
ColdFusion MX 7 Advanced Tutorials

User Defined Functions / UDF Libraries




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

In this movie, we're going to look at creating a UDF library and a UDF library is not a true technical term. We're just calling it that because what it's going to be is a group of functions that are defined within this page. We're going to put multiple functions on this page that kind of coordinate with multiple purposes within our application. So our example here is I have created the page UDF library and called it that. I don't want you to think you have to name it that. You can name it whatever you want. Normally I would give this a much better name based on some of the functions that are on the page. This page has two queries of our tours and our countries that we offer, so I would name this something to do with queries. But just for the sake of you knowing what this is, I gave it UDF library. We have two functions on here so what I did was I took the function from create UDF, the page we used In the previous movie, and I moved it onto the UDF library page. So here is my tours function, the exact one we used over here. Then what I did was copied this and just modified it for countries. So this one gives us a table of all of the tours we offer and the number of days of each tour. This function, called countries, is going to call a listing of all the countries we offer tours to. So we have two functions. That's all we have on this page. I've saved it under the name UDF library. I need to know the name because we need to use it for our next page. So what I'm going to do is create a new page. I'm going to name this calling a function or give it a title, anyway, and I will save this as call UDF. Let's name it that. So call UDF is the name of our page. Now, how do we go about calling a function? Well, if you remember, we just used CFset. We call a function passing any arguments that the function might need inside the parentheses. Now, if we want a list of all of our countries when we call this function, we don't pass anything. If I wanted a specific country, I could pass a country ID inside of here, just like we did with the tours. So we've called our function. Let's see what happens. So I'll put this into the browser and you can see we have an error. It says there is a local variable called countries and it's undefined. So the name of it is countries. Well, where is it getting a local variable called countries? Well, we're using a CFset, so it thinks this is a local variable, even though we have parentheses after it. So I figure I might just as well show this to you, save you some time in doing this. You must always either put the function on the page or call the function into the page using an include. So I'm going to use an include, well go up to CFML, use a Cfinclude and I'm going to include my library. So this is down in UDF library. I will include that file. Now both functions, both the tours function and the countries function are available to me on this page. So let's call the countries first and we'll see what happens this time and there it is. I have access. Those are all of the countries that we offer tours and I can also call, there's no reason why I can't use CFset to call tours as well. So we can put both of these on a page and needless to say, you can organize these on a page using CSS for positioning, using whatever you want. I just did it very simply. You can see there's our countries and there's our tours and we've created that using three lines of code, which is amazing. So just make sure if you have a separate template with the functions, you're not calling them on the page itself, that you include the file with the functions themselves. You can see I got a local variable definition error and that can throw you off if you're calling this and you say wait a minute, I'm not declaring a variable called countries. I'm calling a function. So just so you're aware of that. Go ahead and play around with this. Feel free. You do have some more database tables in here. You could go ahead and create some more functions to put on the UDF library by copying these. I don't have to have this in a table. I just did it to make it look easier for you or visually easier for you to see what's going on. I could put it in a list, I can position these, whatever I want to do using regular code. I just wanted to make it very basic so you can see what's going on here and that is UDFs.

Tutorial Information

Course: ColdFusion MX 7 Advanced
Author: Candyce Mairs
SKU: 33828
ISBN: 1-934743-34-8
Release Date: 2007-11-30
Duration: 6 hrs / 89 lessons
Work Files: Yes
Captions: For Online University members only
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available