Solved

How can I change this code to show random items?

Posted on 2002-07-19
5
200 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
ID: 7164954
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
ID: 7165052
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
ID: 7166273
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
ID: 7166315
Well,
If its SQlServer

U can do a simple thing

Select *
FROM BookTable
ORDER BY NEWID()

Thats it!

Jimmy
0
 

Author Comment

by:jeffmace
ID: 7167369
Works great... and the answer from jimmy works as well.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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