Welcome to Dream.In.Code
Become an Expert!

Join 137,221 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,144 people online right now. Registration is fast and FREE... Join Now!




Random Db Records

 
Reply to this topicStart new topic

> Random Db Records, Display random records from a database

supernova333
****



post 20 Dec, 2004 - 01:59 PM
Post #1


This script will enable you to select a predetermined number of random records from a database without knowing how many total records are in the database and making sure not to display the same record twice. Here's the code, I'll explain a little more below.


CODE

<cfquery datasource="DATASOURCE_HERE" name="getItems">
SQL_QUERY_HERE
</cfquery>
<!--- make sure there are records available --->

<cfif getItems.recordCount>
 <!--- number of records you want to show --->
 <cfset showNum = 10>
 <!--- make sure we aren't trying to show
  more than what's in the DB --->
 <cfif showNum gt getItems.recordCount>
   <cfset showNum = getItems.recordCount>
 </cfif>
 <!--- make a list --->
 <cfset itemList = "">
 <cfloop from="1" to="#getItems.recordCount#" index="i">
   <cfset itemList = ListAppend(itemList, i)>
 </cfloop>
 <!--- randomize the list --->
 <cfset randomItems = "">
 <cfset itemCount = ListLen(itemList)>
 <cfloop from="1" to="#itemCount#" index="i">
   <cfset random = ListGetAt(itemList, RandRange(1, itemCount))>
   <cfset randomItems = ListAppend(randomItems, random)>
   <cfset itemList = ListDeleteAt(itemList, ListFind(itemList, random))>
   <cfset itemCount = ListLen(itemList)>
 </cfloop>
 <!--- display stuff here --->
 <cfloop from="1" to="#showNum#" index="i">
   <cfoutput>#getItems.COLUMN_NAME[ListGetAt(randomItems, i)]#</cfoutput><br>
 </cfloop>
</cfif>


All you need to do is plug in your info for DATASOURCE_HERE, SQL_QUERY_HERE, and COLUMN_NAME. Right now this will display up to 10 records, you can change that by changing the "showNum" variable.

Ok, say you want to display 5 banners from the banners table. Pretend this is your SQL query:
CODE

SELECT url, image
FROM Banners


You would need to set the total number of banners like so:
CODE

<cfset showNum = 5>


Assuming you want to display the banners in a column on the page, your loop under the "display stuff here" comment would look like this:
CODE

 <!--- display stuff here --->
 <cfloop from="1" to="#showNum#" index="i">
   <cfoutput>
   <a href="#getItems.url[ListGetAt(randomItems, i)]#" target="_blank"><img src="/imagepath/#getItems.image[ListGetAt(randomItems, i)]#" border="0"></a>
   </cfoutput>
   <br>
 </cfloop>


This is a really basic example; you could add a lot more to the loop like tracking queries, more formatting, etc.

Feel free to email me know if you have any questions.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

Solitarium
*



post 18 Nov, 2005 - 05:48 PM
Post #2
lifesaver man... sheer lifesaver music.gif
Go to the top of the page
+Quote Post

UnholyStar
Group Icon



post 24 Jan, 2007 - 12:22 PM
Post #3
alright so its a year and 2 months old, i though it was a useful article. So Mr.Author if you ever visit this forum, this was a very helpful article, since i am beggining to learn Coldfusion.
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 12/4/08 02:16PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month