Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random SQL Query

Status
Not open for further replies.

hallm

Programmer
Jun 11, 2000
159
US
Is there any way to pull records out of a database using sql in random order.&nbsp;&nbsp;I'm constructing a ad rotator system that will display 10 links on the right side out of about 100 possible links.&nbsp;&nbsp;I want them randomly selected from the database, then I can just loop through them displaying them one at a time.&nbsp;&nbsp;If not, then how can I randomly display the links without the possiblity of it duplicating any links.&nbsp;&nbsp;The example of how this will look can be found on <A HREF=" TARGET="_new"> now it is a combination of cgi and javascript using a program called webadverts.&nbsp;&nbsp;It is an intense server drain and also is very slow to load.
 
Here's what I've done displaying random images and corresponding links.<br><br>&lt;%<br>Dim strPhysPath1,FSO1,loopcount1, iCount1, x, y, z<br>Dim jCount1,Count1,poscomma1,imgName1,href1,strImage<br>Dim tem1,tem2,stream1,tem<br>Dim dummy(),num(),line1()<br><br>'-------------Create file system object and read the contents of a file into an array------------<br><br>strPhysPath1 = server.MapPath(&quot;.&quot;) & &quot;\Img_Link_T.txt&quot;<br>Set FSO1 = CreateObject(&quot;Scripting.FileSystemObject&quot;)'server.<br>Set Stream1 = FSO1.OpenTextFile(strPhysPath1, 1)<br><br>Count1 = 0<br>redim line1(0)<br><br>Do While stream1.atendofstream &lt;&gt; true<br>line1(count1)= stream1.readline<br>redim preserve line1(ubound(line1) + 1)<br>Count1 = Count1 + 1<br>Loop<br>redim preserve line1(ubound(line1)- 1)<br>Stream1.Close<br>set Sream1 = nothing<br><br>'----------- Create dummy array to hold a sequence of numbers------------------------- <br><br>ReDim dummy(0)<br>tem1 = 0<br><br>For loopcount1 = 0 To count1 - 1<br>&nbsp;dummy(loopcount1) = tem1<br>&nbsp;ReDim Preserve dummy(loopcount1 + 1)<br>&nbsp;tem1 = tem1 + 1<br>Next<br><br>'------------Generate random numbers,sort them and eliminate duplicates--------------<br><br>ReDim num(0)<br><br>For loopcount1 = 0 To 6<br>Randomize<br>&nbsp;&nbsp;jCount1 = CInt(Rnd() * (Count1 - 1))<br>&nbsp;&nbsp;&nbsp;num(loopcount1) = jCount1<br>&nbsp;&nbsp;For x = 0 To UBound(num)<br>&nbsp;&nbsp;&nbsp;&nbsp;If loopcount1 &lt;&gt; x Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If jCount1 = num(x) Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;For y = 0 To UBound(num)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If num(y) &lt;&gt; dummy(y) Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num(loopcount1) = dummy(y)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exit For<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Next<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If num(loopcount1) = jCount1 And y = UBound(num) Then num(loopcount1) = dummy(y + 1)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>&nbsp;&nbsp;&nbsp;&nbsp;End If<br><br>&nbsp;&nbsp;&nbsp;Next<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;For z = 0 To UBound(num) - 1<br>&nbsp;&nbsp;&nbsp;For y = z + 1 To UBound(num)<br>&nbsp;&nbsp;&nbsp;&nbsp;If num(y) &lt; num(z) Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tem = num(z)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num(z) = num(y)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num(y) = tem<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>&nbsp;&nbsp;&nbsp;Next<br>&nbsp;&nbsp;Next<br>ReDim Preserve num(loopcount1 + 1)<br>Next<br><br>'-------------Display random images onto page---------------------------<br><br>for tem2 = 0 to 6<br>&nbsp;for iCount1 = 0 to count1 - 1<br>&nbsp;&nbsp;If iCount1 = num(tem2) Then<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;strImage = line1(iCount1)<br>&nbsp;&nbsp;&nbsp;poscomma1 = InStr(1, strImage, &quot;,&quot;)<br>&nbsp;&nbsp;&nbsp;imgName1 = Left(strImage, poscomma1 - 1)<br>&nbsp;&nbsp;&nbsp;href1 = Right(strImage, Len(strImage) - poscomma1)<br>&nbsp;&nbsp;&nbsp;Response.Write &quot;&lt;a href='&quot; & href1 & &quot;'&gt;&lt;IMG border = 0 SRC='images1_8_5/thumbs/&quot; & imgName1 & &quot;'&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;&quot; <br>&nbsp;&nbsp;End If <br>&nbsp;next<br>Next<br><br>%&gt;<br><br><br>Of course, this is may be not the best way to do it, but it works. <br>Good luck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top