Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I change this code to show random items?

Posted on 2002-07-19
5
Medium Priority
?
225 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 800 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

715 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