Page 1 of 1

Naming a session variable

Posted: Mon May 19, 2003 7:41 am
by Skittlewidth
I'm trying to write a script for an online multiple choice test/exam, and thanks to my boss I have 4 days to do it.
I've been trying to swot up on sessions and I just want to know if the following is possible:
Can you use a variable as a variable name for a session variable? (Confused?)

Every time the user selects an answer and submits it, I want to write the value of that answer (A,B,C or D) to a new session variable. The name of this variable would be $_SESSION['answer_number of the question'].

The way the script would know what question the user had answered would be through another variable from the questions database. Can I just write $_SESSION['answer_$question_no'] or something similar, or is that just not possible?

I'm trying to avoid hardcoding the $_SESSION variable name into the script so that I don't have to have a new page per question (ie 40+ pages).

If the above made no sense at all, let me know and I'll try and explain a different way. In the mean time, I'll go test a few things out!
Thanks!
8)
Skittlewidth

Posted: Mon May 19, 2003 8:04 am
by twigletmac
You can definitely have dynamic array element names, e.g.:

Code: Select all

$array['prefix_'.$number] = 'something';
Mac

Posted: Mon May 19, 2003 8:22 am
by Skittlewidth
Thanks for clarifying that. I've got that bit working now. Hopefully I can go on to panicking about something else now!
:wink: