官术网_书友最值得收藏!

Returning data from the CFC

Lastly we return the rsReturn variable to the caller. You will note that this has changed the variable type to a ColdFusion query which is the name for the returned recordset from<cfquery>.

Now we need to create a calling page. We have to see what is coming back from the database and how it looks inside ColdFusion. Here is the code to our calling page.

<!--- Example: 2_4.cfm --->
<!--- Processing --->
<cfscript>
objProduct = createObject("component","product_2").init();
rsProducts = objProduct.getRecordset();
</cfscript>
<!--- Content --->
<cfdump var="#rsProducts#">

We see something new in the recordset dump since ColdFusion version 8. This version includes the attributes for cached, execution time, and the SQL that was run to produce the query. These can be very helpful for development, debugging, and logging. You will also see that the result set displays each row in a table. Each of the fields returned from the database are columns in the recordset. The number to the left of the table is the row of the recordset returned; it is not the index of the database. That is something you should note. Good database tables should always have a primary index key to refer to the individual record at a later time. You will find we use a field called ID in this database table. Yes, we only have five records in our database, if you are wondering. This is to keep things as simple as possible.

Now we need to be able to use this recordset in more places than<cfdump>. Let's take a look at how this is done. We will use a number of examples to show you how to retrieve data back from your recordset. In the first example, we will use<COUTPUT> and present the results in an unordered list. We simply add an attribute called query and set it equal to the recordset we retrieved from our object instance. Here is the code for this example:

<!--- Example: 2_5.cfm --->
<!--- Processing --->
<cfscript>
objProduct = createObject("component","product_2").init();
rsProducts = objProduct.getRecordset();
</cfscript>
<!--- Content --->
<ul><cfoutput query="rsProducts">
<li>#rsProducts.name#</li></cfoutput>
</ul>

Now we are going to modify the code so it gets linked back and we can return an individual record. We will put them on the same page, but how you do this may change depending on what you are doing. This is just an example. Let's look at the modified code. We will just change the code from example 2_5 this time. Add or modify the highlighted rows to the code:

<!--- Example: 2_5.cfm --->
<!--- Processing --->
<cfparam name="url.id" default="">
<cfscript>
objProduct = createObject("component","product_2").init();
rsProducts = objProduct.getRecordset();
rsProduct = objProduct.getRecordset(where = "id = #url.id#");
</cfscript>
<!--- Content --->
<ul><cfoutput query="rsProducts">
<li> <a href="?id=#rsProducts.id#">#rsProducts.name#</a> </li></cfoutput>
</ul>
<cfif rsProduct.recordCount EQ 1> <cfoutput><table> <tr> <th>Product</th> <td>#rsProduct.name#</td> </tr> <tr> <th>Description</th> <td>#rsProduct.description#</td> </tr> <tr> <th>Price</th> <td>#dollarFormat(rsProduct.price)#</td> </tr> </table></cfoutput> </cfif>

主站蜘蛛池模板: 靖江市| 专栏| 莱阳市| 顺平县| 巴塘县| 宁陵县| 杭州市| 梓潼县| 方城县| 嘉禾县| 呼伦贝尔市| 安顺市| 海宁市| 苍溪县| 开化县| 延长县| 云霄县| 临泉县| 徐闻县| 页游| 合肥市| 乡城县| 吉木萨尔县| 湛江市| 库尔勒市| 温州市| 林甸县| 宜丰县| 揭阳市| 留坝县| 宜宾县| 环江| 磐石市| 财经| 九龙城区| 改则县| 巫溪县| 加查县| 天全县| 信阳市| 武川县|