Solved

How can I change this code to show random items?

Posted on 2002-07-19
5
180 Views
Last Modified: 2013-12-24
I found this code at CFM-Resources.com and it has no email address to ask the author of this code.  What it does is display only 3 in a row and then go to the next row and show 3 more items and so on so forth.

The only thing I would like it to do is pull random items from the database so that the page looks different every time somone visits it..  Here is the code:

<cfquery name="GetAllBooks" datasource="MyDataSource">
SELECT *
FROM BookTable
</cfquery>

<cfset tmp1 = #GetAllBooks.RecordCount#>

<!--- TMP2 change the number after the division symbol "/" to reflect the number of times you want something to display horizontally --->

<cfset tmp2 = #tmp1#/3>

<cfset tmp3 = #Round(tmp2)#+1>

<CFSET RandID = Randomize(second(now()))>
<!---now get a random number--->
<CFSET RandID = RandRange(0, getProducts.recordcount - 1) + 1>

<cfset startpoint = "1">

<!--- ENDPOINT change the value of endpoint to match the number of times you want something to display horizontally --->

<cfset endpoint = "3">

<table border="0" cellspacing="5" cellpadding="5">

<cfloop index="x" from="1" to="#tmp3#">

<tr>
<cfloop query="GetAllBooks" startrow="#startpoint#" endrow="#endpoint#">
<td>
<cfoutput>
<a href="index3.cfm?Trg1=Trg1&d1=#RecId#">
<img src="media/#ThmbPict#" border="0" alt="#Title#">
</a>
</cfoutput>
</td>
</cfloop>
</tr>

<cfset startpoint = #endpoint#+1>

<!--- ENDPOINT make this iteration of the "endpoint" variable one less then the number you set TMP2 to be --->

<cfset endpoint = #startpoint#+2>

</cfloop>

</table>
0
Comment
Question by:jeffmace
  • 2
  • 2
5 Comments
 
LVL 17

Expert Comment

by:anandkp
Comment Utility
Hi jeff,

this is pretty easy.

all u gotta know is the total count of the records in the database. lets say totalcount is [20]

once u know that ... do the following

decide how many records u wanna display in ur page.
lets say [5].
so

<Cfset maxlimit = totalcount-5>
<cfset newnum = RandRange(1,newnum)>

now this newnum could be ne-value b/w 1 & 15.

So u set ur
startpoint = newnum
endpoint= newnum+5

<cfloop query="GetAllBooks" startrow="#startpoint#" endrow="#endpoint#">
<td>
<cfoutput>
<a href="index3.cfm?Trg1=Trg1&d1=#RecId#">
<img src="media/#ThmbPict#" border="0" alt="#Title#">
</a>
</cfoutput>
</td>
</cfloop>


let me know,

K'Rgds
Anand
0
 

Author Comment

by:jeffmace
Comment Utility
Actually you lost me and something doesn't seem right in your code with the newnum...  Take a look at this code.. I forgot to delete some code that i added in there before.. If you can, can you just copy and paste the code i have and insert your code where it should go.

thanks...



<cfquery name="GetAllBooks" datasource="MyDataSource">
SELECT *
FROM BookTable
</cfquery>

<cfset tmp1 = #GetAllBooks.RecordCount#>

<!--- TMP2 change the number after the division symbol "/" to reflect the number of times you want something to display horizontally --->

<cfset tmp2 = #tmp1#/3>

<cfset tmp3 = #Round(tmp2)#+1>

<cfset startpoint = "1">

<!--- ENDPOINT change the value of endpoint to match the number of times you want something to display horizontally --->

<cfset endpoint = "3">

<table border="0" cellspacing="5" cellpadding="5">

<cfloop index="x" from="1" to="#tmp3#">

<tr>
<cfloop query="GetAllBooks" startrow="#startpoint#" endrow="#endpoint#">
<td>
<cfoutput>
<a href="index3.cfm?Trg1=Trg1&d1=#RecId#">
<img src="media/#ThmbPict#" border="0" alt="#Title#">
</a>
</cfoutput>
</td>
</cfloop>
</tr>

<cfset startpoint = #endpoint#+1>

<!--- ENDPOINT make this iteration of the "endpoint" variable one less then the number you set TMP2 to be --->

<cfset endpoint = #startpoint#+2>

</cfloop>

</table>
0
 
LVL 17

Accepted Solution

by:
anandkp earned 200 total points
Comment Utility
Hi there,

use the following as it is & see the result

===========================================================

<!--- Anand K Patel --->

<CFQUERY NAME="GetAllBooks" DATASOURCE="MyDataSource">
SELECT * FROM BookTable
</CFQUERY>

<CFSET TMP1 = #GETALLBOOKS.RECORDCOUNT#>
<CFSET rec_on_page = 5>

<Cfset maxlimit = tmp1-rec_on_page><!--- to avoid over flow in loop --->
<cfset tmp1 = RandRange(1,maxlimit)><!--- loops starts ne-where b/w 1 & 15 --->
<CFSET tmp2 = tmp1+rec_on_page><!--- & will go on till next 5 values  [so that it never overflows the recordcount ]--->


<CFSET STARTPOINT = "1">
<CFSET ENDPOINT = "3"><!--- ENDPOINT change the value of endpoint to match the number of times you want something to display horizontally --->

<TABLE BORDER="0" CELLSPACING="5" CELLPADDING="5">

<CFLOOP INDEX="x" FROM="#tmp1#" TO="#tmp2#"><!--- Check above the values r changed for tmp2 !!! --->

<TR>
<CFLOOP QUERY="GetAllBooks" STARTROW="#startpoint#" ENDROW="#endpoint#">
<TD>
<CFOUTPUT>
<A HREF="index3.cfm?Trg1=Trg1&d1=#RecId#">
<IMG SRC="media/#ThmbPict#" BORDER="0" ALT="#Title#">
</A>
</CFOUTPUT>
</TD>
</CFLOOP>
</TR>

<CFSET STARTPOINT = #ENDPOINT#+1>

<!--- ENDPOINT make this iteration of the "endpoint" variable one less then the number you set TMP2 to be --->

<CFSET ENDPOINT = #STARTPOINT#+2>

</CFLOOP>

</TABLE>  

K'Rgds
Anand
0
 
LVL 11

Expert Comment

by:jimmy282
Comment Utility
Well,
If its SQlServer

U can do a simple thing

Select *
FROM BookTable
ORDER BY NEWID()

Thats it!

Jimmy
0
 

Author Comment

by:jeffmace
Comment Utility
Works great... and the answer from jimmy works as well.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now