Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 5 IBS 520. ColdFusion Variables CF uses variables to store data in memory. There are many different types of variables; each has its own.

Similar presentations


Presentation on theme: "Week 5 IBS 520. ColdFusion Variables CF uses variables to store data in memory. There are many different types of variables; each has its own."— Presentation transcript:

1 Week 5 IBS 520

2

3

4

5

6 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”

7 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

8 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. LocalQueryForm URLSessionCookie

9 Creating Local Variables with The 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: 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: In the example above we are assigning literal values to the variable. All literal values are surrounded by double quotes

10 Creating Local Variables with In this example, the variable draws its value from two other variables: 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

11 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. As you can see by the following example you can perform arithmetic operations on a variable that is set as a string.

12 Displaying Variable Output Use the tag to bracket a block of code for CF processing. CF preprocesses all code that falls between and 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 and tags. The sysntax for the tag is: [CF processing instructions] Here is the example of defining variables with the tag outside the code block: #variables.fullname# The output of this code is: Teddy Bear

13 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.

14 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?

15 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

16 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.

17 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.

18 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.

19 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.

20 Publishing Database Content The tag wraps around the SQL statement. This returns the result set to CF. Select distributor. Distributor_ID, Distributor. Distributor_Name From Distributor Order By Distributor. Distributor_ID

21 DATASOURCE Using the datasource attribute, the tag will use the connection parameters set up in the administrator. This is the only required attribute to Select distributor. Distributor_ID, Distributor. Distributor_Name From Distributor Order By Distributor. Distributor_ID

22 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. Select distributor. Distributor_ID, Distributor. Distributor_Name From Distributor Order By Distributor. Distributor_ID As a naming convention you may want to name all your queries with a prefix of “q”.

23 Displaying Database Data Use the tag to: –Output dynamic variable values –Loop through and output a result set –Use to display the values returned by a query. Bearing in mind what you learned about displaying the values of variables. –Consider the following code:

24 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.

25 RecordCount RecordCount contains the total number of records returned by the query. Number of rows returned: #queryname.recordcount#

26 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. Row#queryname.currentrow# of #queryname.recordcount#

27 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

28

29

30 CFQUERY

31

32

33

34

35

36

37

38 It is a very simple way to reuse code. You put the common code in a separate file and call it using the tag. When Coldfusion encounters this tag searches for the included page. Brings it inline in the processing page and continues processing. The syntax for this tag is You can create common code in a template and use the 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.

39 Uses of the tag: Including toolbars on each page Including common headers and footers on each page Including any reusable content.

40 Mypage.cfm … … Template.cfm Included text here Mypage.cfm …. Included text here ….

41 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.

42 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.

43 Displaying Database Data Select distributor. Distributor_ID, Distributor. Distributor_Name From Distributor Order By Distributor. Distributor_ID # Distributor_ID # #Distributor_Name# 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. # q_getdist.Distributor_ID # # q_getdist.Distributor_Name# 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 tag. The syntax is ……. The QUERY attribute changes the 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 tag to put each row on a different line # q_getdist.Distributor_ID # # q_getdist.Distributor_Name#


Download ppt "Week 5 IBS 520. ColdFusion Variables CF uses variables to store data in memory. There are many different types of variables; each has its own."

Similar presentations


Ads by Google