ColdFusion Tutorial - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

ColdFusion Tutorial

Description:

businesses love ColdFusion, because this database-to-Web gateway provides an ... In our table Contents we're going to add a new field called Picture. ... – PowerPoint PPT presentation

Number of Views:620
Avg rating:3.0/5.0
Slides: 42
Provided by: nickga9
Category:

less

Transcript and Presenter's Notes

Title: ColdFusion Tutorial


1
ColdFusion Tutorial
  • 11/2003

2
ColdFusion Intro
  • Why should you be interested in ColdFusion?
    businesses love ColdFusion, because this
    database-to-Web gateway provides an effective and
    simple way to make a dynamic Web.

3
ColdFusion Intro
  • Webmaster Rule Number OneWhen a potential
    employer/client asks you if you've ever worked
    with XYZ software running on platform ABC, say
    "yes" quickly and with great assurance. Then run
    home, fire up the Net, and figure out what the
    heck they were talking about.

4
ColdFusion Intro
  • ColdFusion is a tool made by MacroMedia that runs
    concurrently with most Windows and Solaris Web
    servers.
  • If you host your own server in-house, you can
    install the package on the same machine if you
    host with an outside provider, you'll have to
    convince them to purchase and install it. There's
    also a growing number of Web providers offering
    ColdFusion as an available service.

5
Intro
  • Once installed, you'll access your database with
    ColdFusion templates, which look surprisingly
    similar to standard HTML pages. The difference is
    that you can create a single template and serve
    up tons of information dynamically rather than
    having to create a separate page for every item.
    You'll need a basic knowledge of databases and
    HTML,.

6
Intro
  • A data source name (DSN) is the database you want
    to serve up on the Web and use with ColdFusion.

7
Intro
  • A ColdFusion template is a standard HTML page
    with some extra coding inserted before the ltHEADgt
    tag. This code tells ColdFusion which data source
    you want to access and sets up variables that
    you'll use further down the page in the actual
    HTML code. Templates use the file extension .cfm
    (in recent versions of the software) or .dbm (in
    versions prior to 3.0).

8
Intro
  • Let's look at a basic template file. We'll assume
    we have a data source set up as
    contents_of_my_pocket and that the data source is
    a Microsoft Access database called
    contents_of_my_pocket.mdb.

9
ColdFusion Intro.
  • Within that database is a single table called
    Contents, and you have fields defined as Item,
    Acquired, and Value. If you wanted a Web page to
    list all of the items in the database, the date
    you acquired them, and their value, the top of
    your template file would look something like
    this

10
ColdFusion Intro
  • ltCFQUERY NAME "pocket" DATASOURCE"contents_of_my
    _pocket"gtSELECT FROM contentslt/CFQUERYgt

11
ColdFusion Intro
  • This code tells ColdFusion to query your data
    source
  • NAME can be anything you like, as long as it
    matches the name in the CFOUTPUT, which I
    describe below. This allows you to set up more
    than one query on a page and name each one, so
    your output could include information from more
    than one database.

12
ColdFusion Intro
  • DATASOURCE is the name of your DSN, exactly as
    you defined it in ColdFusion Administrator.
  • The SELECT statement is a standard SQL statement
    that tells ColdFusion which records you want to
    select from what table. In this case, we're using
    the wildcard symbol "" to indicate that we want
    to grab the information from all fields in the
    table called Contents.

13
ColdFusion Intro
  • Now, below our query, we begin the standard HTML
    part of the template
  • ltHEADgtltTITLEgtContents of My Pocketlt/TITLEgtlt/HEAD
    gt
  • ltBODYgt
  • ltH1gtContents of My Pocketlt/H1gt

14
ColdFusion Intro..
  • ltH1gtContents of My Pocketlt/H1gt
  • ltCFOUTPUT QUERY "pocket"gtltBgtItemlt/Bgt
  • ltBRgtAcquired
  • ltBRgtvalue
  • ltPgtlt/CFOUTPUTgt
  • lt/BODYgt
  • lt/HTMLgt

15
ColdFusion Intro
  • The variables in our template are the items
    enclosed in hash marks (). Note that they're
    keyed exactly to the field names in our database
    contents_of_my_pocket.mdb. We can use these
    variables anywhere in our HTML document, as long
    as we enclose them in ltCFOUTPUTgt tags. CFOUTPUT
    tells ColdFusion that we're about to refer to
    some variables from a specific query named at the
    top of the document.

16
ColdFusion Intro
  • Note the bold tags around the item variable.
    That's one of the great things about this whole
    deal You can format text from your variables
    just like any static HTML text.

17
ColdFusion Intro
  • If a user accessed this page from his or her
    browser, the user would see something like this
  • Contents of My Pocket
  • One Bouncy Ball with Psychedelic Markings 12
    December 199825 cents
  • Half of a Cheese Sandwich 14 December 199825
    cents

18
ColdFusion Intro
  • ColdFusion runs through all of the records you
    chose in your SELECT statement (in this case, all
    of them) and returns formatted HTML code until
    you run out of records. To add items to our page,
    we only have to add items to the database.
    ColdFusion handles the rest.

