week 2 ibs 685. project site upload assignments to your project site that you will create for ibs...

48
Week 2 IBS 685

Post on 22-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

Week 2

IBS 685

Page 2: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

Project Site

• Upload assignments to your project site that you will create for IBS 685

Page 3: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685
Page 4: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685
Page 5: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685
Page 6: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685
Page 7: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

ColdFusion Variables

• CF uses variables to store data in memory. • There are many different types of variables;

each has its own use. • To use a variable, name it and assign it a

value.• When working with variables, you can

assign a specific value, such as “7” or “Sue”

Page 8: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

Variable Naming

• All CF variables must be named using the following rules:– Use one word– Do not use spaces– Begin with a letter– Use only letters, numbers and underscore– Do not use special characters

Page 9: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

Variable Prefixes

• There are many types of variables in CF.

• Each variable has it is own scope– Where it exists

– How long it exist for

– Where the variable value is stored.

Local Query Form

URL Session Cookie

Page 10: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

Creating Local Variables with <CFSET>

The <CFSET> tag is used to initialize the value of a variable. You set the value as being equal to an expression, which can contain variables, functions and operators.

The syntax for this tag is:

<cfset variable name = expression>

The result is a variable than can be accessed on the current page only. This is known as a local variable. The scope or when a variable can be accessed is the page.

In the following examples, the variables have specific values:

<cfset FirstName = “Sue”><cfset Age = “29”>

In the example above we are assigning literal values to the variable. All literal values are surrounded by double quotes

Page 11: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

Creating Local Variables with <CFSET>

In this example, the variable draws its value from two other variables:

<cfset firstname = “Sue”><cfset lastname=“Smith”><cfset fullname = variables.firstname &” ”& variables.lastname>

In the next example, ColdFusion uses the values of the firstname and lastname variables to set the value for the fullname variable in the last line of code. The ampersand (&) string operator joins the variables, and the

space surrounded by double quotation marks (" ") adds a space between the variables. The result would be: Sue Smith

Page 12: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

Referencing Variables

To reference a local variable’s value, use the variables prefix. In this way, you can avoid confusing different components in your code and make your code easier to maintain. variables.firstname

Notice that you reference variables in the expression (right hand side of the equal sign) without using double quotes. CF assumes that non quoted values are variables and will return the value of those variables in assignment.

If you attempt to perform the following assignment, CF would attempt to convert Firstname and LastName to numeric values for addition, which would result in a runtime error.

<cfset firstname = “Sue”><cfset lastname=“Smith”><cfset fullname = variables.firstname + variables.lastname>

As you can see by the following example you can perform arithmetic operations on a variable that is set as a string.

<cfset TheNum = “40”><cfset NewNum=variables.TheNum + “25”><cfset TheString = New Number is: & variables.NewNum>

Page 13: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

Displaying Variable OutputUse the <CFOUTPUT> tag to bracket a block of code for CF processing. CF preprocesses all code that falls between

