f - METU Computer Engineering

horseheadssolidInternet και Εφαρμογές Web

10 Νοε 2013 (πριν από 3 χρόνια και 9 μήνες)

116 εμφανίσεις

Chapter 27 –Macromedia
ColdFusionMX
ColdFusion

MX
Outline
271
Introduction
27
.
1

Introduction
27.2 Simple ColdFusion Example: Clock
27.3 Using Variables and Expressions
27.4 Variable Scoping
275
FPi
27
.
5

F
orm
P
rocess
i
ng
27.6 Creating a Data Source Name
27.7 Bookstore Case Study: Interface and Database
27.8 Bookstore Case Stud
y
: Sho
pp
in
g
Cart
yppg
27.9 Advanced Topics for ColdFusion Developers
27.10 Web Resources
©
2004 Prentice Hall, Inc. All rights reserved.
Objectives
•In this lesson, you will learn:

TocreatebasicColdFusiontemplates

To

create

basic

ColdFusion

templates
.
–To use ColdFusion Markup Language tags and functions to
add dynamic functionality to Web pages.

To use information collected from Web forms.
–To create a database-driven bookstore application.

Tocreateashoppingcartforthebookstorethatfollowsa
To

create

a

shopping

cart

for

the

bookstore

that

follows

a

user through the site.
©
2004 Prentice Hall, Inc. All rights reserved.
27.1Introduction
•ColdFusion

Widelyusedserver
-
sidemarkuplanguage

Widely

used

server
-
side

markup

language
–Developed by Allaire
–ColdFusion Markup Language (CFML)
•Based around a set of tags that work like XHTML tags
•Supports XML, ColdFusion Components and Web services

Savedwitha
.cfm
extension
Saved

with

a

.cfm
extension
•ColdFusion template
©
2004 Prentice Hall, Inc. All rights reserved.
27.2Simple ColdFusion Example: Clock
•CFML comment

<!
---
CFML Comment
---
>
<!
CFML Comment
>
•cfoutput
tag

Variablesandfunctionsareenclosedin
#

s

Variables

and

functions

are

enclosed

in

#
s
–dateFormat
–timeFormat
–now
©
2004 Prentice Hall, Inc. All rights reserved.
Outline
Outline
lkf
1 <?xml version = "1.0"?>
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4

c
l
oc
k
.c
f
m
(1 of 2)
5
<!--- Fig. 27.1 : clock.cfm --->
6
<!--- A simple ColdFusion example --->
7

8 <!--- CFML comment tag --->
9
!
XHTML t t

9
<
!
--
XHTML
commen
t t
ag

-->

10

11 <html xmlns = "http://www.w3.org/1999/xhtml">
12 <head>
13

<title>
A Simple ColdFusion Example
</title>

13


<title>
A Simple ColdFusion Example
</title>

14

15 <style type = "text/css">
16 p { font-size: 14pt; color: blue }
17

td

{

background
-
color
:

black
;

color
:

yellow

}

17


td

{

background
color
:

black
;

color
:

yellow

}

18
</style>
19
</head>
20

21
<
body
>
21

body
22

23 <p>A Simple ColdFusion Example</p>
24

25 <table border = "1">
©
2004 Prentice Hall, Inc.
All rights reserved.

Outline
Outline
lkf
26 <tr>
27 <td>
28

29 <!--- #’s between cfoutput tags are processed --->
30

!
il h d

c
l
oc
k
.c
f
m
(2 of 2)
30


<
!
---

D
i
sp
l
ay

t
h
e

current
d
ate

--->

31 <cfoutput>#dateFormat( now(), "mmmm dd, yyyy" )#</cfoutput>
32 </td>
33

34

<td>

34


<td>

35

36
<!--- Display the current time --->
37
<cfoutput>#timeFormat( now(), "hh:mm:ss tt" )#</cfoutput>
38

</td>

38


</td>

39
</tr>
40

41
<tr>
42

<td colspan
=

"
2
"
>

42


<td colspan
2
>

43

44 <!--- Display the output of now() --->
45 <cfoutput>Date/Time: #now()#</cfoutput>
46 <
/
td>
/
47 </tr>
48 </table>
49 </body>
50 </html>
©
2004 Prentice Hall, Inc.
All rights reserved.

©
2004 Prentice Hall, Inc. All rights reserved.
Outline
Outline
GtdXHTML
1 <?xml version = "1.0"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4

