Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Code Support: ASP (Non-Forum Related)
 Randomly selecting from two different recordsets..
 New Topic  Topic Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

skyhawks
Junior Member

USA
125 Posts

Posted - 05 March 2002 :  00:41:05  Show Profile  Visit skyhawks's Homepage
GauravBhabu,

i have a question about your code for the random (first method). I am trying to randomly select from my database.. now on that one asp page, i want to randomly select from two different Tables (or recordset). anyword, see below what I am trying to do.. i'm not getting what I want. its coming out as following..(after html)

I hope i can explain it correctly...

<!--Dining Out-->
<%
dim sConn, sConnString, sStrSql, sRstRandom
dim sIntMaxRecs
dim sIntTotalRecs,sRndRecList

Const adOpenStatic = 3

sConnString = "nydb"
set sConn = server.createobject("adodb.connection")
sConn.open sConnString
sStrSql = "Select ID, Restaurant From Diningreview"
set sRstRandom = server.CreateObject("adodb.recordset")
sRstRandom.Open sStrSql,sConn,adOpenStatic

sIntMaxRecs = 4 'Replace the Number to your need

if not sRstRandom.EOF then
sIntTotalRecs = sRstRandom.recordcount
if sIntTotalRecs > sIntMaxRecs then
Call FindRandomRecords()
else
do while not sRstRandom.EOF
call DisplayRecord()
sRstRandom.MoveNext
Loop
end if
end if

sRstRandom.Close
set sRstRandom= nothing

'This sub Finds Randomn Records
sub FindRandomRecords()
Dim RndRec,blnRecFound,arrRndList,iRec,jRec
Randomize()
for iRec = 0 to (sIntMaxRecs - 1)
RndRec = Int((sIntTotalRecs - 1 + 1) * Rnd + 1)
sRstRandom.Move RndRec - 1
if not sRstRandom.eof then
blnRecFound = true
else
blnRecFound = false
end if
if blnRecFound then
if iRec > 1 then
arrRndList = split(sRndRecList,"#")
for jRec = 0 to (iRec - 1)
if cint(RndRec) = cint(arrRndList(jRec)) then blnRecFound = false : exit for
next
else
if iRec = 1 then
if cint(RndRec) = cint(sRndRecList) then blnRecFound = false
end if
end if
end if
if blnRecFound then
if iRec = 0 then
sRndRecList = RndRec
else
sRndRecList = sRndRecList & "#" & RndRec
end if
call DisplayRecord()
else
iRec = (iRec - 1)
end if
sRstRandom.MoveFirst
next
end sub

'This sub Displays Randomn Records
sub DisplayRecord()
Response.Write vbNewLine & _
"<li><a href=""/diningout.asp?ID=" & sRstRandom.Fields(0) & """>" & sRstRandom.Fields(1) & "</a><br>" & vbNewLine
end sub
%>


<!--movietitle-->
<%
set sRstRandom = server.CreateObject("adodb.recordset")
sStrSql = "Select ID, movietitle From moviereview"
sRstRandom.Open sStrSql,sConn,adOpenStatic

sIntMaxRecs = 5 'Replace the Number to your need

if not sRstRandom.EOF then
sIntTotalRecs = sRstRandom.recordcount
if sIntTotalRecs > sIntMaxRecs then

Call FindRandomRecords()
else
do while not sRstRandom.EOF
call DisplayRecord()
sRstRandom.MoveNext
Loop
end if
end if

sRstRandom.Close
set sRstRandom= nothing

'This sub Displays Randomn Records
sub DisplayRecord()
Response.Write vbNewLine & _
"<li><a href=""/moviereview.asp?ID=" & sRstRandom.Fields(0) & """>" & sRstRandom.Fields(1) & "</a><br>" & vbNewLine
end sub
%>



The Result in HTML
<!--Dining Out-->
<li><a href="/moviereview.asp?ID=12">Papillon</a><br>
<li><a href="/moviereview.asp?ID=14">Craftbar</a><br>
<li><a href="/moviereview.asp?ID=7">Marseille</a><br>
<li><a href="/moviereview.asp?ID=10">Suba</a><br>

<!--movietitle-->
<li><a href="/moviereview.asp?ID=24">Sorority Boys</a><br>
<li><a href="/moviereview.asp?ID=2">John Q</a><br>
<li><a href="/moviereview.asp?ID=22">The Time Machine</a><br>
<li><a href="/moviereview.asp?ID=11">Spider-Man</a><br>
<li><a href="/moviereview.asp?ID=26">Minority Report</a><br>


Under Dining Out.. the output is wrong.. it should be as follow

<li><a href="/diningout.asp?ID=12">Papillon</a><br>
<li><a href="/diningout.asp?ID=14">Craftbar</a><br>
<li><a href="/diningout.asp?ID=7">Marseille</a><br>
<li><a href="/diningout.asp?ID=10">Suba</a><br>


the only thing wrong with this output is the response.write.....



Nikkol
Forum Moderator

USA
6907 Posts

Posted - 05 March 2002 :  04:20:32  Show Profile
I know I'm not GB, but..
That's because you have two different subs named the same thing. One solution would be to make one sub with an argument that represents whether or not you want a movie review or dining out page. (1=moviereview, 2=diningout). In the sub you would have an if statement along the lines of

if myArg = 1 then
response.write("movie review stuff")
else
response.write("dining out stuff")
end if

Then when you call the sub, use either DisplayRecord(1) or DisplayRecord(2).

Nikkol

Edited by - Nikkol on 05 March 2002 04:21:11
Go to Top of Page

GauravBhabu
Advanced Member

4288 Posts

Posted - 05 March 2002 :  07:32:31  Show Profile
Nikol is Right. You cannot have two subs named the same. Instead you should use arguments to achieve different objectives usnig the same sub or function.


'This sub Displays Randomn Records
sub DisplayRecord(strFileName)
Response.Write vbNewLine & _
"<li><a href=""" & "/" & strFileName & ".asp?ID=" & sRstRandom.Fields(0) & """>" & sRstRandom.Fields(1) & "</a><br>" & vbNewLine
end sub


Then call this sub as below


Call DisplayRecord("moviereview")

or

Call DisplayRecord("diningout")

www.forumSquare.com - GauravBhabu - It is difficult to IMPROVE on Perfection, There is no harm in Keep Trying.
Go to Top of Page

Nikkol
Forum Moderator

USA
6907 Posts

Posted - 05 March 2002 :  08:16:55  Show Profile
sub DisplayRecord(strFileName) ... Even better GB I knew I was missing something!

Nikkol
Go to Top of Page

skyhawks
Junior Member

USA
125 Posts

Posted - 05 March 2002 :  10:56:49  Show Profile  Visit skyhawks's Homepage
thanks GB

and

thanks Nikkol

Go to Top of Page

skyhawks
Junior Member

USA
125 Posts

Posted - 05 March 2002 :  20:07:33  Show Profile  Visit skyhawks's Homepage
same method, but getting a little different info.... was a little hard to explain it.. hopefully waht I wrote out below might be more understandable..

sky




<!--movietitle-->

sub DisplayRecord(strFileName)
If column = 0 Then Response.Write vbNewLine & _
"<tr>" & vbNewLine
Response.Write "<a href=""" & "/" & strFileName & ".asp?ID=" & sRstRandom.Fields(0) & """><img src=""/movie/posters/" & sRstRandom.Fields(1) & """ border=0></a></td>" & vbNewLine
column = column + 1
If column = ColumnCount Then
Response.Write "</tr>" & vbNewLine
column = 0
End If
end sub



!--Dining Out-->

