SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: Array Help?

  1. #1
    SitePoint Wizard silver trophy Jeremy W.'s Avatar
    Join Date
    Jun 2001
    Location
    Toronto, Canada
    Posts
    9,123
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Array Help?

    [vbs]
    <%
    tmpcats=Array()
    t=0
    SET prod_cat = Con.Execute("SELECT DISTINCT CATEGORY_ID FROM PROD_CAT")

    WHILE NOT prod_cat.EOF
    tmpcats(t)=prod_cat("category_id")
    t=t+1
    prod_cat.MOVENEXT
    WEND

    for i = 0 to UBound(tmpcats)
    response.write i & " - " & tmpcats(i)
    next

    %>
    [/vbs]

    Is throwing up a "subscript out of range" error. I know this is simple, but my array stuff is a bit out of date (that and a coworker has the ASP manual for the day).
    SVP Marketing, SoCast SRM
    Personal blog: Strategerize
    Twitter: @jeremywright

  2. #2
    SitePoint Zealot BlitzCraig's Avatar
    Join Date
    Mar 2001
    Location
    Louisville, KY
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    Code:
    Dim tmpcats
    SET prod_cat = Con.Execute("SELECT DISTINCT CATEGORY_ID FROM PROD_CAT")
    If Not prod_cat.EOF Then
        tmpcats = prod_cat.GetRows
    End If
    prod_cat.Close
    Set prod_cat = Nothing
    For i = 0 To UBound(tmpcats)
        Response.Write i & " - " & tmpcats(i)
    Next
    Craig H. Rettig - Bitbucket Heaven
    To understand recursion, you must first understand recursion.

  3. #3
    SitePoint Wizard silver trophy Jeremy W.'s Avatar
    Join Date
    Jun 2001
    Location
    Toronto, Canada
    Posts
    9,123
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    [vbs]
    <%
    Dim tmpcats
    SET prod_cat = Con.Execute("SELECT DISTINCT CATEGORY_ID FROM PROD_CAT")

    If Not prod_cat.EOF Then
    tmpcats = prod_cat.GetRows
    End If

    prod_cat.Close
    Set prod_cat = Nothing

    For i = 0 To UBound(tmpcats)
    Response.Write i & " - " & tmpcats(i)
    Next
    %>
    [/vbs]

    Gives another subscript out of range error on: Response.Write i & " - " & tmpcats(i)
    SVP Marketing, SoCast SRM
    Personal blog: Strategerize
    Twitter: @jeremywright

  4. #4
    SitePoint Wizard silver trophy Jeremy W.'s Avatar
    Join Date
    Jun 2001
    Location
    Toronto, Canada
    Posts
    9,123
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Looking at the code, it's not exactly what I need it to do. I need to create an array with the category_id's that have products in it.

    Yours simply gives me the number of rows returned (which is good!).
    SVP Marketing, SoCast SRM
    Personal blog: Strategerize
    Twitter: @jeremywright

  5. #5
    \m/ R.I.P. Dimebag! \m/ JimBolla's Avatar
    Join Date
    Dec 2001
    Location
    erie, pa
    Posts
    1,130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the error with the original code is that it never defined a size for the array so i don't think you can use it like you are.

    the error with blitzcraig's code is that you're treating the array as a one-dimensionsional array when its actually a two-dimensional array.

    this slight change should make it work:

    Code:
    Dim tmpcats
    SET prod_cat = Con.Execute("SELECT DISTINCT CATEGORY_ID FROM PROD_CAT")
    If Not prod_cat.EOF Then
        tmpcats = prod_cat.GetRows
    End If
    prod_cat.Close
    Set prod_cat = Nothing
    For i = 0 To UBound(tmpcats, 2)
        Response.Write i & " - " & tmpcats(0, i)
    Next

  6. #6
    SitePoint Wizard silver trophy Jeremy W.'s Avatar
    Join Date
    Jun 2001
    Location
    Toronto, Canada
    Posts
    9,123
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Fair enough, but again, it doesn't fill the requirement for what I want to do. I hate nesting database connections, especially in the system we're making, so I just need to grab a small bit of data:

    array(0)=thing
    array(1)=thing2

    etc

    So that I can use that with a for i=0 to UBound(array) to run through the second half while the first is closed. A huge increase in performance as well as I won't be opening/closing the nested db connection each time through, as I'm currently having to do:

    [vbs]
    SET prod_cat = Con.Execute("SELECT DISTINCT CATEGORY_ID FROM PROD_CAT")

    response.write "<p><ul>"
    WHILE NOT prod_cat.EOF
    SET cats = Con.Execute("SELECT description FROM categories WHERE category_id=" & prod_cat("category_id") & " AND is_toplevel='T'")
    WHILE NOT cats.EOF
    response.write "<font face=""verdana"" size=""2""><b><li><a href=""books.asp?action=showcat&cat=" & prod_cat("category_id") & """>" & cats("description") & "</a></b></font><br>" & Chr(10)
    cats.MOVENEXT
    WEND
    cats.CLOSE
    SET cats=NOTHING
    prod_cat.MOVENEXT
    WEND
    response.write "</ul></p>"

    prod_cat.CLOSE
    SET prod_cat=NOTHING
    [/vbs]
    SVP Marketing, SoCast SRM
    Personal blog: Strategerize
    Twitter: @jeremywright

  7. #7
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,294
    Mentioned
    123 Post(s)
    Tagged
    1 Thread(s)
    So why don't you do this (assuming you have a description for every category?

    Code:
    SET prod_cat = Con.Execute("SELECT DISTINCT PC.CATEGORY_ID, DESCRIPTION FROM PROD_CAT PC, CATEGORIES C WHERE PC.CATEGORY_ID = C.CATEGORY_ID and is_toplevel='T'")
    
    if prod_cat.EOF then
    	' Do Nothing - Nothing Found
    else
    	response.write "<p><ul>"
    	Do Until prod_cat.EOF
    		response.write "<font face=""verdana"" size=""2""><b><li><a href=""books.asp?action=showcat&cat=" & _
    				prod_cat("category_id") & """>" & prod_cat("description") & "</a></b></font><br>" & Chr(10)
    		prod_cat.MOVENEXT
    	Loop
    	response.write "</ul></p>"
    end if
    prod_cat.Close 	:	set prod_cat = nothing
    <edit>grr....I wish I'd pay closer attention BEFORE I hit submit </edit>
    Last edited by DaveMaxwell; Mar 15, 2002 at 12:25.
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse

  8. #8
    SitePoint Enthusiast
    Join Date
    Feb 2002
    Location
    Birmingham, UK
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or this

    [VBS]
    SET prod_cat = Con.Execute("SELECT DISTINCT PC.CATEGORY_ID, DESCRIPTION FROM PROD_CAT PC, CATEGORIES C WHERE PC.CATEGORY_ID = C.CATEGORY_ID and is_toplevel='T'")

    If prod_cat.EOF Then
    Set prod_cat = Nothing: Con.Close: Set Con = Nothing
    Else
    MyArr = prod_cat.GetRows
    Set prod_cat = Nothing: Con.Close: Set Con = Nothing

    Response.Write "<p><ul>"

    For i = LBound(MyArr,2) To UBound(MyArr,2)
    Response.Write "<font face=""verdana"" size=""2""><b><li><a href=""books.asp?action=showcat&cat="
    Response.Write MyArr(0,i) & """>"
    Response.Write MyArr(1,i) & "</a></b></font><br>" & Chr(10)
    Next
    Response.Write "</ul></p>"
    End If
    [/VBS]

    You could put the cleanup lines in a function, and put the 3 Response.Write's into a function. GetRows is one way to do things... GetString is usually more efficient as you could do the whole loop in one statement.

    GL ;-)

  9. #9
    SitePoint Wizard wdmny's Avatar
    Join Date
    Jul 2000
    Location
    Here
    Posts
    1,010
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe it is about not setting the size of the array. I know that if you use a Split(), you dont have to, but in this looping you are doing, you probably need to redim the array within the loop.

    Code:
    ...
    
    intUB = UBound(tmpcats)
    intUB = intUB + 1
    
    Redim tmpCats(intUB)
    
    ...


Bookmarks

Posting Permissions

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