OutFront Forums
     Home    Register     Search      Help      Login    

Recent Posts
Todays Posts
Most Active posts
Posts since last visit
My Recent Posts
Mark posts read

Sponsors
Frontpage website templates
Creative Website Templates for FrontPage, Dreamweaver, Flash, SwishMax
Shopping Cart Software
Ecommerce software integrated into Frontpage, Dreamweaver and Golive templates. No monthly fees and available in ASP and PHP versions.
Website Templates
We also have a wide selection of Dreamweaver, Expression Web and Frontpage templates as well as webmaster tools and CSS layouts.

 

Trouble randomizing db results in a row

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
Users viewing this topic: none
Printable Version 

All Forums >> Web Development >> ASP, PHP, and Database >> Trouble randomizing db results in a row
Page: [1]
 
jonance

 

Posts: 316
From: Louisville KY USA
Status: offline

 
Trouble randomizing db results in a row - 1/25/2010 23:21:33   
Hi,

I have used the forums to help me get pictures pulled from my database to appear in a row across the page. However, i would like these results to be randomly ordered each time the page is loaded and am having trouble doing so. Can anyone help? Here is the code:


  <center><table>





<!--#include file="_fpclass/fpdblib.inc"-->
<% if 0 then %>
<SCRIPT Language="JavaScript">
document.write("<div style='background: yellow; color: black;'>The Database Results component on this page is unable to display database content. The page must have a filename ending in '.asp', and the web must be hosted on a server that supports Active Server Pages.</div>");
</SCRIPT>
<% end if %>
<%
fp_sQry="SELECT * FROM listings ORDER BY RND(ID)"
fp_sDefault=""
fp_sNoRecords="No records returned."
fp_sDataConn="louisvilleguide"
fp_iMaxRecords=256
fp_iCommandType=1
fp_iPageSize=0
fp_fTableFormat=False
fp_fMenuFormat=False
fp_sMenuChoice=""
fp_sMenuValue=""
fp_iDisplayCols=1
fp_fCustomQuery=False
BOTID=0
fp_iRegion=BOTID
%>
<!--#include file="_fpclass/fpdbrgn1.inc"-->

<%If x = 0 then %>
<tr>
<%end if%>
<%x=x+1%>
<TD>    <a href="businessdetail.asp?ID=<%=FP_FieldVal(fp_rs,"ID")%>"><img border="0" src="banners/<%=FP_FieldVal(fp_rs,"name")%>"></a>
<% If x < 4 then %>
</td>
<%Else
x = 0%>
</td></tr>
<%end if%>


<!--#include file="_fpclass/fpdbrgn2.inc"-->

</table>
<!--webbot bot="PurpleText" PREVIEW="Code" -->


Spooky

 

Posts: 26630
Joined: 11/11/1998
From: Middle Earth
Status: offline

 
RE: Trouble randomizing db results in a row - 1/26/2010 1:35:42   
Try it this way :

"SELECT ....... ORDER BY NewID()"

_____________________________

If you arent part of the solution, then there is good money to be made prolonging the problem

Sp:)ky


(in reply to jonance)
jonance

 

Posts: 316
From: Louisville KY USA
Status: offline

 
RE: Trouble randomizing db results in a row - 1/26/2010 11:56:24   
Thanks, but this is the error i got.....

Database Results Error
Description: Undefined function 'NewID' in expression.
Number: -2147217900 (0x80040E14)
Source: Microsoft JET Database Engine

(in reply to Spooky)
bobby

 

Posts: 11901
Joined: 8/15/1969
From: Seattle WA USA
Status: offline

 
RE: Trouble randomizing db results in a row - 1/26/2010 14:09:53   
This help?

http://forums.aspfree.com/microsoft-access-help-18/select-a-random-record-from-the-database-21982.html


Not sure how well the RND function works with DRW...

_____________________________

Talk to your kittens about catnip, or someone else will.


:)

(in reply to jonance)
jonance

 

Posts: 316
From: Louisville KY USA
Status: offline

 
RE: Trouble randomizing db results in a row - 1/26/2010 14:46:35   
Thanks...the only problem is i want them all to display in random order....not display just one random result. Anybody know why i'm getting that error?

(in reply to bobby)
bobby

 

Posts: 11901
Joined: 8/15/1969
From: Seattle WA USA
Status: offline

 
RE: Trouble randomizing db results in a row - 1/26/2010 15:51:57   
Do you have a field in your database called NewID? If not, error

You may have to randomize each of the four images... or jump a random number of records three times after the first..?


_____________________________

Talk to your kittens about catnip, or someone else will.


:)

(in reply to jonance)
jonance

 

Posts: 316
From: Louisville KY USA
Status: offline

 
RE: Trouble randomizing db results in a row - 1/26/2010 17:59:03   
I'm trying this now...but still not random

<%
' Initialize ASP RND() function
Randomize()
intRandomNumber = Int (1000*Rnd)+1
fp_sQry= _
"SELECT TOP 1000 ID, realname, name, Rnd(" & -1 * (intRandomNumber) & "*ID)" & _
"FROM listings " & _
"ORDER BY RND()"
fp_sDefault=""
fp_sNoRecords="No records returned."
fp_sDataConn="louisvilleguide"
fp_iMaxRecords=256
fp_iCommandType=1
fp_iPageSize=0
fp_fTableFormat=False
fp_fMenuFormat=False
fp_sMenuChoice=""
fp_sMenuValue=""
fp_iDisplayCols=1
fp_fCustomQuery=False
BOTID=0
fp_iRegion=BOTID
%>

(in reply to bobby)
bobby

 

Posts: 11901
Joined: 8/15/1969
From: Seattle WA USA
Status: offline

 
RE: Trouble randomizing db results in a row - 1/26/2010 18:21:15   
intRandomNumber = Int (1000*Rnd)+1

do you have 1000 records in the database? If not, try changing that number to the total number of records and see if it works?


_____________________________

Talk to your kittens about catnip, or someone else will.


:)

(in reply to jonance)
jonance

 

Posts: 316
From: Louisville KY USA
Status: offline

 
RE: Trouble randomizing db results in a row - 1/26/2010 18:49:52   
No there are 6 but more will be added so i wanted to cover them too.....i changed to 6 to no avail...also ORDER BY was originally 3 from the code snippet. I'll post my code and the snippet below


<%
' Initialize ASP RND() function
Randomize()
intRandomNumber = Int (6*Rnd)+1
fp_sQry= _
   "SELECT TOP 6 ID, realname, name, Rnd(" & -1 * (intRandomNumber) & "*ID)" & _
   "FROM listings " & _
   "ORDER BY 3"
fp_sDefault=""
fp_sNoRecords="No records returned."
fp_sDataConn="louisvilleguide"
fp_iMaxRecords=256
fp_iCommandType=1
fp_iPageSize=0
fp_fTableFormat=False
fp_fMenuFormat=False
fp_sMenuChoice=""
fp_sMenuValue=""
fp_iDisplayCols=1
fp_fCustomQuery=False
BOTID=0
fp_iRegion=BOTID
%>





Microsoft Access
For Microsoft Access we can return a set of one or more random records by using the RND() function to order the resultset. The only difficulty is that the RND() seed is cached resulting in the same ordered set of results if we use RND() directly in the SQL statement. 

To get around this we pass in a different value to the RND() function each time, forcing Access to use a new seed when generating the random number. Assume that we have a table called: Table1 and this contains fields: TableID and Field1 

<%
' Initialize ASP RND() function
Randomize()
intRandomNumber = Int (1000*Rnd)+1

' Return 3 random records
strSQL = _
   "SELECT TOP 3 TableID, Field1, Rnd(" & -1 * (intRandomNumber) & "*TableID)" & _
   "FROM Table1 " & _
   "ORDER BY 3"

Set objRS = objConn.Execute(strSQL)
%> 



(in reply to bobby)
TexasWebDevelopers

 

Posts: 846
Joined: 2/22/2002
From: Dallas, TX
Status: offline

 
RE: Trouble randomizing db results in a row - 1/27/2010 14:37:06   
Here's how we do it:
http://www.texaswebdevelopers.com/examples/retrieverandomrecords.asp

_____________________________

:)

Follow us on TWITTER

(in reply to jonance)
bobby

 

Posts: 11901
Joined: 8/15/1969
From: Seattle WA USA
Status: offline

 
RE: Trouble randomizing db results in a row - 1/27/2010 19:06:35   
slick as usual, TWD

:)

_____________________________

Talk to your kittens about catnip, or someone else will.


:)

(in reply to TexasWebDevelopers)
jonance

 

Posts: 316
From: Louisville KY USA
Status: offline

 
RE: Trouble randomizing db results in a row - 1/29/2010 12:39:35   
Thanks...this is as close as i've come. How can i show ALL records with room to grow? I tried entering like 10k and it showed all but they didn't randomize when refreshed. I feel i'm soooo close...thanks again!

(in reply to bobby)
Spooky

 

Posts: 26630
Joined: 11/11/1998
From: Middle Earth
Status: offline

 
RE: Trouble randomizing db results in a row - 1/29/2010 14:24:54   
Typically NewId() works with the MICROSOFT.JET.OLEDB.4.0 provider without the extra code. The DRW by default uses the older drivers which dont support it

_____________________________

If you arent part of the solution, then there is good money to be made prolonging the problem

Sp:)ky


(in reply to jonance)
jonance

 

Posts: 316
From: Louisville KY USA
Status: offline

 
RE: Trouble randomizing db results in a row - 1/29/2010 14:42:56   
Hey Spooky, do you have a code sample that uses the new drivers? I don't know much about the drivers...does that have anything to do with the version of access or just how it is called in the code?

(in reply to Spooky)
Spooky

 

Posts: 26630
Joined: 11/11/1998
From: Middle Earth
Status: offline

 
RE: Trouble randomizing db results in a row - 1/29/2010 14:47:47   
Heres an example of what the DRW does :

http://www.frontpagewebmaster.com/m-176449/key-oledb%252Cdrivers/tm.htm#176462

_____________________________

If you arent part of the solution, then there is good money to be made prolonging the problem

Sp:)ky


(in reply to jonance)
jonance

 

Posts: 316
From: Louisville KY USA
Status: offline

 
RE: Trouble randomizing db results in a row - 2/2/2010 13:40:08   
Ok...i changed to the new code in global.asa, but i get this error now:

Database Results Error
Description: Undefined function 'NewID' in expression.
Number: -2147217900 (0x80040E14)
Source: Microsoft JET Database Engine

(in reply to Spooky)
Page:   [1]

All Forums >> Web Development >> ASP, PHP, and Database >> Trouble randomizing db results in a row
Page: [1]
Jump to: 1





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts