正在载入……

Attention, Zin 的交互设计之路。

User interface, user experience and interactive design.

« flash鱼眼菜单,类似苹果系统导航利于搜索引擎的ajax的tab标签及ajax分页 »

数组随机排列之多版本

之前问过一个朋友得到了随机排列数组的方法,做了一个flash版本的,可以把预先定义的数组重新排列输出,代码如下

function randArr(myArr:Array):Array {
  var myArrL:Number = myArr.length;
  var tempArr;
  for (i=0; i<myArrL; i++) {
    a = Math.floor(Math.random()*myArrL);
    b = Math.floor(Math.random()*myArrL);
    tempArr = myArr[a];
    myArr[a] = myArr[b];
    myArr[b] = tempArr;
  }
  return myArr;
}


ASP版本,这里包含有个列子,是从数据库读取全部记录然后随机排列输出前n个,可以用来做到每次访问都显示不同的新闻、图片、广告等等

function randArr(arr)
  dim l:l=ubound(arr)
  dim tempStr, a, b
  for i=0 to l
    Randomize timer
    a = int(rnd()*l)
    b = int(rnd()*l)
    tempStr = arr(a)
    arr(a) = arr(b)
    arr(b) = tempStr
  next
  randArr = arr
end function

function buildIndexAW(db, n)
  set rs = server.CreateObject("adodb.recordset")
    sql = "select * from works where aw=true order by w_order"
    rs.open sql, conn(db), 1, 1
    if not rs.eof then
      dim al:al = rs.recordcount
      dim j:j = 0
      dim rsArr()
      redim rsArr(al)
      do while not rs.eof
        bgimg = rs("picsml")
        if bgimg="" or isnull(bgimg) then bgimg="images/thumb-sml.gif"
        if isFileExists(bgimg)=false then bgimg="images/thumb-sml.gif"
        id = rs("w_id")
        title = TransferHTML(rs("title")) & " - " & rs("aw_words")
        rsArr(j) = array(bgimg, id, title)
        rs.movenext
        j = j+1
      loop
      response.Write("<ul>")
      myrsArr = randArr(rsArr)
      if n>al+1 then n=al
      for i=0 to n-1
        response.Write("<li><a href=""works-view.asp?i=" & myrsArr(i)(1) & """ title=""" & myrsArr(i)(2) & """ style=""background-image: url(" & myrsArr(i)(0) & ");"" target=""_blank"">" & myrsArr(i)(2) & "</a></li>")
      next
      response.Write("</ul>")
    else
      response.Write("资料整理中……")
    end if
    rs.close
  set rs = nothing
end function


另,javascript版本跟php版本类似flash的actionscript的写法,但要注意变量声明方式

转载请注明出处,有问题请留言

欢迎发表看法:

您的讨论更有价值!

控制面板

<< 2008-12 >>

Sun

Mon

Tue

Wed

Thu

Fri

Sat

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

最近留言