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

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>

主站蜘蛛池模板: 名山县| 嘉峪关市| 阿鲁科尔沁旗| 康定县| 浙江省| 宁武县| 漳浦县| 万年县| 广汉市| 米脂县| 贡嘎县| 阜新市| 衡阳市| 玉门市| 潼南县| 克东县| 元氏县| 永善县| 微山县| 唐海县| 南郑县| 福贡县| 西畴县| 麟游县| 张掖市| 闸北区| 长寿区| 罗城| 于田县| 驻马店市| 宜宾市| 深水埗区| 伊春市| 东山县| 阿巴嘎旗| 建昌县| 桑日县| 余江县| 金阳县| 普格县| 明水县|