I wrote a routine to grab random non-consecutive image names from a database. I won't
go into detail over the code now, but the steps are as follows:
1. Choose a random number R.
2. Find the minimum distance between any ID number and R - using MIN(ABS(ID-R)) in SQL
3. Choose a single item whose ID number is this distance from R.
It all fits in a single query with a subquery.
Alexander Lamon wrote:
> Robert,
>
> Are the primary keys you refer to below consecutive *and* contiguous? If
> so, you can do something like this:
>
> <!--- Get number fo records in the table --->
> <cfquery name="get_count datasource="someData">
> select article from someTable;
> </cfquery>
>
> <!--- Grab a random number from this record count -->
> <cfset randomID = RandRange(1,#get_count.recordcount#)>
>
> <!--- Then grab the article with that matches this randomly chosen ID --->
> <cfquery name="get_art" datasource="someData">
> select article from someTable
> where ID = #randomID#;
> </cfquery>
>
> *However*, if you have gaps in the numbers like 1,2,3,7, this won't work
> for obvious reasons.
>
> Hope this helps,
>
> Alec Lamon
> IT Manager
> Wharton Direct
>
> > Ok here is my problem, I have a database that has news articles. Now what
> >my boss wants me to do is make it so the first feature story you see on the
> >page is different each time you come on. It can be random and it can loop
> >over once you have seen all the feature articles. The problem is the only
> >unique field is the autonumber field so the feature articles could be
> >15,18,34,48,78. How would I be able to make it so each article is different.
> >Anyone have something they have already done that I could use or modify?
> >
> >Robert Everland III
> >Network Administrator
> >Orlando.com
> >
> >--------------------------------------------------------------------------
> >----
> >Archives: http://www.eGroups.com/list/cf-talk
> >To Unsubscribe visit
> >http://www.houseoffusion.com/index.cfm?sidebar=lists&body=lists/cf_talk or
> >send a message to [EMAIL PROTECTED] with 'unsubscribe' in
> >the body.
> ------------------------------------------------------------------------------
> Archives: http://www.eGroups.com/list/cf-talk
> To Unsubscribe visit
>http://www.houseoffusion.com/index.cfm?sidebar=lists&body=lists/cf_talk or send a
>message to [EMAIL PROTECTED] with 'unsubscribe' in the body.
------------------------------------------------------------------------------
Archives: http://www.eGroups.com/list/cf-talk
To Unsubscribe visit
http://www.houseoffusion.com/index.cfm?sidebar=lists&body=lists/cf_talk or send a
message to [EMAIL PROTECTED] with 'unsubscribe' in the body.