<CFOUTPUT> and </CFOUTPUT> tags and passes all other code unprocessed, to the Web Server. To output the value of a variable rather than its name:• Surround the variable name with pound signs (##)• Place the name between <CFOUTPUT> and </CFOUTPUT> tags.

The sysntax for the <CFOUTPUT> tag is:

<CFOUTPUT> [CF processing instructions] </CFOUTPUT>

Here is the example of defining variables with the <CFSET> tag outside the <CFOUTPUT> code block:

<cfset firstname = “Teddt”><cfset lastname=“Bear”><cfset Fullname = variables.firstname &” ”&variables.lastname>

<CFOUTPUT> #variables.fullname# </CFOUTPUT>

The output of this code is: Teddy Bear

Page 14: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

CF Functions

• CFML is made up of two primary language elements– Tags – perform operations such as accessing a

database, evaluating a condition– Functions- Return (possibly process) data and

do things such as getting the current date and time, converting text to uppercase, and rounding a number to its nearest integer.

Page 15: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

CF Functions

• #Now()# is an instruction telling CF to execute a function named Now()- – a function that returns the current date and time.

e.g.{ts ‘1998-10-03 22:40:23’}

• What happens – If no pound signs?– If put outside the cfoutput tag?

Page 16: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

Usage Examples

• Call a function to return a value: Function()– #Now()#– Returns {ts ‘1998-10-03 22:40:23’}

• Nesting functions:Function1(Function2(data))– DateFormat(Now())– Returns 03-Oct-97

Page 17: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

CF Functions

• Format of that date function is not entirely readable.

• Another function, DateFormat( ) can help here.

• DateFormat () role is to format dates so they are readable.

Page 18: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

CF Functions

• DateFormat is an example of a function that accepts (and requires) that data must be passed to it – after all it needs to know which date you want to format to display.

• #DateFormat(Now())# tells CF to format the date returned by the Now() function.

Page 19: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

NEST(le)

• Passing a function as a parameter to another function is referred to as NESTING.

• The Now() function is said to be NESTED in the DateFormat() function.

Page 20: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

DateFormat()

• DateFormat() takes second optional attribute: – A format mask used to describe the output format.

#DateFormat(Now(), “MMMM-DD-YY”)#

#DateFormat(Now(), “MM-DD-YY”)#

#DateFormat(Now(), “DDD,MMMM DD-YYYY”)#

Parameters passed to a function are always separated by comma.

Page 21: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

Commenting Your Code

• In order to ensure maintability of your applications, you should always comment your code in as much detail as possible.

• A better way is to document the code as you write it, using CFML comments.– You may be familiar with HTML comments:– <!- - This is an HTML comment - ->– CF comments are similar, but use three dashes instead of two:– <!- - - This is an HTML comment - - ->– CF comments are not sent back to the browser, but are processed

out and omitted from the page. In this way you can be sure that users will not be able to read your sensitive comments.

Page 22: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

Publishing Database Content

The <CFQUERY > tag wraps around the SQL statement. This returns the result set to CF.

<CFQUERY……>

Select distributor. Distributor_ID, Distributor. Distributor_NameFrom DistributorOrder By

Distributor. Distributor_ID </CFQUERY>

Page 23: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

DATASOURCE

• Using the datasource attribute, the <CFQUERY>tag will use the connection parameters set up in the administrator. This is the only required attribute to <CFQUERY>

<CFQUERY datasource=“OWS’…..>Select distributor. Distributor_ID, Distributor.

Distributor_NameFrom DistributorOrder By Distributor. Distributor_ID </CFQUERY>

Page 24: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

NAME

• The query returns a result set identified by the NAME attribute. This name will be used to point to the result set in memory after the database returns the requested data to CF.

<CFQUERY datasource=“OWS’ Name=“q_getlist”>Select distributor. Distributor_ID, Distributor. Distributor_NameFrom DistributorOrder By Distributor. Distributor_ID </CFQUERY>

As a naming convention you may want to name all your queries with a prefix of “q”.

Page 25: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

<CFQUERY> Result Sets

Page 26: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

Displaying Database Data

• Use the <CFOUTPUT> tag to:– Output dynamic variable values– Loop through and output a <CFQUERY> result

set– Use <CFOUTPUT> to display the values

returned by a query. Bearing in mind what you learned about displaying the values of variables.

– Consider the following code:

Page 27: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

Special Information Returned With Queries

• For each query you execute, CF also stores special information about the query.

• This information is stored in variables. These variables are available to you after each query and are valid until the end of the page process.

Page 28: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

RecordCount

RecordCount contains the total number of records returned by the query.

Number of rows returned:

<cfoutput>

#queryname.recordcount#

</cfoutput>

Page 29: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

CurrentRow

CF assigns a sequential number to each row returned. You can use the variable CurrentRow to print out this number. As you print out each row, you can use CurrentRow to display the current row and recordCount to display the total number of rows.

<cfoutput query =“queryname”>Row#queryname.currentrow# of #queryname.recordcount#</cfoutput>

Page 30: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

Execution Time

• Execution time displays the amount of time spent executing and returning the data generated by the query. Access this attribute by using: cfquery.ExecutionTime

Page 31: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685
Page 32: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685
Page 33: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

CFQUERY

Page 34: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685
Page 35: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685
Page 36: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685
Page 37: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685
Page 38: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685
Page 39: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685
Page 40: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685
Page 41: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

<CFINCLUDE>

• It is a very simple way to reuse code.

• You put the common code in a separate file and call it using the <cfinclude> tag.

• When Coldfusion encounters this tag <cfinclude> searches for the included page.

• Brings it inline in the processing page and continues processing.

• The syntax for this tag is <cfinclude template= “Template File Name”>

• You can create common code in a template and use the <cfinclude> tag to insert the template’s content wherever you need it.

• This can be a single line of code or an entire document.

• The application referencing the included application is referred to as a calling page.

• The included application is referred to as an included page.

Page 42: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

Uses of the <CFINCLUDE> tag:

• Including toolbars on each page

• Including common headers and footers on each page

• Including any reusable content.

Page 43: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

<CFINCLUDE TEMPLATE=“TEMPLATE.CFM”>

Mypage.cfm<HTML>…<cfinclude template=“Template.cfm”>…</HTML>

Template.cfm<Table><tr><td>Included text here</td></tr>

Mypage.cfm

<HTML>….<Table><tr><td>Included text here</td></tr>….</HTML>

Page 44: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

Typeless (Not definable)

• All CF variables are typeless, which means you do not need to identify the type of the data(Such as numbers or dates.) that the variable stores.

• Behind the scenes CF stores all the values as strings.

• When variables are accessed and expected to be of a certain data type (such as attempting to perform arithmetic operations on a variable) they are “cast” into the expected type.

Page 45: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

Referencing Variables

• To reference a local variable’s value, use the variables prefix.

• Notice that you reference variables in the expression (right hand side of the equal sign) without using double quotes.

Page 46: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

Movie List Page

Page 47: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

Displaying Database Data<CFQUERY datasource=“OWS’ Name=“q_getlist”>Select distributor. Distributor_ID, Distributor. Distributor_NameFrom DistributorOrder By Distributor. Distributor_ID </CFQUERY>

<cfoutput># Distributor_ID ##Distributor_Name# </cfoutput>

The above code will not work. To reference query results, you must specify the query result set explicitly. Do this by using Q_getdist prefix on each variable. Note that “q_getdist” is the name of the query in the above example.

<cfoutput># q_getdist.Distributor_ID ## q_getdist.Distributor_Name# </cfoutput>

The output of the above code would be: 1 Beans ‘R Us

Notice that only the first row of the result set is displayed. To show all rows returned, add the query attribute to the <CFOUTPUT> tag. The syntax is <CFOUTPUT query=“query name”> ……. </CFOUTPUT>

The QUERY attribute changes the <CFOUTPUT> into an iterative loop that will execute the number of rows returned in the assignmed query. This syntax will return all rows in the Q_getdist query.

Use an HTML <BR> tag to put each row on a different line

<cfoutput query=“q_getdist”># q_getdist.Distributor_ID ## q_getdist.Distributor_Name# </cfoutput>

Page 48: Week 2 IBS 685. Project Site Upload assignments to your project site that you will create for IBS 685

The output of the previous code would be:

1. Beans R Us2. The Buzz3. Coffee Galore4. Perk Plus5. Café Colombian6. Jumpin Java7. Coffee 20008. The whole Bean 9. Roast Resellers