Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Databases section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 11th, 2003, 10:26 AM
Authorized User
 
Join Date: Sep 2003
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default session variables help

<%
'this loop starts the for loop to loop through each item in the collection
For Each item in Session.Contents
'if item is an object then it will say it can't display object
If IsObject(session.contents(item)) then
 Response.Write(item & ":Can't display object" & "<br>")
else
'this is the part i am lost ????
  If Isarray(session.Contents(item)) then
      response.Write "array: " & session.Contents(item)
    For each objarray in session.Contents(item)
       response.Write "<li>"& _
       session.Contents(item)(objarray)& "<br>"
    next
       response.write "</li>"
   else
response.Write(item & " : " & session.Contents(item) & "<br>")
    end if
end if

Next
%>

i got this code about sessions variable from wrox asp 3.0 book and i understand that it list all the session variables in the session collection, but the book doesnt explain what each section does. Please explain thanks.

Reply With Quote
  #2 (permalink)  
Old September 11th, 2003, 10:33 AM
Authorized User
 
Join Date: Sep 2003
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<%
For Each item in Session.Contents
response.Write(item & " : " & session.Contents(item) & "<br>")
Next
%>

well all im saying is what is the rest of the code for, ie objarray, etc. When all i can do is write these couple of lines and it does the same. thanks

Reply With Quote
  #3 (permalink)  
Old September 11th, 2003, 01:28 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi face,

If you store just single values in Session state, your code will work fine.

However, when you insert an array, the additional code block sees that it is an array, and performs some additional tasks on it. Personally, I don't think this code will run fine. AFAIK, you cannot directly retrieve elements from an array in Session state. You'll need to "materialize" it in the page as a local variable, and then loop through its items. I believe the following is a better solution to display both string items as arrays from Session state:

Code:
<%
Session("Test") = "Test Value"
Dim anArray(2)
anArray(0) = "Item 1"
anArray(1) = "Item 2"
anArray(2) = "Item 3"

Session("MyArray") = anArray

'this loop starts the for loop to loop through each item in the collection
For Each item in Session.Contents
    'if item is an object then it will say it can't display object
    If IsObject(session.contents(item)) then
        Response.Write(item & ":Can't display object" & "<br>")
    Else
        'this is the part i am lost ????
        If Isarray(session.Contents(item)) then
            Dim localArray
            localArray = session.Contents(item)
            For each myItem in localArray
                response.Write "<li>"& myItem 
                response.write "</li>"
            next
        Else 
                Response.Write(item & " : " & session.Contents(item) & "<br>")
        End if
    End if
Next
%>
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #4 (permalink)  
Old September 12th, 2003, 01:49 PM
Authorized User
 
Join Date: Sep 2003
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

dim anArray(9)

For i=1 to 3
  anArray(i-1)="item"


next

Session("MyArray") = anArray

'this loop starts the for loop to loop through each item in the collection
For Each item in Session.Contents
    'if item is an object then it will say it can't display object
    If IsObject(session.contents(item)) then
        Response.Write(item & ":Can't display object" & "<br>")
    Else
        If Isarray(session.Contents(item)) then
            Dim localArray
            localArray = session.Contents(item)
            For each myItem in localArray
                response.Write "<li>"& myItem
                response.write "</li>"
            next
        Else
                Response.Write(item & " : " & session.Contents(item) & "<br>")
        End if
    End if
Next

The problem i have with this is that it will write the 3 items, and the rest of it will be just bullets going all the way to the 9th array. Is there a way to fix it to just write just the 3 items?

Reply With Quote
  #5 (permalink)  
Old September 12th, 2003, 03:57 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Well, I don't think so. You declare the array to hold 10 elements (from 0 to 9) so it will display them all. Although you haven't assigned a value, there is still an element. Two work arounds:

1. Declare your array using dim anArray(2) so it holds just the two elements.

2. Check the value of the item in the loop:

For each myItem in localArray
    If Len(myItem) > 0 Then
        response.Write "<li>"& myItem
        response.write "</li>"
    End If
Next

This will write the array's contents only when there is a value in the array item. This can cause some unpredictable results; if your 2 item doesn't contain a value, but your third does, it will skip the second, but display the third item.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Session Variables in C# shikha09 C# 1 November 28th, 2006 10:38 AM
Session Variables testsubject Visual Studio 2005 8 March 8th, 2006 04:26 PM
Is it possible for me using session variables into see07 ASP.NET 1.x and 2.0 Application Design 4 March 9th, 2005 07:46 PM
Session Variables - help? Annoyamouse BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 11 August 31st, 2004 03:56 PM
Session Variables flesh_god Dreamweaver (all versions) 7 November 11th, 2003 05:52 PM



All times are GMT -4. The time now is 07:08 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.