19
ColdFusion Intro
  • SQL is a database query language that is
    relatively standard among most database
    applications, although Microsoft in its own
    inimitable way has added proprietary statements
    (see your Help file if you're using MS Access).
    In short, it's a way to select certain records
    from database tables, using criteria you choose.

20
ColdFusion Intro
  • For example, let's say that you wanted to select
    records from the Contents table in our
    hypothetical database contents_of_my_pocket.mdb.
    For our purposes, we'll assume that you want to
    select only items that were acquired on 12
    December 1998. Your template's SQL statement
    would look something like this

21
ColdFusion Intro
  • ltCFQUERY NAME "pocket" DATASOURCE
    "contents_of_my_pocket"gtSELECT FROM contents
    WHERE acquired IS "12 December 1998"lt/CFQUERYgt

22
ColdFusion Intro
  • The above tells ColdFusion to select all the
    records () from the table Contents where the
    field Acquired exactly matches the text "12
    December 1998."

23
ColdFusion Intro
  • In general, SQL uses the term IS to match text
    fields and "" to match numbers. Let's say you
    want to be less selective and see all of the
    items acquired in the month of December
  • ltCFQUERY NAME "pocket" DATASOURCE
    "contents_of_my_pocket"gtSELECT FROM contents
    WHERE acquired LIKE "December"lt/CFQUERYgt

24
ColdFusion Intro
  • The LIKE term tells ColdFusion to look for
    records where the Acquired field starts with the
    text "December" and is followed by any old day
    and date. The percent sign is SQL's wildcard
    character it means that we don't care what
    follows the word December. LIKE is almost always
    used in conjunction with a wildcard character.
    Similarly, you could select all items acquired in
    1998 by using the following

25
ColdFusion Intro
  • ltCFQUERY NAME "pocket" DATASOURCE
    "contents_of_my_pocket"gtSELECT FROM contents
    WHERE acquired LIKE "1998"lt/CFQUERYgt

26
ColdFusion Intro
  • Or if you want to select only items that have the
    letter "b" in them, you could use a statement
    like this one
  • ltCFQUERY NAME "pocket" DATASOURCE
    "contents_of_my_pocket"gtSELECT FROM contents
    WHERE item LIKE "b"lt/CFQUERYgt

27
ColdFusion Intro
  • All you need is a ltCFOUTPUTgt tag and the name of
    a query you defined in your top statement
  • ltCFOUTPUT QUERY "pocket"gt Once you've defined
    that, you're free to go hog wild with your
    variables. You have all HTML tags at your command
    as well, but remember that anything put within
    ltCFOUTPUTgt tags is going to repeat again and
    again down your page until all your selected
    records have been accounted for. For example, you
    wouldn't want to use this code....

28
ColdFusion Intro
  • ... because your output would look like this
  • Items in Pocket
  • One Bouncy Ball with Psychedelic Markings
  • Items in Pocket
  • Half of a Cheese Sandwich
  • Items in Pocket
  • A Bus Transfer etc....

29
ColdFusion Intro
  • One header line is sufficient, so we should place
    it before the output tag, so it won't get
    repeated for each record, like this
  • ltH1gtItems in Pocketlt/H1gt ltCFOUTPUT QUERY
    "pocket"gtitemltPgtlt/CFOUTPUTgt

30
ColdFusion Intro
  • That way we'll get
  • Items in Pocket
  • One Bouncy Ball with Psychedelic Markings
  • Half of a Cheese Sandwich
  • A Bus Transfer
  • etc....

31
ColdFusion Intro
  • You can do all sorts of HTML formatting on your
    variables and place them anywhere you'd like.

32
ColdFusion Intro
  • ColdFusion also has special built-in tags that
    will take your formatting even further.
  • Let's check out the ltCFIFgt and ltCFELSEgt tags.

33
ColdFusion Intro
  • In our table Contents we're going to add a new
    field called Picture. It will contain a URL that
    points to a location on our site where we've
    stored a picture of each item in my pocket. If we
    just wanted to output a picture of each item
    along with the text describing it, we'd use a
    CFOUTPUT section like this

34
ColdFusion Intro
  • ltCFOUTPUT QUERY "pocket"gtItem Item
  • ltBRgtltIMG SRC"Picture"gt
  • ltBRgtlt/CFOUTPUTgt

35
ColdFusion Intro
  • It's no problem, because ColdFusion variables
    don't mind being used as the source for ltIMGgt,
    ltSRCgt, or ltA HREFgt tags. But just to screw things
    up, let's say that we have pictures for some of
    our items but not all of them. In our Contents
    table the items without pictures have an empty
    Picture field. If we used a standard CFOUTPUT
    section like the one above, we'd end up with
    several broken images.

36
ColdFusion Intro
  • So let's use ColdFusion's ltCFIFgt and ltCFELSEgt
    tags to set up a simple if-then statement that
    will check to see whether the Picture field
    contains text or not.

37
ColdFusion Intro
  • CFOUTPUT QUERY "pocket"gtItem Item
  • ltBRgt
  • ltCFIF Picture EQ""gt    ltIMG SRC"my_generic_pic
    ture.jpg"gt ltBRgtltCFELSEgt    ltIMG
    SRC"Picture"gt ltBRgtlt/CFIFgt
  • lt/CFOUTPUTgt

38
ColdFusion Intro
  • What we've done here is set up an easy test in
    which ColdFusion will examine our Picture field
    and do one thing if it is empty (NULL) and
    another if text appears there. In the above code,
    I used a generic picture that will appear for
    items that don't have their own illustrations.

39
ColdFusion Intro
  • The test you perform on the CFIF line doesn't
    have to be a simple NULL or NOT NULL check. You
    can also use computations, Boolean operators, etc.

40
ColdFusion Resources
  • The resource you'll probably turn to most is
    ColdFusion's CFML language reference, which is a
    big list of CF tags, functions, and JavaScript
    statements. You'll find it along with other
    developer docs at MacroMedia's developer site.
    They're in Adobe Acrobat's PDF format.

41
ColdFusion Recap
  • ColdFusion is a great tool to use with web sites
    and your DBs
  • Cost --- 1300.00
Write a Comment
User Comments (0)
About PowerShow.com