Nesting arrays in $_SESSION

Are you using LiveCode to create server scripts or CGIs?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
matthew@majic.co.uk
Posts: 3
Joined: Wed Mar 29, 2017 9:19 am

Nesting arrays in $_SESSION

Post by matthew@majic.co.uk » Wed Mar 29, 2017 10:07 am

Hi, this is my first time here and I'm a relative LC newbie.

I am developing a LC Server CGI system that acts as a client web portal for an insurance business, by this I mean the client will log in and be able to see a whole host of information about their account. They'll be able to see their policy history, details of cover and download insurance certificates etc.

The basics are currently in place, clients can log in and the system connects to the database and pulls back their data.

HOWEVER, I would like to pre-load most of the important data during log in. For this I'm loading the data into $_SESSION, which works wonderfully for the basic user data (company name etc.)

As soon as I try to store an array of data in $_SESSION it simply doesn't work (e.g. data about each of their policies). If I store this data in a global array variable it works fine for the very next page loaded but it of course disappears for the rest of the session.

Code below for loading the data from the DB

Code: Select all

//load client info
put $_SESSION ["username"] into var1
put "SELECT * FROM client WHERE client_number_display_text_c = :1" into tQuery
put revQueryDatabase(tDatabaseID, tQuery, "var1") into tCursor

put revDatabaseColumnNamed(tCursor, "_pk_client") into $_SESSION["pk_client"]
put revDatabaseColumnNamed(tCursor, "client_account_status") into $_SESSION["account_status"]
put revDatabaseColumnNamed(tCursor, "client_category_aec") into $_SESSION["category"]
put revDatabaseColumnNamed(tCursor, "client_display_name_c") into $_SESSION["display_name_c"]

//load policy info
put $_SESSION["pk_client"] into var1
put "SELECT * FROM policy WHERE " & quote & "_fk_client" & quote & " = :1" into tQuery
put revQueryDatabase(tDatabaseID, tQuery, "var1") into tCursor
					
if tCursor is an integer then
	put 1 into i

	put revDatabaseColumnNamed(tCursor, "_fk_broker") into $_SESSION["policies"][i]["fk_broker"]
	put revDatabaseColumnNamed(tCursor, "_pk_policy") into $_SESSION["policies"][i]["pk_policy"]
	put revDatabaseColumnNamed(tCursor, "policy_date_expiry_actual_aec") into $_SESSION["policies"][i]["actual_expiry_date"]
	put revDatabaseColumnNamed(tCursor, "policy_date_inception_aec") into $_SESSION["policies"][i]["inception_date"]
	put revDatabaseColumnNamed(tCursor, "policy_display_cover_products_c") into $_SESSION["policies"][i]["display_products_c"]
	put revDatabaseColumnNamed(tCursor, "policy_total_price_inc_tax_admin_c") into $_SESSION["policies"][i]["total_price_inc_tax_admin_c"]
end if
(I'll be using a loop to read all the policy data in once I get this bit working, hence the 'i' variable)

When displaying the main screen I then use the code

Code: Select all

put "<tr>"
put "<td>" & $_SESSION["policies"][1]["display_products_c"] & "</td>"
put "<td>" & $_SESSION["policies"][1]["inception_date"] & "</td>"
put "<td>" & $_SESSION["policies"][1]["actual_expiry_date"] & "</td>"
put "<td>" & $_SESSION["policies"][1]["total_price_inc_tax_admin_c"] & "</td>"
put "</tr>"
This simply displays a blank table row. I've tried putting $_SESSION into an array and using Combine but I simply get the top level data and nothing from the nested array

As a test I used the following code

Code: Select all

put "<tr>"
put "<td>" & $_SESSION["display_name_c"] & "</td>"
put "<td>" & $_SESSION["policies"][1]["inception_date"] & "</td>"
put "<td>" & $_SESSION["policies"][1]["actual_expiry_date"] & "</td>"
put "<td>" & $_SESSION["policies"][1]["total_price_inc_tax_admin_c"] & "</td>"
put "</tr>"
In this case the Client's Display Name shows the correct data in the first column - all other columns are blank.

I have used exactly the same code but instead of $_SESSION I simply use gUser (defined as global gUser). When using gUser in place of S_SESSION all data displays exactly as expected - but doesn't persist.

It appears as though $_SESSION isn't able to store nested arrays. If this is the case then I'll simply store a text list of keys (cr delimited probably) in $_SESSION["policy_keys"] - and then use AJAX to load the data on the fly when needed. So I'm aware there are ways around it, but I thought I'd ask before making further changes.

I'm using LiveCode Server 8.1.2, on Windows Server 2016 Datacenter

Any help and guidance is most welcome

Ledigimate
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 132
Joined: Mon Jan 14, 2013 3:37 pm

Re: Nesting arrays in $_SESSION

Post by Ledigimate » Wed Mar 29, 2017 7:39 pm

What if you put $_SESSION["policies"] into its own variable, modify it, and put it back into $_SESSION["policies"]?

Code: Select all

put $_SESSION["policies"] into policies
#...
put revDatabaseColumnNamed(tCursor, "policy_date_expiry_actual_aec") into policies[i]["actual_expiry_date"]
#...
put policies into $_SESSION["policies"]
and when displaying the policy data

Code: Select all

put $_SESSION["policies"] into policies
# ...
put "<td>" & policies["inception_date"] & "</td>"
#...
010100000110010101100001011000110110010100111101010011000110111101110110011001010010101101010100011100100111010101110100011010000010101101001010011101010111001101110100011010010110001101100101

Post Reply

Return to “CGIs and the Server”