Wrox Programmer Forums
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #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.

  #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

  #3 (permalink)  
Old September 11th, 2003, 01:28 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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.
  #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?

  #5 (permalink)  
Old September 12th, 2003, 03:57 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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.


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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.