ColdFusion

 <  Day Day Up  >  


Another popular server-scripting solution is Macromedia's ColdFusion (www.macromedia.com). Like PHP, ColdFusion focuses on helping developers build dynamic, database-driven Web site applications with an easy-to-use, server-side markup language similar to HTML. Getting started with ColdFusion requires learning a few new markup tags that look like HTML but make up what is called ColdFusion Markup Language (CFML). Because one of its primary functions is database access, ColdFusion uses the Open Database Connectivity (ODBC) standard to connect to popular database servers such as Microsoft SQL Server, Access, Sybase, Oracle, and others. ColdFusion is not dependent on a particular database or Web server, and it works well on a variety of Windows-based servers as well as Solaris and Linux. While ColdFusion is not open source nor a W3C-defined standard, it is widely used. It is presented here to illustrate another example of parsed HTML and demonstrate a simple database-driven page.

Like PHP, Web applications built with ColdFusion use dynamic pages composed of a mixture of code and HTML markup, but in the case of ColdFusion, the code is represented in tags. When the page is requested , the ColdFusion application running on the server preprocesses the page, interacts with a database or other server-side technologies, and returns a dynamically generated HTML page. It probably is better to refer to ColdFusion-enabled pages as templates because the actual page output varies.

The following discusses how to use CFML to select and output data in a dynamic Web page. This section shows how to use a number of CFML tags to query data from a database, take the results of the query, and populate a Web page.

A database is simply a collection of data that is organized in a regular fashion, typically in the form of a table. Imagine you want to create a Web site to post the various job openings in your company. The first thing you need to do is decide what information is relevant: position number, job title, location, brief job description, hiring manager, and posting date. This information could be organized in the form of a database table, called Positions , as shown in Table 13-4.

Table 13-4: Simple Database Table Called Positions

PositionNum

JobTitle

Location

Description

Hiring Manager

PostDate

343

Gadget Sales

Austin

This position requires an aggressive salesperson to sell gadgets to guys and gals.

M. Spacely

01/20/00

525

Office Manager

San Jose

Responsible for running the entire office single-handedly.

P. Mohta

01/24/00

2585

President

San Diego

Figurehead position, requires daily golf games and nightly poker parties.

T. Powell

01/30/00

3950

Groundskeeper

San Diego

Must like outdoor work and long hours in the sun with no sunscreen.

J. Tam

01/30/00

1275

HTML Hacker

Seattle

Must be able to recite HTML specifications by heart and code HTML by hand. Long hours, low pay.

D. Whitworth

01/27/00

2015

Game Tester

Los Angeles

Must be able to play games all day long; poor posture and junk food diet essential.

J. Daffyd

01/18/00

The example is populated with some simple data, but how can the data be retrieved to be displayed in a Web page automatically?

The first step is to define a database query using Structured Query Language (SQL). SQL is the language used to retrieve or modify data from the tables in a database. The language is relatively simple, at least as far as mastering the basics. If you were interested in making a query to the database table called Positions, you would use a SQL statement like this:

 SELECT * FROM Positions 

This query simply says to select all items indicated by the wildcard ( * ) in the table called Positions. If you want only to list all the positions in Austin, you could qualify the query by adding a WHERE modifier, indicating you only want entries for which the location is Austin.

 SELECT * FROM Positions WHERE Location="Austin" 

