Results 1 to 8 of 8

Thread: ARRAY

  1. #1
    ARRAY Guest

    Default ARRAY

    I can&#039;t get this array to work. It is my first one and I have read all the FAQ&#039;s but there is nothing about multi-dementional array&#039;s. My GOAL is to pull out info from the DB in a format like so:<BR><BR>Category1<BR> * question related to category1<BR> * question related to category1<BR>Category2<BR> * question related to category2<BR> * question related to category2<BR><BR>etc... Every question in DB is related to one of the categories.<BR><BR>Here is what I have so far:<BR><BR> &#060;%<BR> Dim catArray()<BR> Dim rs<BR> Dim x<BR> x=0<BR> <BR> <BR> sql="SELECT distinct categories FROM TBL1 where Product =&#039;Whatever&#039;" <BR> sql=sql & "ORDER BY categories"<BR> Set rs = Server.CreateObject("ADODB.Recordset") <BR> rs.Open sql, oConn, adopenstatic <BR> <BR> do while not rs.EOF<BR> x=x+1<BR> &#039;redim PRESERVE catArray(x) <BR> redim PRESERVE catArray(18, x-1) <BR> &#039;catArray(x)= rs("Categories")<BR> catArray(0, x-1)=rs("Categories")<BR> catArray(1, x-1)=rs("Categories")<BR> catArray(2, x-1)=rs("Categories")<BR> catArray(3, x-1)=rs("Categories")<BR> catArray(4, x-1)=rs("Categories")<BR> catArray(5, x-1)=rs("Categories")<BR> catArray(6, x-1)=rs("Categories")<BR> catArray(7, x-1)=rs("Categories")<BR> catArray(8, x-1)=rs("Categories")<BR> catArray(9, x-1)=rs("Categories")<BR> catArray(10, x-1)=rs("Categories")<BR> catArray(11, x-1)=rs("Categories")<BR> catArray(12, x-1)=rs("Categories")<BR> catArray(13, x-1)=rs("Categories")<BR> catArray(14, x-1)=rs("Categories")<BR> catArray(15, x-1)=rs("Categories")<BR> catArray(16, x-1)=rs("Categories")<BR> catArray(17, x-1)=rs("Categories")<BR> rs.MoveNext <BR> loop<BR> rs.close<BR><BR> <BR> <BR>Dim QuestArray<BR> <BR> <BR>sql="Select * from TBL1 where product =&#039;whatever&#039;"<BR>&#039;sql=sql & "AND categories = categories" <BR>sql=sql & "ORDER BY categories"<BR>Set rs = Server.CreateObject("ADODB.Recordset") <BR>rs.Open sql, oConn, adopenstatic <BR> do while not rs.EOF<BR> redim PRESERVE QuestArray(x) <BR> QuestArray(x)= rs("Questions")<BR> <BR> response.write "&#060;ul&#062;"<BR> response.write rs("categories") & "&#060;p&#062;"<BR> response.write "&#060;li&#062;"<BR> response.write rs("Questions") & "&#060;p&#062;" <BR> response.write "&#060;/li&#062;" response.write "&#060;/ul&#062;" <BR> <BR> rs.MoveNext <BR> loop<BR> %&#062;<BR>Error is Microsoft VBScript runtime (0x800A000D)<BR>Type mismatch<BR>on this line: redim PRESERVE QuestArray(x)<BR><BR>I have referenced books and the examples are not dealing with DB and are fairly simple. Any help would be appreciated. Thanks!<BR>

  2. #2 Guest

    Default RE: ARRAY

    You know, I can&#039;t, for the life of me, figure out what you&#039;re trying to do. But, intuitively, I think you don&#039;t need the array&#039;s at all...just a single, more complex SQL statement and a couple of string variables. But your code seems incomplete if my intuition about your intent is correct.

  3. #3
    brother love Guest

    Default first of all...

    ...i couldn&#039;t let this one pass by:<BR><BR>&#062;multi-dementional array&#039;s<BR><BR>i try to use non-demented arrays whenever possible<BR><BR>but anyway....<BR><BR>i don&#039;t think that vbscript allows you to redim multi-dimensional arrays<BR><BR>sometimes what i have done to evade that little problem is to either create a class which is essentially a multi-dimensional array and then have a dynamic, one-dimensional array of those array objects created - in essence, a collection of those puppies<BR><BR>or you may want to try to make a straight-forward array of arrays without creating the class - Bill W. has informed me that classes in vbscript aren&#039;t very performance-friendly

  4. #4
    Join Date
    Dec 1969

    Default RE: first of all...

    Can only redimension the top dimension of the array.

  5. #5
    ARRAY Guest

    Default TRUST ME!!!

    Trust me if I can do it without using an array then heavens forbid show me how. To clear up any confusion what I am attempting to do is display content from a db in a format like this:(example)<BR><BR>Category (Beans)<BR> * qustion (what is a bean)<BR> * question (how many beans are there in a Message board?)j/k<BR> * etc.<BR>Category (Carrot)<BR> * qustion (what is a carrot)<BR> * question (how long are they?) ha ha j/k<BR> * etc.<BR><BR>NOW ... In the DB each question is related to a specific category:<BR><BR>I am trying to eliminate the redundancy (example)<BR><BR>Bean &#124 qustion (what is a bean)<BR>Bean &#124 question (how many beans are there in a Message board?)j/k<BR>Bean &#124 etc.<BR><BR>Carrot &#124 qustion (what is a carrot)<BR>Carrot &#124 question (how long are they?) ha ha j/k<BR>Carrot &#124 etc.<BR><BR>Understand yet????

  6. #6
    Join Date
    Dec 1969

    Default Couple of different ways

    1. use data shaping to get you a recordset where field 1 contains category, field 2 contains another recordset that contains queestions in category then do<BR><BR>do while not rs1.eof<BR>response.write rs1("field1") & "<BR>"<BR>set rs2 = rs("field2").value<BR>do while not rs2.eof<BR>response.write rs2("field1") & "<BR>"<BR>loop<BR>loop<BR><BR>2. just get a recordset that repeats the category for each question, use get rows to conver to an array then check the first element to see if different from last if so write out category and question, else just question<BR><BR><BR>

  7. #7 Guest

    Default RE: TRUST ME!!!

    OK, here&#039;s how I would do it:<BR><BR>... initialize adodb objects ...<BR><BR>strSQL = "SELECT Categories, Question" & vbcrlf & _<BR> "FROM TBL1" & vbcrlf & _<BR> "WHERE product=&#039;whatever&#039;" & vbcrlf & _<BR> "ORDER BY Categories"<BR><BR> strSQL, oConn, adopenstatic<BR><BR>Loop Until rs.eof<BR> &#039;I use loop instead of do while because I <BR> &#039;prefer using trues where I can<BR><BR> if strCategory &#060;&#062; rs.fields("Categories").value then<BR> ... Code to write category out ...<BR> strCategory = rs.fields("Categories").value<BR> end if<BR><BR> ... Code to write Questions out ...<BR><BR> rs.movenext<BR><BR> &#039;this next is optional, but necessary if you&#039;re<BR> &#039;using nested &#060;UL&#062;&#039;s<BR> if strCategory &#060;&#062; rs.fields("Categories").value then<BR> ... Code close the category out ...<BR> end if<BR><BR>loop<BR><BR>Hope this helps

  8. #8
    Join Date
    Dec 1969

    Default *GUARANTEED*!! The answer!

    Your money cheerfully refunded if I am wrong.<BR><BR>******************<BR><BR>(1) Look at how you got REDIM PRESERVE to work in your first set of code:<BR>Dim catArray()<BR>...<BR>redim PRESERVE catArray(18, x-1) <BR><BR>Look at how it does *NOT* work in your second set of code:<BR>Dim QuestArray<BR>...<BR>redim PRESERVE QuestArray(x) <BR><BR>What did you do differently? Hmmm?? HINT: Notice the () in the first set of code.<BR><BR>****************<BR><BR>(2) Doing Redim Preserve is an EXPENSIVE operation. You should never bump the size of an array each time through a loop! Horrible performance! Find almost *any* other way to do it!<BR><BR>****************<BR><BR>(3) All this is done in one of the ASPFAQs I wrote: The first one you see in the category "Databases, General" if you click on the link to the ASPFAQs at the top right of this page.<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts