1 / 43

Week 5

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

keon
Download Presentation

Week 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 5 IBS 520

  2. 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”

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

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

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

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

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

  8. Displaying Variable Output Use 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

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

  10. 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?

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

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

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

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

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

  16. 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_Name From Distributor Order By Distributor. Distributor_ID </CFQUERY>

  17. 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_Name From Distributor Order By Distributor. Distributor_ID </CFQUERY>

  18. 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_Name From Distributor Order By Distributor. Distributor_ID </CFQUERY> As a naming convention you may want to name all your queries with a prefix of “q”.

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

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

  21. RecordCount RecordCount contains the total number of records returned by the query. Number of rows returned: <cfoutput> #queryname.recordcount# </cfoutput>

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

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

  24. CFQUERY

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

  26. Uses of the <CFINCLUDE> tag: • Including toolbars on each page • Including common headers and footers on each page • Including any reusable content.

  27. <CFINCLUDE TEMPLATE=“TEMPLATE.CFM”> <HTML> … <cfinclude template=“Template.cfm”> … </HTML> <Table> <tr><td> Included text here </td></tr> Mypage.cfm Template.cfm <HTML> …. <Table> <tr><td> Included text here </td></tr> …. </HTML> Mypage.cfm

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

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

  30. Displaying Database Data <CFQUERY datasource=“OWS’ Name=“q_getlist”> Select distributor. Distributor_ID, Distributor. Distributor_Name From Distributor Order 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>

More Related