Using the WHERE modifier, it is possible to create complex queries. For example, you could query all jobs in Austin, or in Los Angeles, where the position is Game Tester:

 SELECT *       FROM Positions       WHERE ((Location="Austin" OR               (Location="Los Angeles") AND              (Position="Game Tester")) 

This brief discussion should reveal the flavor of SQL. Although the basic language is simple, queries can be more complicated. A full discussion of SQL is well beyond the scope of this book. For the sake of this discussion, only simple queries are used in the examples.

To pull data out of the database, write a SQL query, and then place it within a <CFQUERY> element. The following example illustrates the use of <CFQUERY> . A select SQL query called ListJobs , as specified by the NAME attribute, will query a database and retrieve all the records in the Positions table. The syntax for this example is shown here:

  <CFQUERY NAME="ListJobs"   DATASOURCE="CompanyDataBase">  SELECT * FROM Positions  </CFQUERY>  

Notice that the DATASOURCE attribute is set equal to CompanyDataBase, which is the ODBC data source that contains a database called Company, which contains the Positions table from which data is pulled.

Note  

Open Database Connectivity (ODBC) is a standardized way to access data from a variety of different databases. ODBC provides a layer of abstraction that protects the developer from having to learn the particulars of a specific database system. To query the Positions table, your server might connect to a simple Microsoft Access database or a powerful Oracle system. To access a database, a developer needs to set up an ODBC data source. This requires that developer to select an ODBC driver, name the data source, and configure any specific settings for the database. A complete discussion of how to set up ODBC drivers and configure data sources can be found in the documentation for ColdFusion.

Aside from NAME and DATASOURCE , the <CFQUERY> element has a variety of attributes, but these are all that are necessary for most simple queries.

Using the <CFOUTPUT> tag, it is possible to display the data retrieved from a previously defined <CFQUERY> tag. For example, in order to output the query called ListJobs, you would use a code fragment, as shown here:

  <CFOUTPUT QUERY="ListJobs">   <hr noshade="noshade"><br />  Position Number: #PositionNum#  <br /><br />  Title: #JobTitle#  <br /><br />  Location: #Location#  <br /><br />  Description: #Description#  </CFOUTPUT>  

Notice the use of the # symbols throughout this code fragment. These values are used to delimit the areas in which you wish to place the data from the database. For example, #PositionNum# will be populated with data from the column PositionNum , while #JobTitle# will get the values for the JobTitle column in the database. Notice also that normal HTML markup can be used within the query.

By putting both the <CFQUERY> and the <CFOUTPUT> tags together in a complete CFML template file, which you could call listjobs.cfm, and putting this on a server that understands ColdFusion, you could create a dynamically generated page. The following is a complete example showing the two primary ColdFusion elements:

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   <html xmlns="http://www.w3.org/1999/xhtml" lang="en">   <!-- SQL statement to select jobs available from the database-->   <CFQUERY NAME="ListJobs" DATASOURCE="CompanyDataBase">  SELECT * from Positions  </CFQUERY>   <head>   <title>  Demo Company Job Listings  </title>   <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />   </head>   <body>   <h2 align="center">  Job Listings  </h2>   <hr />   <CFOUTPUT QUERY="ListJobs">   <hr noshade="noshade"><br />  Position Number: #PositionNum#  <br /><br />  Title: #JobTitle#  <br /><br />  Location: #Location#  <br /><br />  Description: #Description#  </CFOUTPUT>   <hr />   <address>  Demo Company, Inc. &copy; 2003  </address>   </body>   </html>  

Figure 13-5 shows a ColdFusion dynamically generated page under Netscape. Of course, like any server-side language, there are generally no browser-side requirements. In other words, this application would work equally well under Internet Explorer, Lynx, MSN TV, or any other browser.

click to expand
Figure 13-5: ColdFusion output under Netscape

The preceding discussion is just a sample of what ColdFusion can do. It is meant only to illustrate what a server-side HTML language can do. For more detailed information on the syntax of ColdFusion, as well as examples of its use, see the ColdFusion Language Reference at Macromedia (www.macromedia.com/software/coldfusion). Whereas ColdFusion seems somewhat specific to database access, there are other server-side parsed HTML solutions, such as Microsoft's ASP, which might appear to provide more general functionality.



 <  Day Day Up  >  


HTML & XHTML
HTML & XHTML: The Complete Reference (Osborne Complete Reference Series)
ISBN: 007222942X
EAN: 2147483647
Year: 2003
Pages: 252
Authors: Thomas Powell

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net