20. maj 2001 - 18:25Der er
7 kommentarer og 1 løsning
array og noget
Jeg har et array som består af 10 forskellige ting.. (uuh) :).. nå, men de skal så smides ud på siden på tilfældige pladser - men alle elementerne i array\'et SKAL brugues... nogen ideer?
Denne kode udskriver felter fra database i en tilfældig rækkefolge
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">
<html> <head> <title>Untitled</title> </head>
<body> <% Function ReOrderArray2D(ByVal aArray)
\'Get the upper and lower bounds from the second \'dimension (the row count)... also, create a temporary \'array, aTmpArray, to hold the reordered values Dim iCount, aTmpArray iCount = UBound(aArray, 2) ReDim aTmpArray(UBound(aArray, 1), UBound(aArray, 2))
Dim iLoop, strIndex, iUpper, iLower iLower = LBound(aArray, 2)
For iLoop = iLower to iCount strIndex = strIndex & CStr(iLoop)
If iLoop < iCount then strIndex = strIndex & \",\" Next
\'Choose a Random Index Randomize Timer
Dim iRnd, aTmp, iTmpUpper, strNewIndex, iInnerLoop Redim aTmp(iCount)
\'Loop through the array For iLoop = iLower to iCount iTmpUpper = iCount - iLoop
\'Rebuild aTmp, the array of indexes ReDim Preserve aTmp(iTmpUpper) aTmp = split(strIndex, \",\")
\'Add an element to the temp array... iRnd = Int(Rnd * (iTmpUpper + 1)) For iInnerLoop = LBound(aArray,1) to UBound(aArray,1) aTmpArray(iInnerLoop, iLoop) = aArray(iInnerLoop, aTmp(iRnd)) Next
\'Compact aTmp, the array of indexes aTmp(iRnd) = aTmp(iTmpUpper)
ReDim Preserve aTmp(iTmpUpper - 1) strIndex = join(aTmp, \",\") Next
ReOrderArray2D = aTmpArray End Function
\'Before we call ReOrderArray2D, we need to use the GetRows method to convert a Recordset into an array. This is quite simple, and can be seen with the following code:
\'Open up connection to database... \'Dim objConn \'Set objConn = Server.CreateObject(\"ADODB.Connection\") \'objConn.Open \"Driver={Microsoft Access Driver (*.mdb)};DBQ=\" & Server.MapPath(\"boykot1.mdb\")
\'Create a recordset object and grab a query \'Dim objRS \'Set objRS = Server.CreateObject(\"ADODB.Recordset\") \'objRS.Open \"SELECT forumname FROM Myforums\", objConn \'strSQL = \"Select * from Myforums\" \'set objRS = objConn.execute( strSQL ) Set Conn = Server.CreateObject(\"ADODB.Connection\") Conn.Open \"Driver={Microsoft Access Driver (*.mdb)};DBQ=\" & Server.MapPath(\"db.mdb\") strSQL = \"Select felt from tabel\" set RS = Conn.execute( strSQL ) If RS.eof Then Response.write \"Findes ingen\" Response.End End If \'Response.write RS(\"sagsnr\") \'Response.End
\'Create an Array variable and use GetRows Dim aRows aRows = RS.GetRows() Dim Test(100,3)
ja... du opretter et array mere som fylder det samme (10 poster) derefter laver du en løkke som kører fra 10 ned til 1 du tar en tilfældig post fra den array med maks på den værdi som løkken nu har (int(rnd*løkke)+1) udskriver den og bytter med den sidste post (løkke)
og så skal der outputtes i tilfældig rækkefølge.. som f.eks. 2,10,8,3,5,1,7,6,4,9 men alle tingene i array\'et SKAL bruges - altså tallet 8 må ikke forekomme 2 gange..
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.