[thelist] Re: Strange problem with ASP (random) (here is the code)

Joshua Olson joshua at alphashop.net
Sun Jan 20 09:00:40 CST 2002


Chris,

Is this code, it does look like randomize may be the issue involved.  Simply
execute Randomize somewhere before the pick_row88 =
Int(Rnd*randombookcounter) and that should work a little better.  However,
your code could be reduced somewhat as follows:

      <%
      SQL22=("SELECT * FROM t_bookstore ORDER BY aID;")
      set randombookdisplay=conn.execute(SQL22)
      Randomize
      pick_row88 = Int(Rnd*randombookdisplay.RecordCount)
      randombookdisplay.Move pick_row88

      Response.Write "<center><a href=booklist.asp?category=" & _
        randombookdisplay(2) & "><img border=0 src=" & _
        randombookdisplay(1) & "></a></center>"
      randombookdisplay.close
      %>

The big change is that the code no longer makes two trips to the DB.  The
RecordCount property of the Recordset returns the number of rows returned,
so I do not have to loop through the results to count them.  Secondly, the
function Move of the Recordset advances the current record within the
Recordset thereby removing the second "display" loop.  Thirdly--and I did
not make this change above--I'd explicitly identify those columns to return
in the select statement.  For example, if column 1 is "categoryID" and
column 2 is "bookID" then modify the SELECT statement as follows:

SELECT categoryID, bookID FROM t_bookstore ORDER BY aID;

It will execute faster than SELECT * if there are more than two columns in
the table.

HTH,
-joshua





More information about the thelist mailing list