sub DisplayRecord(strFileName)
Response.Write vbNewLine & _
"<li><a href=""" & "/" & strFileName & ".asp?ID=" & sRstRandom.Fields(0) & """>" & sRstRandom.Fields(1) & "</a><br>" & vbNewLine
end sub



This is what I wanted in HTML

<!--movietitle-->
<tr>
<td align="center"><a href="moviereview.asp?ID=2"><img src="/movie/posters/johnq_poster_large.jpg" border=0></a></td>
<td align="center"><a href="moviereview.asp?ID=26"><img src="/movie/posters/minorityreport_poster_large.jpg" border=0></a></td>
<td align="center"><a href="moviereview.asp?ID=19"><img src="/movie/posters/queenlarge.jpg" border=0></a></td>
</tr>
<tr>
<td align="center"><a href="moviereview.asp?ID=11"><img src="/movie/posters/spiderman_poster_large.jpg" border=0></a></td>
<td align="center"><a href="moviereview.asp?ID=9"><img src="/movie/posters/blade2_poster_large.jpg" border=0></a></td>
<td align="center"><a href="moviereview.asp?ID=6"><img src="/movie/posters/big_fat_liar_poster_large.jpg" border=0></a></td>
</tr>


<!--Dining Out-->
<li><a href="/moviereview.asp?ID=12">Papillon</a><br>
<li><a href="/moviereview.asp?ID=14">Craftbar</a><br>
<li><a href="/moviereview.asp?ID=7">Marseille</a><br>
<li><a href="/moviereview.asp?ID=10">Suba</a><br>


this is the result i am getting....

<!--movietitle-->

<li><a href="/moviereview.asp?ID=2">johnq_poster_large.jpg</a><br>
<li><a href="/moviereview.asp?ID=26">minorityreport_poster_large.jpg</a><br>
<li><a href="/moviereview.asp?ID=19">queenlarge.jpg</a><br>
<li><a href="/moviereview.asp?ID=11">spiderman_poster_large.jpg</a><br>
<li><a href="/moviereview.asp?ID=9">blade2_poster_large.jpg</a><br>
<li><a href="/moviereview.asp?ID=6">big_fat_liar_poster_large.jpg</a><br>


<!--Dining Out-->
<li><a href="/moviereview.asp?ID=12">Papillon</a><br>
<li><a href="/moviereview.asp?ID=14">Craftbar</a><br>
<li><a href="/moviereview.asp?ID=7">Marseille</a><br>
<li><a href="/moviereview.asp?ID=10">Suba</a><br>

Edited by - skyhawks on 05 March 2002 20:12:33
Go to Top of Page

GauravBhabu
Advanced Member

4288 Posts

Posted - 05 March 2002 :  20:23:12  Show Profile
you should use the sub only once

Either This
 
sub DisplayRecord(strFileName)
If column = 0 Then Response.Write vbNewLine & _
"<tr>" & vbNewLine
Response.Write "<a href=""" & "/" & strFileName & ".asp?ID=" & sRstRandom.Fields(0) & """><img src=""/movie/posters/" & sRstRandom.Fields(1) & """ border=0></a></td>" & vbNewLine
column = column + 1
If column = ColumnCount Then
Response.Write "</tr>" & vbNewLine
column = 0
End If
end sub





!--Dining Out-->

Or This

sub DisplayRecord(strFileName)
Response.Write vbNewLine & _
"<li><a href=""" & "/" & strFileName & ".asp?ID=" & sRstRandom.Fields(0) & """>" & sRstRandom.Fields(1) & "</a><br>" & vbNewLine
end sub





www.forumSquare.com - GauravBhabu - It is difficult to IMPROVE on Perfection, There is no harm in Keep Trying.
Go to Top of Page

Nikkol
Forum Moderator

USA
6907 Posts

Posted - 05 March 2002 :  20:29:41  Show Profile
yes, and the problem with the html output you are having (with the picture) is this... you have:

Response.Write "<a href=""" & "/" & strFileName & ".asp?ID=" & _
sRstRandom.Fields(0) & """><img src=""/movie/posters/" & _
sRstRandom.Fields(1) & """ border=0></a></td>" & vbNewLine

Take out those two double quotes in red.

Nikkol

Edited by - Nikkol on 05 March 2002 20:31:23
Go to Top of Page

GauravBhabu
Advanced Member

4288 Posts

Posted - 05 March 2002 :  20:57:17  Show Profile
Nikol Looking at his code, I think he wants to use images for moview Review only so it will be better for him to use case statements in the sub.


sub DisplayRecord(strFileName)
Select Case strFileName
Case "moviereview"
Response.Write "<a href=""" & "/" & strFileName & ".asp?ID=" & sRstRandom.Fields(0) & """><img src=""/movie/posters/" & sRstRandom.Fields(1) & """ border=0></a></td>" & vbNewLine

