Reputation: 87
I have this code:
// Values of drop-down lists (cmbPosition)
$studPosition = array();
$i = 0;
foreach ($_SESSION['arrayNamePosition'] as $value) {
$studPosition[$i] = $_GET[$value];
$i++;
// Calculates the points for each student in the event
$points = ($_SESSION['noStudents']+1) - $_GET[$value];
echo $points;
$_SESSION['points'] = $points;
}
The code above loops through $_SESSION['arrayNamePosition']
which contains an array. Every thing works but $_SESSION['points'] = $points;
is where the problem is (This is the $_SESSION variable which contains $points
).
When I echo $points
in the current php form, it outputs: 583276
But when I echo $_SESSION['points']
inside a while loop in a different php form it only outputs the last element stored in $echo $_SESSION['points']
points which is 6.
How can I fix this so that echo $_SESSION['points']
outputs all the values of $points
in a different php form.
NOTE: I have also put echo $_SESSION['points']
inside a for loop but it still outputs the last value stored in $points. e.g. output: 666666
Thanks in advance.
Upvotes: 0
Views: 135
Reputation: 171
I guess you mean that the loop with echo $points;
outputs you 583276. Like the $_SESSION['points']
, it stores only one value, but you print the value each time.
I would suggest following changes:
$studPosition = array();
$i = 0;
$points = '';
foreach ($_SESSION['arrayNamePosition'] as $value) {
$studPosition[$i] = $_GET[$value];
$i++;
$points = $points.(($_SESSION['noStudents']+1) - $_GET[$value]);
}
echo $points;
$_SESSION['points'] = $points;
Upvotes: 0
Reputation: 456
Variable points is not an array, therefore it contains only last calculated element. Change it to array, then save each value as new element in array.
Upvotes: 0
Reputation: 4298
It's because $_SESSION['points'] itself is not an array. You should change your line to:
$_SESSION['points'][] = $points;
Upvotes: 3