Passing results from query to variables for use on many other pages

I know that this, or very similar, questions have been asked and answered in the past but I cannot get this to work and would really like some help.

I have a login page and two tables in a MySQL DB.
Table 1 is a table of Contacts and
Table 2 is the Login table which just has contact_id, username and password.

The user enters username and password in the form fields and then submits

image

So there is the Database Connection, followed by Security Provider and Security Restrict. I then have a Database Query to join the two tables based on the contact_id. The results of this query are needed throughout the rest of the app. For instance…

  • Forename
  • Surname
  • Email
  • Job_position
  • Avatar
  • Notes

image

How can I put these results into either session variables or cookies for use on other pages?
And can it be done at this point or should the query be run on the next page?

I have looked at Using Session Variables, Logged User Session Name, Getting Details of the Logged In User and Binding them to your page and others but I can’t see how to populate variables without having to populate a form. I am sure it is simple but I can’t see it. Cheers all.

Hello,
Which value would you like to store in a session/cookie and where do you need to use it? You say in other pages, but does that mean in other server actions?

Yes, add can add session stage in your server action like this

image

Hi @Teodor, I would like to store Forename, Surname, Email, Job_position, Avatar, Notes from the Contacts table and use these in the header of other pages. I would also like to use the contact_id in various server actions.

Hope that helps.

Hi Brian, does that store ALL the results from the query?
And if so how do I then use them on the following pages?

Why not including the user details server action in the a SSI include and reuse it on all pages (as probably your header is)?

As for user id in different server actions, you can just add the same security provider step in each action (use the folder icon to load the already defined one) it returns the user id so you can use it in any step in any action.

yes, it stores all the results of the query in the session array and it then available in app connect via the session storage manager

1 Like

I thought you had to use a repeat to access the values to store in a variable (even if there is only one record returned).

image

As Brian mentioned, you can store all values returned in one go (as he demonstrated) or only store specific values (as above).

3 Likes

My SSI’s are PHP Includes (not SSI File or SSI Virtual), does that make a difference?

I did not realise Server Actions could be part of the Include page. Does this mean the query is run each time the header appears? If so would it not be better to use some storage such as session or cookies?

I have only been putting Server Actions at the top of each page. Part of the reason is that I can’t edit the header on the page it is included in, and I can’t edit or see results when trying to edit the header file directly. Again, I may be doing something wrong here and so willing to be educated.

As for your second point, again I did not realise that the Security Provider is transportable. Does this hold information very much like a session variable?

Yes, my mistake Tom, always forget that even if only 1 result returned, still need a repeat. I put the action together untested to demo it and forgot that. Sorry for any confusion

1 Like

As the identity of the current user is stored as a session you can use it to query/ re-query the user details at any time. If the page has a security provider it can be accessed from the picker.
This may help

2 Likes

No, I mean any SSI including PHP includes.

Yes it can be part of an include page, useful when you want to reuse data. And yes it will run the server action on each reload, but still you can cache its value using the cache option available in the app connect panel - so it will run only once.
Once the user logs out you reload the action and clear its cached data :slight_smile:

You define one security provider per site. You click the link icon when you define it to make it globally available across the whole site. Then in every server action you add a security provider step, click the folder icon, load the globally available one and you are done. All the steps after that have access to the Id in the dynamic data picker…

1 Like

Hi guys, brilliant responses as always. Many thanks for the guidance. Now to get my head stuck into Wappler again and get this thing working :smile:
Cheers

2 Likes

Hi Brian, still stuck. I have stored all the results of the query in a session variable

image

But when I come to enter the results into page 2 using Session Storage Manager, there is nothing showing in the Session Storage Manager.

I can see that there is information in a session variable when I look at the page using Chrome.

How do I fetch the info and place on the page?

Yes see Toms reply. you have to put the session stage in a repeat, my mistake. Dont forget to check output.

image

1 Like

and dont forget to add the security provider to the page

1 Like

Hi guys, sorry but I’m starting to feel a bit stupid now because I can’t get this to work. :frowning_face:

These are my action steps on my login page …

image

I have the DB connection > Security Provider > Security Login > DB Query > Query Repeat > and individual Set Session statements for each of the returned fields in the Query.

On my next page (not the login page) I want to enter the $_SESSION.nickname where the “xx” is in the paragraph marks but I really don’t know how to.

I have inserted the Session Storage Manager under App Structure on the page and created a single variable inside it called “nickname”.

If I put my cursor inside the paragraph tags I don’t get the option to Insert Binding. If I put it outside of the paragraph tags I can select the recently created “nickname” variable from the Session Storage Manager and put it on the page but nothing shows on the rendered page.

I just need a quick idiots guide to help me on my way.
All I am trying to do is get the user’s nickname to show on a page.

I suspect my problems is that I have been so used to hand-coding (albeit in DW), using lots of snippets of code from here and there, and without the use of any frameworks, without any previous DMXzome extensions etc. that I just struggle with the simplest of tasks.

Seems like you are making it more complicated than it needs to be to display user data.

Have a look at the following tutorial.

Unless I totally misunderstand what you are trying to do.

Hi @brad, you are now making me question my own sanity :laughing: and it’s getting late.

I have read that numerous times and I can get that to work. What I am trying to do is to execute a query once when the user logs in, and to then store the results of the query into a session variable to be used throughout the app.

Tom and Brian have tried to explain above but they don’t mention how to output the session variable.

I wanted to prevent the same query being triggered over and over again as the user opens the pages of the app. This app is an administration system and will be used throughout the day by the users.

Maybe you are right, maybe I am trying to make it more complicated than it should be, but my simple brain says do a complex query, dump the results into a variable and keep calling the variable.

I hope you’ve managed to maintain your sanity since yesterday. I would follow Teodor’s suggestion and use a server action linked to the user details table and put it in an include.

Regarding displaying session values on a page, I asked about this long ago on a DMX forum. I don’t think it’s possible or perhaps I just haven’t discovered how to do it. If it’s not a bug, it’s at least confusing, given you can select them as a dynamic value. Perhaps it’s because the values are stored on the server rather than the client, unlike standard cookies where the data is stored locally.

2 Likes