G
enera
t
e
d

XHTML

code
(1 of 2)
5
<!-- XHTML comment tag -->
6

7
<html xmlns = "http://www.w3.org/1999/xhtml">
8
<head>
9

titl
A Sil CldFi El
/titl

9


<
titl
e>
A Si
mp
l
e
C
o
ldF
us
i
on
E
xamp
l
e<
/titl
e>

10

11
<style type = "text/css">
12 p { font-size: 14pt; color: blue }
13

td

{

background
color
:

black
;

color
:

yellow

}

13


td

{

background
-
color
:

black
;

color
:

yellow

}

14 </style>
15 </head>
16

17

<body>

17


<body>

18

19 <p><strong>A Simple ColdFusion Example</strong></p>
20

21

<table border
=

"
1
"
>

21


<table border
1
>

22 <tr>
23 <td>
24 July 24, 2003
25 </td>
©
2004 Prentice Hall, Inc.
All rights reserved.

Outline
Outline
GtdXHTML
26

27
<td>
28
01:56:33 PM
29 </td>
30

/

G
enera
t
e
d

XHTML

code
(2 of 2)
30


<
/
tr>

31

32
<tr>
33 <td colspan = "2">
34

Date/Time: {ts '2003
07
24 13:56:33'}

34


Date/Time: {ts '2003
-
07
-
24 13:56:33'}

35
</td>
36 </tr>
37 </table>
38

</body>

38


</body>

39
</html>

©
2004 Prentice Hall, Inc.
All rights reserved.
27.3Using Variables and Expressions
•CFML also supports creating and manipulating
variables
variables
©
2004 Prentice Hall, Inc. All rights reserved.
Outline
Outline
iblf
1 <?xml version = "1.0"?>
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4

var
i
a
bl
es.c
f
m
(1 of 2)
5
<!--- Fig. 27.3 : variables.cfm --->
6
<!--- Expressions using Variables and Operators. --->
7

8 <html xmlns = "http://www.w3.org/1999/xhtml">
9

hd

9


<
h
ea
d
>

10 <title>Expressions using Variables and Operators</title>
11

12 <style type = "text/css">
13

p
{
font
family
:
arial sans
serif
;

13


p
{
font
-
family
:
arial
,
sans
-
serif
;

14 font-size: 12pt; color: blue }
15 </style>
16 </head>
17
17


18
<body>
19

20
<!--- Define variables using cfset --->
21
<
c
f
set
fir
stSt
rin
g
= "
O
n
e
">

21

cset
stStg
Oe
22
<cfset secondString = "Four">
23 <cfset firstNumber = 1>
24 <cfset secondNumber = 4>
25

©
2004 Prentice Hall, Inc.
All rights reserved.

Outline
Outline
iblf
26 <!--- Concatenate two strings --->
27 <cfset combinedStrings = firstString & secondString>
28

29 <!--- Add two numbers --->
30

f
dddb

fib

db

var
i
a
bl
es.c
f
m
(2 of 2)
30


<c
f
set

a
dd
e
d
Num
b
ers

=

fi
rstNum
b
er

+

secon
d
Num
b
er>

31

32
<p><strong>Expressions using Variables and Operators</strong></p>
33

34
<p>

34

<p>

35 <cfoutput>
36 Concatenation Example: #combinedStrings#
37 <br />
38

Addition Example:
#
addedNumbers
#

38


Addition Example:
#
addedNumbers
#

39 </cfoutput>
40 </p>
41

42

</body>

42


</body>

43 </html>

©
2004 Prentice Hall, Inc.
All rights reserved.
©
2004 Prentice Hall, Inc. All rights reserved.
27.3.1Variables and Data Types
•Variables much like variables in most other
languages
languages
•cfset

Does not
p
roduce an
y
out
p
ut
pyp
–No close tag
©
2004 Prentice Hall, Inc. All rights reserved.
27.3.1Variables and Data Types
Data type

Description
Array Indexed group of elements (one to three dimensions).
Boolean True or false.
Date/Time Any date or time.
List A string with text values and delimiters.
Nb
Ailb
N
um
b
er
A
n
y

i
nte
g
er or rea
l
num
b
er.
Query Format similar to a database table with named
columns and numbered rows.
String
Textenclosedineithersingle(
’’
)ordouble(
""
)
String

Text

enclosed

in

either

single

(
)

or

double

(
)

quotes.
Structure Group of associated data using key-value pairs.
Fig274
ColdFusiondatatypes
Fig
.
27
.
4

ColdFusion

data

types
.

©
2004 Prentice Hall, Inc. All rights reserved.
27.3.2Expressions and Operators
Operator
Type
Operator
Type
+
unary positive
-
unary negative
^
exponent
*
multiplication
/

division
/

division
\
integer division
MOD
modulus
+
addition
-
subtraction
&

concatenation
&

concatenation
EQ
equal
NEQ
not equal
LT
less than
LTE
less than or equal
GT

greaterthan
GT

greater

than
GTE
greater than or equal
CONTAINS
contains
DOES NOT
CONTAIN
does not contain
N
O
T
not
O
not
AND
and
OR
or
XOR
exclusive or
EQV
equivalence
IMP

implication
©
2004 Prentice Hall, Inc. All rights reserved.
IMP

implication
Fig. 27.5

ColdFusion operator precedence.

27.4Variable Scoping
•Scope

Definesfromwherethevariablecanbeaccessed

Defines

from

where

the

variable

can

be

accessed
–Refers to the named space where the variable is stored
•cfif
ta
g
g
–Behaves just like
if
statements in other languages
–cfelseif
–cdelse
•isDefined
function
Rfl

R
eturns true or
f
a
l
se
•NOT
operator
Reversesthetruthorfalsityofentireexpression
©
2004 Prentice Hall, Inc. All rights reserved.

Reverses

the

truth

or

falsity

of

entire

expression
27.4Variable Scoping
Scope prefix

Prefix required

References
variables
No Local variable
form

No
Form
form

No
Form

url
No URL
request
Yes HTTP request
cgi

No
CGIvariable
cgi

No
CGI

variable
cookie
No Cookie
client
No Application
session
YesA
pp
lication
pp
application
Yes Application
server
Yes Server
flash
Yes Macromedia Flash
queryname

NoQuery
Fig. 27.6

ColdFusion variable scopes.

©
2004 Prentice Hall, Inc. All rights reserved.
Outline
Outline
f
1 <?xml version = "1.0"?>
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4

name.c
f
m
(1 of 2)
5
<!--- Fig. 27.7 : name.cfm --->
6
<!--- Using url scope variables --->
7

8 <html xmlns = "http://www.w3.org/1999/xhtml">
9

hd

9


<
h
ea
d
>

10 <title>Hello</title>
11

12 <style type = "text/css">
13

p
{
font
family
:
arial sans
serif
;

13


p
{
font
-
family
:
arial
,
sans
-
serif
;

14 font-size: 14pt; color: navy }
15 </style>
16 </head>
17
17


18
<body>
19

20
<!--- IF Control Structure --->
21
<
c
fif N
O
T i
s
D
e
fin
ed(
"
u
rl.n
a
m
e
"

)
>

21

cO
seed(
uae
)
22

23 <!--- Create default if name variable is not in the URL --->
24 <cfset url.name = "Guest">
25 </cfif>

©
2004 Prentice Hall, Inc.
All rights reserved.

Outline
Outline
f
26

27
<!--- Display URL Variable --->
28
<p>Hello <cfoutput>#url.name#</cfoutput></p>
29
</body>
30
/hl

name.c
f
m
(2 of 2)
30

<
/h
tm
l
>


©
2004 Prentice Hall, Inc.
All rights reserved.
27.5Form Processing
•form
variable scope

Gatherinformationfromtheuser

Gather

information

from

the

user
•isNumeric
function

Similarto
isDefined
function
Similar

to

isDefined
function
–Returns boolean value
–Returns true if parameter is number

False otherwise
©
2004 Prentice Hall, Inc. All rights reserved.
Outline
Outline
fhtl
1 <?xml version = "1.0"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4

f
orm.
ht
m
l
(1 of 2)
5 <!-- Fig. 27.8 : form.html -->
6 <!-- A simple auction bid form -->
7

8 <html xmlns = "http://www.w3.org/1999/xhtml">
hd
9 <
h
ea
d
>
10
<title>Simple Auction</title>
11

12 <style type = "text/css">
13

{
f
fil

il
if

13


p
{
f
ont-
f
am
il
y:

ar
i
a
l
,

sans-ser
if
;

14 font-size: 14pt; color: navy }
15 </style>
16 </head>
17
17


18 <body>
19

20 <!-- Form variables created using post method -->
21

<form action
"
formprocesscfm
"
method
"
post
"
>

21


<form action
=

form
_
process
.
cfm
method
=

post
>

22 <p>Enter your bid:</p>
23

24 <p>
25
©
2004 Prentice Hall, Inc.
All rights reserved.
25



Outline
Outline
fhtl
26 <!--- name attribute becomes variable name --->
27
$<input name = "bidPrice" type = "text" size = "20" />
28 <input type = "submit" value = "Enter" />
29 </p>
30

/f

f
orm.
ht
m
l
(2 of 2)
30


<
/f
orm>

31
</body>
32 </html>

©
2004 Prentice Hall, Inc.
All rights reserved.
Outline
Outline
ff
1 <?xml version = "1.0"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4

f
orm_process.c
f
m
(1 of 2)
5 <!--- Fig. 27.9 : form_process.cfm --->
6 <!--- A simple auction bid processing form --->
7

8 <html xmlns = "http://www.w3.org/1999/xhtml">
hd
9 <
h
ea
d
>
10
<title>Simple Auction</title>
11

12 <style type = "text/css">
13

{
f
fil

il
if

13


p
{
f
ont-
f
am
il
y:

ar
i
a
l
,

sans-ser
if
;

14 font-size: 14pt; color: navy }
15 </style>
16 </head>
17
17


18 <body>
19

20 <!--- Determine if bidPrice exists --->
21

<cfif
isDefined(

"
formbidPrice
"

)
>

21


<cfif
isDefined(

form
.
bidPrice

)
>

22

23 <!--- Determine if bidPrice is a number --->
24 <cfif isNumeric( form.bidPrice )>
25
©
2004 Prentice Hall, Inc.
All rights reserved.
25



Outline
Outline
ff
26 <!--- Determine is bidPrice is GTE 0 --->
27 <cfif form.bidPrice GTE 0>
28

29 <!--- Display bid from form field --->
30


f
orm_process.c
f
m
(2 of 2)
30


<p>

31
Your bid is:
32
<cfoutput>$#form.bidPrice#</cfoutput>
33
</p>
34

<cfelse>


34


<cfelse>


35 <p>Your bid must be greater then or equal to $0.00!</p>
36 </cfif>
37

38

<c
felse>


38


<c
felse>


39 <p>Your bid must be a valid numeric dollar amount!</p>
40 </cfif>
41

42

<cfelse>


42


<cfelse>


43
<p>Error, no bid found.</p>
44
</cfif>
45

46 <
/
bod
y
>
/y
47 </html>

©
2004 Prentice Hall, Inc.
All rights reserved.
©
2004 Prentice Hall, Inc. All rights reserved.
27.6Creating a Data Source Name
©
2004 Prentice Hall, Inc. All rights reserved.
Fig. 27.10Data source registration in the ColdFusion Administrator.
27.7Bookstore Case Study: Interface and
Database
•cfquery
tag
cflocation
tag

cflocation
tag
–Redirects the user to another CFML template

cfabort
tag

cfabort
tag
–Instructs CFML interpreter to stop and not finish processing
page
•cfloop
tag
–Outputs the code between its opening and closing tags once
©
2004 Prentice Hall, Inc. All rights reserved.
Outline
Outline
bklitf
1 <?xml version = "1.0"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4

b
oo
kli
s
t
.c
f
m
(1 of 3)
5 <!--- Fig. 27.11 : booklist.cfm --->
6 <!--- Bookstore - List of Books --->
7

8 <html xmlns = "http://www.w3.org/1999/xhtml">
hd
9 <
h
ea
d
>
10 <title>Book List</title>
11

12 <style type = "text/css">
13

{
li

}

13


p
{
text-a
li
gn:

center
}

14 h1 { text-align: center }
15 </style>
16 </head>
17
17


18 <body>
19

20 <!--- CFML Query using SQL on 'books' Data Source Name --->
21

<cfquery name
"
bookList
"
datasource
"
books
"
>

21


<cfquery name
=

bookList
datasource
=

books
>

22 SELECT ISBN, Title, EditionNumber
23 FROM Titles
24 ORDER BY Title ASC, EditionNumber DESC
25

</cfquery>


©
2004 Prentice Hall, Inc.
All rights reserved.
25


</cfquery>



Outline
Outline
bklitf
26

27 <h1>Available Books</h1>
28

29
<hr />
30
b
oo
kli
s
t
.c
f
m
(2 of 3)
30


31 <p>Select a book from this list and click the button to
32 view the selected book's information.</p>
33

34

<form action
"bookinfocfm"
method

"post"
>

34


<form action
=

"bookinfo
.
cfm"
method

=

"post"
>

35 <p>
36

37
<!--- Dynamically build select box from database --->
38

<select name =
"
ISBN
"
size =
"
5
"
>

38


<select name =
ISBN
size =
5
>

39

40 <!--- Looping results of query using cfoutput --->
41 <cfoutput query = "bookList">
42

<option value
=

"
#
bookList.ISBN
#
"
>
#
bookList.Title
#

42


<option value
#
bookList.ISBN
#
>
#
bookList.Title
#

43
(Edition #bookList.EditionNumber#)</option>
44
</cfoutput>
45
</select>
46 <
/p
>
/p
47


©
2004 Prentice Hall, Inc.
All rights reserved.
Outline
Outline
bklitf
48
<p><input type = "submit" value = "View Information" /></p>
49
</form>
50
</body>
51
</html>
b
oo
kli
s
t
.c
f
m
(3 of 3)

©
2004 Prentice Hall, Inc.
All rights reserved.
Outline
Outline
bkiff
1 <?xml version = "1.0"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4

b
oo
ki
n
f
o.c
f
m
(1 of 5)
5 <!--- Fig. 27.12 : bookinfo.cfm --->
6 <!--- Bookstore - Detailed Book Information --->
7

8 <html xmlns = "http://www.w3.org/1999/xhtml">
hd
9 <
h
ea
d
>
10
<title>Book Info</title>
11

12 <style type = "text/css">
13

bl
{
bd
ll

ll
}

13


ta
bl
e
{
b
or
d
er-co
ll
apse:

co
ll
apse
}

14 td { border: 2px solid lightgray;
15 padding: 5px }
16 </style>
17

/head

17


<
/head
>

18

19 <body>
20

21

<!
Make sure that an ISBN was submitted to this page
>

21


<!
---
Make sure that an ISBN was submitted to this page
---
>

22 <cfif NOT isDefined( "form.ISBN" )>
23

24 <!--- CFML URL Forwarder --->
25

<cflocation url =
"
booklistcfm
"
addtoken =
"
no
"
>

©
2004 Prentice Hall, Inc.
All rights reserved.
25


<cflocation url =
booklist
.
cfm
addtoken =
no
>


Outline
Outline
bkiff
26 <cfabort>
27
</cfif>
28

29
<!--- CFML Query using SQL on 'books' DSN --->
b
oo
ki
n
f
o.c
f
m
(2 of 5)
30 <!--- Dynamically insert form.ISBN variable --->

31
<cfquery name = "bookInfo" datasource = "books">
32 SELECT ISBN, Title, EditionNumber, Copyright, Description,
33
ImageFile, PublisherName
34

il blih


34


FROM

T
i
t
l
es,

Pu
bli
s
h
ers


35
WHERE Titles.ISBN = '#form.ISBN#'
36 AND Titles.PublisherID = Publishers.PublisherID
37
</cfquery>
38
38


39 <!--- Using cfoutput with a query to loop result set --->
40 <cfoutput query = "bookInfo">
41

42

<!
Based on ISBN use SQL to retrieve authors
>

42


<!
---
Based on ISBN
,
use SQL to retrieve authors
---
>

43 <cfquery name = "authorInfo" datasource = "books">
44 SELECT FirstName, LastName
45 FROM AuthorISBN, Authors
46

WHERE AuthorISBNisbn =
'
#
bookInfoISBN
#
'

46


WHERE AuthorISBN
.
isbn =
#
bookInfo
.
ISBN
#

47 AND AuthorISBN.AuthorID = Authors.AuthorID
48 </cfquery>
49

50
<h1>
#
bookInfoTitle
#
</h1>

©
2004 Prentice Hall, Inc.
All rights reserved.
50

<h1>
#
bookInfo
.
Title
#
</h1>


Outline
Outline
bkiff
51

52 <hr />
53

54 <p>
b
oo
ki
n
f
o.c
f
m
(3 of 5)
55

56
<!--- Display authors by using a CFML Loop --->
57
<cfloop query = "authorInfo">
58 #authorInfo.FirstName# #authorInfo.LastName# <br />
59

</cfloop>


59


</cfloop>


60 </p>
61

62 <!---Output information from database about book --->
63
<table>

63

<table>

64 <tr>
65 <td>ISBN:</td>
66
<td>#bookInfo.ISBN#</td>
67

<td rowspan
="
4
"
>

67


<td rowspan
4
>

68

69
<!--- Dynamically insert the filename into the tag --->
70 <img src = "images/#bookInfo.ImageFile#"
71 alt = "Ima
g
e File"
/
>



g
/
72 </td>
73 </tr>
74


©
2004 Prentice Hall, Inc.
All rights reserved.
Outline
Outline
bkiff
75
<tr>
76
<td>Edition:</td>
77
<td>#bookInfo.EditionNumber#</td>
78
</tr>
79
b
oo
ki
n
f
o.c
f
m
(4 of 5)
79


80
<tr>
81
<td>Copyright:</td>
82
<td>#bookInfo.Copyright#</td>
83

</tr>

83


</tr>

84

85
<tr>
86
<td>Publisher:</td>
87

<td>
#
bookInfoPublisherName
#
</td>

87


<td>
#
bookInfo
.
PublisherName
#
</td>

88
</tr>
89
</table>
90
<p>#bookInfo.Description#</p>
91

91

92
<!--- Used in Section 27.8 --->
93
<!--- Button to add book to shopping cart --->
94
<form action = "shoppingcart.cfm" method = "post">
95
<
p
>
p
96
<input type = "hidden" name = "ISBN"
97
value = "#bookInfo.ISBN#" />
98


©
2004 Prentice Hall, Inc.
All rights reserved.
Outline
Outline
bkiff
99 <input type = "hidden" name = "title"
100 value = "#bookInfo.Title#" />
101

102
<input type = "hidden" name = "edition"
103

l
"
#
bkfdiib
#
"
/

b
oo
ki
n
f
o.c
f
m
(5 of 5)
103


va
l
ue

=

"
#
b
oo
k
In
f
o.E
di
t
i
onNum
b
er
#
"
/
>

104

105 <input type = "submit" value = "Add To Cart"
106 disabled = "disabled" />
107

</p>

107


</p>

108 </form>
109
</cfoutput>
110

111

<!
---
Button to go back to the book list
---
>

111


<!
---
Button to go back to the book list
---
>

112 <form action = "booklist.cfm" method = "post">
113 <p>
114 <input type = "submit" value = "Book List" />
115

</p>

115


</p>

116
</form>
117
</body>
118 </html>

©
2004 Prentice Hall, Inc.
All rights reserved.
©
2004 Prentice Hall, Inc. All rights reserved.
27.8Bookstore Case Study: Shopping Cart
•cfapplication
tag

Accessthevariablescopesrelatedtoapplications

Access

the

variable

scopes

related

to

applications
–client
variables
•Can be stored as cookies on user’s computer or in server’s
i
reg
i
stery
–session
variables
•Store contents of user’s sho
pp
in
g
cart as user makes selections
ppg
–application
variables
©
2004 Prentice Hall, Inc. All rights reserved.
Outline
Outline
Alitif
1
<!--- Fig. 27.13 : Application.cfm --->
2
<!--- Bookstore - Application Framework --->
3

4
<!--- Initialize the application framework --->
5
fliti
"bkSt"


A
pp
li
ca
ti
on.c
f
m
(1 of 1)
5

<c
f
app
li
ca
ti
on

name

=

"b
oo
kSt
ore
"


6
clientmanagement = "Yes"
7
sessionmanagement = "Yes"
8
sessiontimeout = "#createTimeSpan( 0,0,20,0 )#"
9

applicationtimeout =
"
#
createTimeSpan( 2000
)
#
"

9


applicationtimeout =
#
createTimeSpan( 2
,
0
,
0
,
0
)
#

10
clientstorage = "cookie">
11

12
<!--- Make sure the shopping cart exists or create one --->
13
<cfif NOT isDefined
(
"session.sho
pp
in
g
Cart"

)
>

(
ppg
)
14
<cfset session.shoppingCart = arrayNew( 2 )>
15
</cfif>

©
2004 Prentice Hall, Inc.
All rights reserved.
Outline
Outline
hitf
1 <?xml version = "1.0"?>
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4

5
!
i 2714 hif

s
h
opp
i
ngcar
t
.c
f
m
(1 of 3)
5
<
!
---

F
i
g.
27
.
14
:

s
h
opp
i
ngcart.c
f
m

--->

6
<!--- Bookstore - Simple Shopping Cart--->
7

8 <cfif isDefined( "form.ISBN" )>
9
9


10 <!--- Add a new book to the shopping cart. --->
11 <cfif arrayAppend( session.shoppingCart[ 1 ], form.ISBN )></cfif>
12 <cfif arrayAppend( session.shoppingCart[ 2 ], form.title )></cfif>
13

<cfif
arrayAppend(

session.shoppingCart[

3

], form.edition )
></cfif>

13


<cfif
arrayAppend(

session.shoppingCart[

3

], form.edition )
></cfif>

14 <cfelseif isDefined( "form.clearCart" )>
15

16 <!--- Empty the shopping cart. --->
17
<cfif arrayClear( session.shoppingCart )></cfif>
18 </cfif>
19

20 <html xmlns = "http://www.w3.org/1999/xhtml">
21 <head>
22 <title>Bookstore Shopping Cart</title>
23

24
<style type = "text/css">
25 table { border-collapse: collapse }
©
2004 Prentice Hall, Inc.
All rights reserved.

Outline
Outline
hitf
26 td { border: 2px solid lightgray;
27 padding: 5px }
28 </style>
29 </head>
30
s
h
opp
i
ngcar
t
.c
f
m
(2 of 3)
30


31 <body>
32
<h1>Shopping Cart</h1>
33

34

<!
Check the size of the shopping cart
>

34


<!
---
Check the size of the shopping cart
---
>

35
<cfset arraySize = arrayLen( session.shoppingCart[ 1 ] )>
36

37
<cfif arraySize GTE 1>
38

<table>

38


<table>

39

40 <!--- Display the items in the shopping cart --->
41 <cfloop index="i" from="1" to="#arraySize#" step="1">
42

<cfoutput>


42


<cfoutput>


43 <tr>
44 <td>#session.shoppingCart[ 1 ][ i ]#</td>
45 <td>#session.shoppingCart[ 2 ][ i ]#</td>
46 <td>Edition #session.sho
pp
in
g
Cart
[

3

]
[

i
]
#<
/
td>

ppg[
]
[
]
/
47 </tr>
48 </cfoutput>
49 </cfloop>
50 </table>
©
2004 Prentice Hall, Inc.
All rights reserved.

Outline
Outline
hitf
51 <cfelse>
52 <p>Empty Shopping Cart.</p>
53 </cfif>
54

55

!
bk h bk li

s
h
opp
i
ngcar
t
.c
f
m
(3 of 3)
55


<
!
---

Button
b
ac
k
to

t
h
e
b
oo
k li
st

--->

56
<form action = "booklist.cfm" method = "post">
57 <p><input type = "submit" value = "Book List" /></p>
58 </form>
59
59


60 <!--- Button to clear the shopping cart --->
61 <form action = "shoppingcart.cfm" method = "post">
62 <p>
63

<input name =
"
clearCart
"
type =
"
submit
"


63


<input name =
clearCart
type =
submit


64
value = "Empty Cart" />
65
</p>
66
</form>
67

</body>

67


</body>

68 </html>

©
2004 Prentice Hall, Inc.
All rights reserved.
©
2004 Prentice Hall, Inc. All rights reserved.
27.9Advanced Topics for ColdFusion
Develo
p
ers
p
•Ability to create custom tags and user-defined
functions
functions
•Set of built-in CFML tags for working with XML
andforinteractingwithWebservices
and

for

interacting

with

Web

services
©
2004 Prentice Hall, Inc. All rights reserved.
27.10Web Resources
•www.macromedia.com/software/coldfusion

www.macromedia.com/support/coldfusion/documentation.html
www.macromedia.com/support/coldfusion/documentation.html
•www.macromedia.com/cfusion/exchange/index.cfm?view=sn130
•www.forta.com
•www.sys-con.com/coldfusion
©
2004 Prentice Hall, Inc. All rights reserved.