using session to store data and use at a later stage

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
will83
Forum Commoner
Posts: 53
Joined: Thu Nov 10, 2005 3:13 pm

using session to store data and use at a later stage

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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!'

?>
:)
User avatar
will83
Forum Commoner
Posts: 53
Joined: Thu Nov 10, 2005 3:13 pm

Post 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
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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:
User avatar
will83
Forum Commoner
Posts: 53
Joined: Thu Nov 10, 2005 3:13 pm

Post by will83 »

Hi thanks for the reply.

What is the [0-2] for?

Will
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

just to save typing 3 lines with [0], [1] and [2]
User avatar
will83
Forum Commoner
Posts: 53
Joined: Thu Nov 10, 2005 3:13 pm

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

$_SESSION['people'][$i]['stuff']

Where $i is the dynamic variable.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

count() my boy :wink:

$total_rows = count($_SESSION['people']);
User avatar
will83
Forum Commoner
Posts: 53
Joined: Thu Nov 10, 2005 3:13 pm

Post 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
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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:
User avatar
will83
Forum Commoner
Posts: 53
Joined: Thu Nov 10, 2005 3:13 pm

Post 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++;
}
?>


?
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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...
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

<?php

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

?>
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

man. I don't even wanna be a mod, I just want to have the power to add PHP tags. LOL.
Post Reply