Case "diningout"
Response.Write vbNewLine & _
"<a href=""" & "/" & strFileName & ".asp?ID=" & sRstRandom.Fields(0) & """>" & sRstRandom.Fields(1) & "</a><br>" & vbNewLine
end select
end sub



I got to rush out...

www.forumSquare.com - GauravBhabu - It is difficult to IMPROVE on Perfection, There is no harm in Keep Trying.

Edited by - GauravBhabu on 05 March 2002 20:57:57
Go to Top of Page

skyhawks
Junior Member

USA
125 Posts

Posted - 06 March 2002 :  08:33:49  Show Profile  Visit skyhawks's Homepage
yea GB, thats what I'm trying to do... i'm a little lost what you just did

Go to Top of Page

GauravBhabu
Advanced Member

4288 Posts

Posted - 06 March 2002 :  10:13:32  Show Profile
Post the complete code you are using. Post it here or at my site.

www.forumSquare.com - GauravBhabu - It is difficult to IMPROVE on Perfection, There is no harm in Keep Trying.
Go to Top of Page

skyhawks
Junior Member

USA
125 Posts

Posted - 08 March 2002 :  12:56:47  Show Profile  Visit skyhawks's Homepage
hi GB, just wanted to say thanks for helping me out. sorry it took me this long to reply. anyway, heres the code i started out with.....

<html><head><title></title></head><body>

<%
dim sConn, sConnString, sStrSql, sRstRandom
dim sIntMaxRecs
dim sIntTotalRecs,sRndRecList

sConnString = "newyorkdb"

Const adOpenStatic = 3
Const ColumnCount = 3 'Number of Columns you want in the table

set sConn = server.createobject("adodb.connection")
sConn.open sConnString
sStrSql = "Select ID,imgposterlarge From moviereview ORDER BY ID ASC"
set sRstRandom = server.CreateObject("adodb.recordset")
sRstRandom.Open sStrSql,sConn,adOpenStatic

column = 0
sIntMaxRecs = 6 'Number of records outputting from database

if not sRstRandom.EOF then
sIntTotalRecs = sRstRandom.recordcount
if sIntTotalRecs <> "" then
Response.Write vbNewLine & _
"<table width=""100%"" border=""0"" cellspacing=""20"" cellpadding=""0"">" & vbNewLine

if sIntTotalRecs > sIntMaxRecs then
Call FindRandomRecords()
else
do while not sRstRandom.EOF
call DisplayRecord()
sRstRandom.MoveNext
Loop
end if
Response.Write vbNewLine & _
"</table>" & vbnewLine

end if
end if

sRstRandom.Close
set sRstRandom= nothing

sub FindRandomRecords()
Dim RndRec,blnRecFound,arrRndList,iRec,jRec
Randomize()
for iRec = 0 to (sIntMaxRecs - 1)
RndRec = Int((sIntTotalRecs - 1 + 1) * Rnd + 1)
sRstRandom.Move RndRec - 1
if not sRstRandom.eof then
blnRecFound = true
else
blnRecFound = false
end if
if blnRecFound then
if iRec > 1 then
arrRndList = split(sRndRecList,"#")
for jRec = 0 to (iRec - 1)
if cint(RndRec) = cint(arrRndList(jRec)) then blnRecFound = false : exit for
next
else
if iRec = 1 then
if cint(RndRec) = cint(sRndRecList) then blnRecFound = false
end if
end if
end if
if blnRecFound then
if iRec = 0 then
sRndRecList = RndRec
else
sRndRecList = sRndRecList & "#" & RndRec
end if
call DisplayRecord()
else
iRec = (iRec - 1)
end if
sRstRandom.MoveFirst
next
end sub

sub DisplayRecord()
If column = 0 Then Response.Write vbNewLine & _
"<tr>" & vbNewLine
Response.Write "<td align=""center""><a href=""/newyork/features/movies/moviereview.asp?ID=" & sRstRandom.Fields(0) & """><img src=""/movie/posters/" & sRstRandom.Fields(1) & """ border=0></a></td>" & vbNewLine
column = column + 1
If column = ColumnCount Then
Response.Write "</tr>" & vbNewLine
column = 0

