This content has been marked as final. Show 4 replies
You don't need a loop and you only need one trip to the database.
Start by running your 2nd query with two modifications.
1. put pettag in the select clause.
2. take pettag out of the where clause.
Then do that randrange thing on this query.
Then use listgetat to get the petttag you want.
Then do a query of queries to get your final answer.
Thank you for the things to look into.
I am a little confused with the QofQ.
I just want one random record from the database to display on the front page
of the web site.
I just need to make sure that both the record number exists (unless the pet
was deleted) and that the 'active =1' (if the pet hasn't either been adopted
If you start with the 2nd query, slightly modified of course, you have already determined that the record exists and that active = 1.
Try this modified function. The first query will pull all active pets from your db. The second QoQ ("getPet") will pull one random pet from the first query.
<cffunction name="GetFeaturedPet" access="public" returntype="query">
<cfquery name="GetFeatured" datasource="#Request.MainDSN#">
SELECT p.pettag, P.name, p.age, p.gender, p.breed, R.org
FROM pets P LEFT OUTER JOIN rescues R
ON p.username = R.username
WHERE active = 1;
<cfset featured = RandRange(1,#GetFeatured.RecordCount#)>
<cfquery name="getPet" dbtype="query">
SELECT * FROM GetFeatured WHERE pettag = #featured#