Outputting Information from a Query

I l @ ve RuBoard

After you have retrieved information from a database, you usually want to display that data. How it is presented is up to you. You can use whatever type of HTML formatting you like when outputting information from a database query; you are not stuck with a proprietary ColdFusion output look and feel. When you output information that was returned from the database, ColdFusion will loop over each record and output them one at a time, and any formatting that you have specified with these records will also be output each time. You can make the records bold, or italic, change the font or size , or put each one in a separate table cell .

  1. In firstquery.cfm, after the output comment, type:

     <cfoutput query="productlist"> 

    The <cfoutput> tag, when used with a specific query name , tells ColdFusion to output the results of a query. The query attribute specifies which query to output. The query attribute in <cfoutput> is the same as the name attribute in <cfquery> . This is how ColdFusion knows which query to output information from. In this exercise, the value of both of those attributes is productlist. You will be outputting the query you have already constructed .

    TIP

    Keep your query names as descriptive and short as possible. It will make it easier for you to remember what you were doing with the query.

    NOTE

    When you use <cfoutput> with a query specified, it works like a loop; it executes the code between the two <cfoutput> tags for each record that is returned in the query.

  2. After the <cfoutput> tag, type:

     <strong>#ProductName#<strong><br>  #ProductDesc#<br> Price: #DollarFormat(ProductPrice)# <br><br> 

    The variables that you are outputting are the ones that were returned from the productlist query. They have the same names as the fields in the database. The variables for product name, product description, and product price have been output. You will also notice that there is HTML formatting to control the line breaks and fonts.

  3. Type </cfoutput> after the variable names.

    When outputting the results of a query, the <cfoutput> tag loops over each row that was returned. (In other words, it reads each record separately.) So for each record from the database, the same code (whether it is ColdFusion or HTML) is going to be processed . The only difference is going to be the values of the variables. Knowing this, you could construct different ways to display your information: in tables, using lists, or in whatever manner you like.

    NOTE

    There are two variables that are built into any query: recordcount and currentrow. Recordcount lets you know how many records were returned with your query. Currentrow carries the number of the row that you are currently on when looping over query results. For example, if you were on the second record in the loop, the currentrow variable would contain the number 2.

  4. Choose File > Save As or Ctrl+Shift+S and navigate to the directory Inetpub\ wwwroot \Lesson5\. Make sure the File name field says firstquery.cfm and click Save.

  5. Open a Web browser and enter the following URL into the address window: http://127.0.0.1/Lesson5/firstquery.cfm .

    When you open the window, you should see a list of all the products in the database. If you have not added any more items, you should see four products. Congratulationsyou have now successfully completed your first query and output that information.

I l @ ve RuBoard


Macromedia ColdFusion 5. Training from the Source
Macromedia ColdFusion 5 Training from the Source (With CD-ROM)
ISBN: 0201758474
EAN: 2147483647
Year: 2002
Pages: 99
Authors: Kevin Schmidt

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