End If
end sub
%>

<!-- Now Showing -->
<p>Now Showing</p>

<%
'dim sConn, sConnString, sStrSql, sRstRandom
'dim sIntMaxRecs
'dim sIntTotalRecs,sRndRecList

'Const adOpenStatic = 3

'sConnString = "newyorkdb"
'set sConn = server.createobject("adodb.connection")
'sConn.open sConnString

sStrSql = "SELECT ID, movietitle FROM moviereview WHERE showing = 'yes' ORDER BY movietitle ASC"
set sRstRandom = server.CreateObject("adodb.recordset")
sRstRandom.Open sStrSql,sConn,adOpenStatic

sIntMaxRecs = 8

if not sRstRandom.EOF then
sIntTotalRecs = sRstRandom.recordcount
if sIntTotalRecs > sIntMaxRecs then
Call FindRandomRecords()
else
x = 1
do while not sRstRandom.EOF
call DisplayRecord(newyork/features/movies/moviereview)
x = x + 1
sRstRandom.MoveNext
Loop
end if
end if

sRstRandom.Close
set sRstRandom= nothing

sub FindRandomRecords()
Dim RndRec,blnRecFound,arrRndList,iRec,jRec
Randomize()
for iRec = 0 to (sIntMaxRecs - 1)
RndRec = Int((sIntTotalRecs - 1 + 1) * Rnd + 1)
sRstRandom.Move RndRec - 1
if not sRstRandom.eof then
blnRecFound = true
else
blnRecFound = false
end if
if blnRecFound then
if iRec > 1 then
arrRndList = split(sRndRecList,"#")
for jRec = 0 to (iRec - 1)
if cint(RndRec) = cint(arrRndList(jRec)) then blnRecFound = false : exit for
next
else
if iRec = 1 then
if cint(RndRec) = cint(sRndRecList) then blnRecFound = false
end if
end if
end if
if blnRecFound then
if iRec = 0 then
sRndRecList = RndRec
else
sRndRecList = sRndRecList & "#" & RndRec
end if
call DisplayRecord(newyork/features/movies/moviereview)
else
iRec = (iRec - 1)
end if
sRstRandom.MoveFirst
next
end sub

sub DisplayRecord(strFileName)
Response.Write x
Response.Write ". <a href=""" & "/" & strFileName & ".asp?ID=" & sRstRandom.Fields(0) & """>" & sRstRandom.Fields(1) & "</a><br>" & vbNewLine

end sub
%>


<!-- Coming Soon -->
<p>coming soon</p>

<%

sStrSql = "SELECT ID, movietitle FROM moviereview WHERE showing = 'no' ORDER BY releasedate ASC"

set sRstRandom = server.CreateObject("adodb.recordset")
sRstRandom.Open sStrSql,sConn,adOpenStatic

sIntMaxRecs = 8

if not sRstRandom.EOF then
sIntTotalRecs = sRstRandom.recordcount
if sIntTotalRecs > sIntMaxRecs then
Call FindRandomRecords()
else
do while not sRstRandom.EOF
call DisplayRecord(newyork/features/movies/moviereview)
sRstRandom.MoveNext
Loop
end if
end if

sRstRandom.Close
set sRstRandom= nothing

sub FindRandomRecords()
Dim RndRec,blnRecFound,arrRndList,iRec,jRec
Randomize()
for iRec = 0 to (sIntMaxRecs - 1)
RndRec = Int((sIntTotalRecs - 1 + 1) * Rnd + 1)
sRstRandom.Move RndRec - 1
if not sRstRandom.eof then
blnRecFound = true
else
blnRecFound = false
end if
if blnRecFound then
if iRec > 1 then
arrRndList = split(sRndRecList,"#")
for jRec = 0 to (iRec - 1)
if cint(RndRec) = cint(arrRndList(jRec)) then blnRecFound = false : exit for
next
else
if iRec = 1 then
if cint(RndRec) = cint(sRndRecList) then blnRecFound = false
end if
end if
end if
if blnRecFound then
if iRec = 0 then
sRndRecList = RndRec
else
sRndRecList = sRndRecList & "#" & RndRec
end if
call DisplayRecord(newyork/features/movies/moviereview)
else
iRec = (iRec - 1)
end if
sRstRandom.MoveFirst
next
end sub

sub DisplayRecord(strFileName)
Response.Write "<li>"
Response.Write "<a href=""" & "/" & strFileName & ".asp?ID=" & sRstRandom.Fields(0) & """>" & sRstRandom.Fields(1) & "</a></li>" & vbNewLine


end sub
%>

</body></html>


this is how I wanted to be display.....

<table width="100%" border="0" cellspacing="20" cellpadding="0">
<tr>
<td align="center"><a href="/newyork/features/movies/moviereview.asp?ID=7"><img src="/movie/posters/black.jpg" border=0></a></td>
<td align="center"><a href="/newyork/features/movies/moviereview.asp?ID=2"><img src="/movie/posters/johnq.jpg" border=0></a></td>
<td align="center"><a href="/newyork/features/movies/moviereview.asp?ID=18"><img src="/movie/posters/hartswar.jpg" border=0></a></td>
</tr>
<tr>
<td align="center"><a href="/newyork/features/movies/moviereview.asp?ID=26"><img src="/movie/posters/minority.jpg" border=0></a></td>
<td align="center"><a href="/newyork/features/movies/moviereview.asp?ID=19"><img src="/movie/posters/queen.jpg" border=0></a></td>
<td align="center"><a href="/newyork/features/movies/moviereview.asp?ID=8"><img src="/movie/posters/walk.jpg" border=0></a></td>
</tr>
</table>

<p>Now SHowing</p>

1. <a href="/newyork/features/movies/moviereview.asp?ID=8">A Walk to Remember</a><br>
2. <a href="/newyork/features/movies/moviereview.asp?ID=6">Big Fat Liar </a><br>
3. <a href="/newyork/features/movies/moviereview.asp?ID=7">Black Hawk Down</a><br>
4. <a href="/newyork/features/movies/moviereview.asp?ID=10">Collateral Damage</a><br>
5. <a href="/newyork/features/movies/moviereview.asp?ID=1">Crossroads </a><br>
6. <a href="/newyork/features/movies/moviereview.asp?ID=18">Hart's War </a><br>
7. <a href="/newyork/features/movies/moviereview.asp?ID=2">John Q</a><br>
8. <a href="/newyork/features/movies/moviereview.asp?ID=19">40 Days and 40 Nights</a><br>
9. <a href="/newyork/features/movies/moviereview.asp?ID=17">The Count of Monte Cristo</a><br>
10. <a href="/newyork/features/movies/moviereview.asp?ID=16">The Mothman Prophecies</a><br>

<p>Coming Soon</p>

<li><a href="/newyork/features/movies/moviereview.asp?ID=25">The Scorpion King </a></li>
<li><a href="/newyork/features/movies/moviereview.asp?ID=5">Men In Black 2</a></li>
<li><a href="/newyork/features/movies/moviereview.asp?ID=26">Minority Report </a></li>
<li><a href="/newyork/features/movies/moviereview.asp?ID=21">We Were Soldiers </a></li>
<li><a href="/newyork/features/movies/moviereview.asp?ID=23">Resident Evil </a></li>
<li><a href="/newyork/features/movies/moviereview.asp?ID=9">Blade II </a></li>
<li><a href="/newyork/features/movies/moviereview.asp?ID=24">Sorority Boys </a></li>
<li><a href="/newyork/features/movies/moviereview.asp?ID=22">The Time Machine </a></li>
<li><a href="/newyork/features/movies/moviereview.asp?ID=12">Star Wars: Episode II - Attack of the Clones </a></li>
<li><a href="/newyork/features/movies/moviereview.asp?ID=11">Spider-Man</a></li>




Go to Top of Page

skyhawks
Junior Member

USA
125 Posts

Posted - 17 March 2002 :  17:21:41  Show Profile  Visit skyhawks's Homepage
GauravBhabu,

are you there?? i'm just wondering whats the status with the question i have. Thanks,

sky

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.27 seconds. Powered By: Snitz Forums 2000 Version 3.4.07