Solved

How can I change this code to show random items?

Posted on 2002-07-19
5
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wordpress email form 23 88
ColdFusion Rereplace 3 97
Web Site Hosting 10 101
Two wordpress questions 3 108
This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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