使用数组打乱记录顺序,随机显示记录

来源:半月设计 发布人:guobar 发布时间:2005-7-1 10:38:02 人气: 所属栏目:ASP研究-ASP

本程序使用数组,经过随机运算之后,将数组打乱,然后将数据随机显示,虽然实用性不高,但对asp研究还是有一定好处的。

*********************

Guobar原创
http://www.guobar.com

*********************
主程序代码:
’建立记录集访问数据库
set rs=server.CreateObject("adodb.recordset")
sql="select * from Admin_menu"
rs.open sql,conn,3,1
first=rs.GetRows()
if(ubound(first,2)<0) then
response.Write("没有记录")
response.end
end if
’’生成id数组
idstr="0"
for i=0 to ubound(first,2)
idstr=first(0,i)&","&idstr
next 
rs.close
set rs=nothing
’’对数据id进行排序,打乱顺序
str= idid(idstr,0)
’’分离新数组
strs=split(str,",")
’’循环整个数组
for i=0 to ubound(strs)-1
  for j=0 to ubound(first,2)
    if cint(first(0,j))=cint(strs(i)) then
 ’’按照新数组的顺序显示记录
    response.Write (first(0,j)&"-"&first(1,j)&"
") 
    end if
  next
next
’’--begin---定义数组打乱函数
Function idid(idstr,temp)
ids=split(idstr,",")
idsl=lbound(ids)
idsu=ubound(ids)
Randomize
n=Int(idsu * Rnd + 1)
idm=ids(n-1)
  if idm<>"0" then
 temp= idm&","&temp
   end if
  idstr="0"
    for i=0 to idsu
      if cstr(ids(i))<>cstr(idm)  then
      idstr=ids(i)&","&idstr
      end if
    next
 ’response.write idstr&"
"
  if idsu>1 then
  call idid(idstr,temp)
  end if
  idid=temp
End Function
rsc conn
’’--end---




生成时间:2006-5-27 2:00:22