Avatar billede guzzie Nybegynder
18. januar 2001 - 10:21 Der er 17 kommentarer og
1 løsning

random posts from database

Hey There

Instead of just taking the TOP 3 posts out of my database i would like it to take 3 random instead? Is this possible
heres the script i use

<%

Set Conn = Server.CreateObject(\"ADODB.Connection\")
Conn.Open \"DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=\" & server.MapPath(\"adresse.mdb\")

SQLstmt = \"SELECT TOP 3 * FROM adresse ORDER BY navn;\"
\'
\'response.write SQLstmt


Conn.Execute (SQLstmt)

Set rs = conn.Execute(SQLstmt)
%>


<table width=\"750\" align=\"left\" valign=\"top\" border=\"0\" cellspacing=\"5\">
<tr>
    <td colspan=\"2\" align=\"right\"><form method=\"POST\" action=\"search_result.asp\"><b>Søg:&nbsp;&nbsp;&nbsp; </b><input type=\"text\" name=\"Keyword\" size=\"20\"><input type=\"submit\" value=\"Search\" name=\"Action\"></td>
</tr>
  <tr>
    <%
  DO WHILE NOT rs.EOF
    id = rs(\"ID\")
    link = rs(\"link\")
    navn = rs(\"navn\")
    banner = rs(\"banner\")
    info_om = rs(\"info_om\")
    %>

<td class=\"one\">
<table bgcolor=\"0A223A\"><tr><td colspan=\"3\"><b><h4><%=navn%></h4></b></td>
</tr>
<tr>
    <td><a href=\"vis_data_alt.asp?Id=<%=Id%>\"><%=link%></a></td>
</tr>
<tr>
    <td colspan=\"2\" align=\"left\"><img width=\"468\" height=\"60\" border=\"1\" src=\"images/<%=banner%>\"></td>
</tr>
<tr>
    <td colspan=\"2\" width=\"100%\"><%=info_om%><br><br></td>
</tr>
</table>
</td>
</tr>
    <%
    rs.MoveNext
  LOOP
  RS.Close
  Response.Write \"</TABLE>\"

conn.Close
Set conn = nothing
Set SQLstmt = nothing
    %>
 
</tr>
</table>

</td></tr>
</table>
</body>

Anybody ?

Guzzie(nubie)
Avatar billede tdaugaard Nybegynder
18. januar 2001 - 10:24 #1
Is you ID field set to auto increment ? If so it\'s a little bit more complicated than if not..
Avatar billede tdaugaard Nybegynder
18. januar 2001 - 10:25 #2
Check out this question: http://195.184.98.141/spm/44049

It\'s in Danish, but feel free to ask if you\'re unsure about something.
Avatar billede guzzie Nybegynder
18. januar 2001 - 10:36 #3
Hey There

I saw that question before and tried to insert

sql = \"SELECT COUNT(*) AS total FROM din_tabel;\" \' tæl antal poster
Set Rs = Conn.Execute(sql)
intCount = CInt(Rs(\"total\"))

sql = \"SELECT * FROM din_tabel;\"
Set Rs = Conn.Execute(sql)

For Index = 1 To 3 \' find 3 forskellige
  intIndex = Int(Rnd * intCount)

  Rs.Move intIndex
  \' .. lav dit udtræk her


in my batabase but I am a little unsure where it should be in my DB connection
When i did try it I didn´t recieve any error but
it only showed 2 posts from my databse ?
And they aren´t random as far as i can see

heres the script after i tried what you suggested

<%

Set Conn = Server.CreateObject(\"ADODB.Connection\")
Conn.Open \"DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=\" & server.MapPath(\"adresse.mdb\")

sql = \"SELECT COUNT(*) AS total FROM adresse;\" \' tæl antal poster
Set Rs = Conn.Execute(sql)
intCount = CInt(Rs(\"total\"))

sql = \"SELECT * FROM adresse;\"
Set Rs = Conn.Execute(sql)

For Index = 1 To 3 \' find 3 forskellige
  intIndex = Int(Rnd * intCount)

  Rs.Move intIndex
  \' .. lav dit udtræk her

%>


<table width=\"750\" align=\"left\" valign=\"top\" border=\"0\" cellspacing=\"5\">
<tr>
    <td colspan=\"2\" align=\"right\"><form method=\"POST\" action=\"search_result.asp\"><b>Søg:&nbsp;&nbsp;&nbsp; </b><input

type=\"text\" name=\"Keyword\" size=\"20\"><input type=\"submit\" value=\"Search\" name=\"Action\"></td>
</tr>
  <tr>
    <%
  DO WHILE NOT rs.EOF
    id = rs(\"ID\")
    link = rs(\"link\")
    navn = rs(\"navn\")
    banner = rs(\"banner\")
    info_om = rs(\"info_om\")
    %>

<td class=\"one\">
<table bgcolor=\"0A223A\"><tr><td colspan=\"3\"><b><h4><%=navn%></h4></b></td>
</tr>
<tr>
    <td><a href=\"vis_data_alt.asp?Id=<%=Id%>\"><%=link%></a></td>
</tr>
<tr>
    <td colspan=\"2\" align=\"left\"><img width=\"468\" height=\"60\" border=\"1\" src=\"images/<%=banner%>\"></td>
</tr>
<tr>
    <td colspan=\"2\" width=\"100%\"><%=info_om%><br><br></td>
</tr>
</table>
</td>
</tr>
    <%
    rs.MoveNext
  LOOP
  RS.Close
  Response.Write \"</TABLE>\"
Next
conn.Close
Set conn = nothing
Set SQLstmt = nothing
    %>
 
</tr>
</table>

</td></tr>
</table>
</body>

Guzzie :)
Avatar billede tdaugaard Nybegynder
18. januar 2001 - 10:46 #4
You shouldn\'t use a DO ... LOOP block as the For..Next is what selects each recordset. Here\'s how your script SHOULD look:

<%
Set Conn = Server.CreateObject(\"ADODB.Connection\")
Conn.Open \"DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=\" & server.MapPath(\"adresse.mdb\")

sql = \"SELECT COUNT(*) AS total FROM adresse;\" \' count totalt posts
Set Rs = Conn.Execute(sql)
intCount = CInt(Rs(\"total\"))

sql = \"SELECT * FROM adresse;\"
Set Rs = Conn.Execute(sql)

For Index = 1 To 3 \' find 3 random posts
    intIndex = Int(Rnd * intCount)

    Rs.Move intIndex

    id = rs(\"ID\")
    link = rs(\"link\")
    navn = rs(\"navn\")
    banner = rs(\"banner\")
    info_om = rs(\"info_om\")
%>
<table width=\"750\" align=\"left\" valign=\"top\" border=\"0\" cellspacing=\"5\">
<tr>
    <td colspan=\"2\" align=\"right\"><form method=\"POST\" action=\"search_result.asp\"><b>Søg:&nbsp;&nbsp;&nbsp; </b><input

type=\"text\" name=\"Keyword\" size=\"20\"><input type=\"submit\" value=\"Search\" name=\"Action\"></td>
</tr>
  <tr>
<td class=\"one\">
<table bgcolor=\"0A223A\"><tr><td colspan=\"3\"><b><h4><%=navn%></h4></b></td>
</tr>
<tr>
    <td><a href=\"vis_data_alt.asp?Id=<%=Id%>\"><%=link%></a></td>
</tr>
<tr>
    <td colspan=\"2\" align=\"left\"><img width=\"468\" height=\"60\" border=\"1\" src=\"images/<%=banner%>\"></td>
</tr>
<tr>
    <td colspan=\"2\" width=\"100%\"><%=info_om%><br><br></td>
</tr>
</table>
</td>
</tr>
<%
    Response.Write \"</TABLE>\"
Next
conn.Close: Set conn = nothing
Set Rs = Nothing
%>
 
</tr>
</table>

</td></tr>
</table>
</body>
Avatar billede tdaugaard Nybegynder
18. januar 2001 - 10:47 #5
Oops ... at the bottom, change:

conn.Close: Set conn = nothing
Set Rs = Nothing

to
Set Rs = Nothing
conn.Close: Set conn = nothing
Avatar billede guzzie Nybegynder
18. januar 2001 - 10:51 #6
Hey There tdaugaard

I tried that and now it only shows one post from the db :(

Guzzie :)
Avatar billede tdaugaard Nybegynder
18. januar 2001 - 10:57 #7
It does ? Well it shows it 3 times then .. or ?

Another thing, you might want to insert the statement

Randomize

right before the start of the the For..Next loop
Avatar billede guzzie Nybegynder
18. januar 2001 - 11:00 #8
Hey There

Hmmm still only showing 1 post ;(

I checked Id in my database and it is set to
\"autonummering\" (think i spelt that right)
Guzzie (Still Nubie)
/me gives a couple more points
Avatar billede tdaugaard Nybegynder
18. januar 2001 - 11:43 #9
he he .. okay .. yes, autonummerering (auto increment). But i think that with this method it shouldn\'t matter whether it\'s set to any other type nor auto increment, as it counts the posts and selects a specific post without using SQL, where selecting a post using SQL on the ID it would cause a problem because of the auto increment field.

Did that make any sense ?

I\'ll take a closer look at it and see what i can do ..
Avatar billede tdaugaard Nybegynder
18. januar 2001 - 11:57 #10
My bad!

Insert \"Rs.MoveFirst\" (without the quotes) before Rs.Move intIndex
Avatar billede guzzie Nybegynder
18. januar 2001 - 13:30 #11
You r still the Dude :)

Thank u thank u thank u

Guzzie
Avatar billede tdaugaard Nybegynder
18. januar 2001 - 13:32 #12
:o) Your welcome :-)

Btw, where are you from ?
Avatar billede guzzie Nybegynder
18. januar 2001 - 13:45 #13
hey there :)

I come from England :)
however I have lived here in denmark (Fåborg) for 4 years
I speak and read Danish but my written Danish at this present moment really sucks ;(

Dansk er et svart sprøg :( - heh heh :)

Guzzie says u cool asp dude :)
Avatar billede tdaugaard Nybegynder
18. januar 2001 - 13:49 #14
England .. cool .. it\'s a great country :-)

he he .. like my written english sometimes suck big time ;o)
Avatar billede guzzie Nybegynder
18. januar 2001 - 14:08 #15
heh - Its a damn sight better than my crappy written danish :)
I know you are danish but whereabouts in Denmark ?
Avatar billede tdaugaard Nybegynder
18. januar 2001 - 14:12 #16
*lol* I\'m from Sjælland .. near the storebæltsbro (*g* that sounds VERY stupid if you read that sentence..)
Avatar billede guzzie Nybegynder
18. januar 2001 - 14:16 #17
HEH :) think i know :)
I know where slagelse at least :)
r u a programmer or what ?
:) heh
Ps you should hear my Denglish :)
Avatar billede tdaugaard Nybegynder
18. januar 2001 - 14:21 #18
You know where Slagelse is ? I live around 11 KM from Slagelse .. (in a itty-bitty town called Boeslunde...)

Well, I\'m not a professional programmer .. but a sort of, yes.

*LOL* Denglish ..

Do you have ICQ ? If so my UIN is # 4033867
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester