iliketurtles
iliketurtles

Reputation: 48

Multiple pages session need to be added together?

I have a multi page that records numbers, I have a session that is being saved as on "submit.php"

$_SESSION['hello'] = $_POST['hello'];
echo  $_SESSION['hello'];

My problem is that it's replacing the session with only the previous one. How do i make it so they just add up?

For example,

<?php
if ($page2 == 2) { ?>

 <?php echo $numbers?>

<?php } else if ($page3 == 3) { ?>

  <?php echo $numbers + $numbers ?>

<?php } else if ($page4 == 4) { ?>

 <?php echo $numbers + $numbers + $numbers?>

Is there so that the sessions are being taken from each page and not replaced?

Upvotes: 0

Views: 62

Answers (4)

semsem
semsem

Reputation: 1192

Use php arrays:

<?php
$arr = array();
$arr[] = $_POST['hello'];
$_SESSION['hello'] = $arr;
?>

Then you can call any value at any page by using:

<?php
echo $_SESSION['hello'][0];
echo $_SESSION['hello'][1];
?>

Upvotes: 0

Andre
Andre

Reputation: 893

$sessionvar = $_SESSION['hello'];

Then you can add up the number if you want:

$sessionvar = $sessionvar + $number;

Then store the new value into the session:

$_SESSION['hello'] = $sessionvar; 

Upvotes: 0

Chris McClellan
Chris McClellan

Reputation: 1105

Are you using sessionstart();?

http://php.net/manual/en/features.sessions.php

Upvotes: 0

thecodeparadox
thecodeparadox

Reputation: 87083

Define $_SESSION['hello'] as an array then store your page number to it and retrieve as necessity.

$_SESSION['hello'] = array();

array_push($_SESSION['hello'], $_POST['hello']);

Upvotes: 1

Related Questions