Page 1 of 2

using session to store data and use at a later stage

Posted: Fri Nov 11, 2005 7:15 am
by will83
Hi there,

I am building an online insurance booking form which over several screens will take details of people to be insured.

On the first screen, the client specifies the amount of people he wants to insure. On the second screen I have used a loop to create the correct amount of spaces to take the details of each person he wants to insure.

Now where i'm getting stuck (and this is probably something strait forward) is how I store each of these person's details in the session and then recall them a couple of screens later to firstly confirm the client's details but then also to save onto the server for the insurance to be issued.

Information such as telephone number and address for example is easy because I know that there will only be one variable name to store in the session, but with the list of client's details I am not going to know how many there as that is controlled by the person entering in the amount on the front page.


I have got as far as creating a loop which as well as creating the correct amount of spaces for the details but it also names each text box a unique name i.e. name1, age1, name2, age2, name3, age3 etc.

Do I add something to the loop which will add each of the details to a session variable eerytime it loops?

Any help or suggestions would be really appreciated.

Many thanks

will

Posted: Fri Nov 11, 2005 7:26 am
by Jenk
Using session in PHP is easy :)

ensure that you have:

Code: Select all

<?php
session_start();

//rest of page

?>
on every page, and then anything stored in the session will be in the superglobal $_SESSION

as an example, page1.php:

Code: Select all

<?php
session_start();

$_SESSION['variable'] = 'Hello world!';

header('Location: http://www.somesite.com/page2.php');
exit();

?>
page2.php:

Code: Select all

<?php
session_start();

echo $_SESSION['variable']; //outputs 'Hello world!'

?>
:)

Posted: Fri Nov 11, 2005 8:43 am
by will83
Hi, yes,and thank you, I found that out last night.

but my problem lies how do I store my dynamically created client details in the session variables to then be able to extract at a later page.

i.e.

PAGE1: user wants to insure 3 people.

PAGE2: 3 rows of text box's are created for user to enter details.

PAGE3: just a page for inserting address etc.

PAGE4: All of the details are displayed, incl the three rows that the client entered on PAGE2.


You see the 3 rows are dynamically created and the amount of rows produced is specified by the amount the user enters on PAGE1.

Do you know what I can do :?: :oops:

Thanks

Posted: Fri Nov 11, 2005 8:47 am
by n00b Saibot
Page 1: no requirement of session
Page 2: $_SESSION['people'][0-2][necessary details];
Page 3: $_SESSION['address'];
Page 4: print all the $_SESSION vars...

simple :wink:

Posted: Fri Nov 11, 2005 9:30 am
by will83
Hi thanks for the reply.

What is the [0-2] for?

Will

Posted: Fri Nov 11, 2005 9:35 am
by Jenk
just to save typing 3 lines with [0], [1] and [2]

Posted: Fri Nov 11, 2005 9:46 am
by will83
Ok I see

But the number of lines are dependent on how many the ppl the user wants to insure.

So how could I dynamically change that number in the variable assignement according to the amount needed to be insured and more so how do I recall that correct amount when I display them on screen.

I think I need to go and read a tutorial book.

Posted: Fri Nov 11, 2005 9:50 am
by Jenk
$_SESSION['people'][$i]['stuff']

Where $i is the dynamic variable.

Posted: Fri Nov 11, 2005 9:52 am
by n00b Saibot
count() my boy :wink:

$total_rows = count($_SESSION['people']);

Posted: Fri Nov 11, 2005 10:27 am
by will83
Ok -


You say I use:

$total_rows = count($_SESSION['people']);


But the variables ive added are named with a number on the end:

people1, people2 etc.

So is there a way around this?

Thanks for helping.

Will

Posted: Fri Nov 11, 2005 10:47 am
by n00b Saibot
uh oh... that's not the way i told you :)
so, instead of using $_SESSION['people1'], $_SESSION['people2'] etc. you use $_SESSION['people'][0] & $_SESSION['people'][1] since arrays start from 0 :wink:

Posted: Fri Nov 11, 2005 10:58 am
by will83
Ok thanks for that -

So when I come to displaying the variableson screen I can create a count loop and do:

Code: Select all

<?php
$pcount = 6;
$i = 0;

while ($i < $pcount) {

print "<tr>";
print "<td>";
print "$_SESSION['people'][$i]";
print "</td>";
print "</tr>";

$i++;
}
?>


?

Posted: Fri Nov 11, 2005 11:19 am
by n00b Saibot
nah!

Code: Select all

<?php 
$pcount = count($_SESSION['people']);

for($i = 0; $i < $pcount; $i++) {
print "<tr>"; 
print "<td>"; 
print $_SESSION['people'][$i]['whatever'];
print "</td>"; 
print "</tr>"; 
} 
?>
pay attention to the 7th line. all elements in the sub-array would not be printed automatically... you have to print them by explicitly coding the indices...

and for god's sake use PHP tags for posting code...

Posted: Fri Nov 11, 2005 11:25 am
by Jenk

Code: Select all

<?php

foreach($_SESSION['people'] as $person) {
    echo "<tr><td>{$person['whatever']}</td></tr>\n";
}

?>

Posted: Fri Nov 11, 2005 11:25 am
by Charles256
man. I don't even wanna be a mod, I just want to have the power to add PHP tags. LOL.