我有这个代码:
// 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;
}
上面的代码循环通过包含一个数组的$_SESSION['arrayNamePosition']
。一切正常,但问题出在$_SESSION['points'] = $points;
(这是包含$points
的$_SESSION变量)。
当我在当前php表单中回显$points
时,它输出:583276
但是,当我在不同php形式的while循环中echo $_SESSION['points']
时,它只输出存储在$echo $_SESSION['points']
点中的最后一个元素,即6。
如何修复此问题,使echo $_SESSION['points']
以不同的php形式输出$points
的所有值。
注意:我还将echo $_SESSION['points']
放入for循环中,但它仍然输出存储在$points中的最后一个值。例如输出:666666
提前谢谢。
$_SESSION['points'][] = $points;
变量点不是数组,因此它只包含最后一个计算的元素。将其更改为数组,然后将每个值保存为数组中的新元素。
我想你的意思是echo $points;
的循环输出583276。与$_SESSION['points']
一样,它只存储一个值,但每次都要打印该值。我建议进行以下更改:
$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;