Solved

How can I change this code to show random items?

Posted on 2002-07-19
5
213 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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 …
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

756 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