cf_randomquery is a quick solution to give a random recordset. This custom tag supports any kind of query (recordset) as the datasource, not limited to specific database.
Requirements: ColdFusion 5, MX and up
Usage: <cf_randomquery DataSource="query/recordset source - mandatory" Name="The name query. The query name is used later in the page to reference the query's record set - mandatory" MaxRows="Specifies the maximum number of rows to return in the record set - optional (default all rows)">
Examples: 1. Randomize recordset from database query. <!--- query database for information ---> <cfquery name = "GetParks" dataSource = "cfsnippets"> SELECT PARKNAME, REGION, STATE FROM Parks </cfquery> <cf_randomquery DataSource="GetParks" Name="RandomParks"> <!--- RandomParks will contain all rows from GetParks in random order ---> <cf_randomquery DataSource="GetParks" Name="RandomPark" MaxRows="1"> <!--- RandomPark will contain 1 random row from GetParks --->
2. Randomize recordset from programmatically created query <cfset myQuery = QueryNew("name, address, phone")> <cfset newRow = QueryAddRow(MyQuery, 2)> <cfset temp = QuerySetCell(myQuery, "name", "Fred", 1)> <cfset temp = QuerySetCell(myQuery, "address", "9 Any Lane", 1)> <cfset temp = QuerySetCell(myQuery, "phone", "555-1212", 1)> <cfset temp = QuerySetCell(myQuery, "name", "Jane", 2)> <cfset temp = QuerySetCell(myQuery, "address", "14 My Street", 2)> <cfset temp = QuerySetCell(myQuery, "phone", "588-1444", 2)> <cf_randomquery DataSource="myQuery" Name="RandomMyQuery"> <!--- RandomMyQuery will contain all rows from myQuery in random order ---> <cf_randomquery DataSource="myQuery" Name="RandomMyQuery1" MaxRows="1"> <!--- RandomMyQuery1 will contain 1 random row from myQuery --->
Important Note: This custom tag adds one column name "cf_randomquery" to the resulting recordset. You may want to remove this column if your application manipulate data from query directly without specifying column name. So, please keep in mind that you are recommended to specify column names to manipulate data from resulting random query.
Click here for demonstration http://www.masrizal.com/index.cfm?fuseaction=idea.download_detail&ProductID=cf_randomquery
*** DISCOUNT FOR CFTAGSTORE CUSTOMERS *** Use promotion code: CFTAGSTORE to get 40% discount
|