I have a web site called MyNextPet.org. I want to have a
featured pet on the
home page that is Random
I am attempting to write a function that does this:
Selects the total pets in the database ('Pets') and generate
a RecordCount
Create a variable that is random from 1 to the RecordCount of
'Pets'
Attempt to select information about that pet based on the
generated number
(that must match the auto incremented 'pettag' number)
If the number does not match any of the pets, loop through an
do it again
If the number does match, stop and output the information
selected
I winged this function so I am sure I did something wrong and
the fact that
I am posting it proves that I did. I am a little (or a lot)
confused on how
to break out of a loop if the condition is met.
Help!
Code I have:
<cffunction name="GetFeaturedPet" access="public"
returntype="query">
<cfquery name="Pets" datasource="#Request.MainDSN#">
SELECT * FROM pets
</cfquery>
<cfloop>
<cfset featured = RandRange(1,#Pets.RecordCount#)>
<cfquery name="GetFeatured"
datasource="#Request.MainDSN#">
SELECT P.name, p.age, p.gender, p.breed, R.org
FROM pets P LEFT OUTER JOIN rescues R
ON p.username = R.username
WHERE pettag = #featured# AND active = 1
</cfquery>
<cfif #GetFeatured.RecordCount# EQ 0>
</cfloop>
</cfif>
<cfelse>
<cfabort>
<cfreturn GetFeatured>
</cffunction>
--
Wally Kolcz
Developer / Support