Grouping Result Output - macromedia COLDFUSION MX 7 Web Application Construction Kit [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

macromedia COLDFUSION MX 7 Web Application Construction Kit [Electronic resources] - نسخه متنی

Ben Forta, Raymond Camden, Leon Chalnick, Angela Buraglia

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید





  • Grouping Result Output


    Before a new level of complexity is introduced, let's review how ColdFusion processes queries.

    In ColdFusion, data queries are created using the <cfquery> tag. <cfquery> performs a SQL operation and retrieves results if any exist. Results are stored temporarily by ColdFusion and remain only for the duration of the processing of the template that contained the query.

    The <cfoutput> tag is used to output query results. <cfoutput> takes a query name as an attribute and then loops through all the rows that were retrieved by the query. The code block between <cfoutput> and </cfoutput> is repeated once for each and every row retrieved.

    All the examples created until now displayed results in a single list or single table.

    What would you do if you wanted to process the results in subsets? For example, suppose you wanted to list movies by rating. You could change the SQL statement in the <cfquery> to retrieve the rating ID and set the sort order to be RatingID and then by MovieTitle.

    This would retrieve the data in the correct order, but how would you display it? If you used <cfoutput> as you have until now, every row created by the <cfoutput> block would have to be the same. If one had the rating displayed, all would have to because every row that is processed is processed with the same block of code.

    Look at Figure 10.11. As you can see, the screen contains nested lists. The top-level list contains the rating IDs, and within each rating ID is a second list containing all the movies with that rating. How would you create an output like this?

    Figure 10.11. Grouping lets you display data grouped into logical sets.

    [View full size image]

    Listing 10.9 contains the code for a new page; save this as ratings1.cfm and execute it in your browser.

    Listing 10.9. ratings1.cfmGrouping Query Output


    <!---
    Name: ratings1.cfm
    Author: Ben Forta (ben@forta.com)
    Description: Query output grouping
    Created: 12/15/04
    --->
    <!--- Get movie list from database --->
    <cfquery name="movies" datasource="ows">
    SELECT MovieTitle, RatingID
    FROM Films
    ORDER BY RatingID, MovieTitle
    </cfquery>
    <!--- Creat260 page --->
    &l275>
    <head>
    <title>Orange Whip Studios - Movie List</title>
    </head>
    <body>
    <h2>Movie List</h2>
    <!--- Display movie list --->
    <ul>
    <!--- Loop through ratings --->
    <cfoutput query="movies" group="RatingID">
    <li>#RatingID#</li>
    <ul>
    <!--- For each rating, list movies --->
    <cfoutput>
    <li>#MovieTitle#</li>
    </cfoutput>
    </ul>
    </cfoutput>
    </ul>
    </body>
    </html>

    Figure 10.12). Save Listing 10.10 as ratings2.cfm, and then execute it in your browser.

    Listing 10.10. ratings2.cfmGrouping Query Output


    <!---
    Name: ratings2.cfm
    Author: Ben Forta (ben@forta.com)
    Description: Query output grouping
    Created: 12/15/04
    --->
    <!--- Get movie list from database --->
    <cfquery name="movies" datasource="ows">
    SELECT MovieTitle, RatingID
    FROM Films
    ORDER BY RatingID, MovieTitle
    </cfquery>
    <!--- Creat260 page --->
    &l275>
    <head>
    <title>Orange Whip Studios - Movie List</title>
    </head>
    <body>
    <h2>Movie List</h2>
    <!--- Display movie list --->
    <table>
    <!--- Loop through ratings --->
    <cfoutput query="movies" group="RatingID">
    <tr valign="top">
    <td>Rating #RatingID#</td>
    <td>
    <!--- For each rating, list movies --->
    <cfoutput>
    #MovieTitle#<br>
    </cfoutput>
    </td>
    </tr>
    </cfoutput>
    </table>
    </body>
    </html>

    Figure 10.12. Grouped data can be used in lists, tables, and any other form of data presentation.

    RatingID new table row is created containing two cells. The left cell contains the RatingID, and the right cell contains the movies.

    To do this, the inner <cfoutput> loop is used in that right cell (between the <TD> and </td> tags) so that, for each RatingID listed on the left, all the appropriate movies are listed on the right.

    TIP

    A single level of grouping is used here, but there is no limit to the number of levels in which data can be grouped. To group multiple levels (groups within groups), you simply need an additional <cfoutput> per group (and of course, the SQL statement must sort the data appropriately).

  • / 281