我无法在动态表中进行简单的数学运算(这会变得更加复杂)。
每次我提交结果时,它都应该加总并将总得分存储在$ players [$ player] ['score']中,但是一旦循环,它就会再次重置为零。
任何想法,我要去哪里错了?
$new_players = $_SESSION["players"];
//$_SESSION["players"] - brings in array an array like following
/* array (size=1)
'John' =>
array (size=3)
'previous' => int 0
'score' => int 0
'hand' => int 0 */
if (empty($players)) {//added as i thought $new_players = $_SESSION["players"]; might be blanking scores
$players = $new_players;
}
if (isset($_POST[$submit])) {
$new_array = $_POST['player'];
foreach ($players as $player =>$i) {
$players[$player]['hand'] = $new_array[$player]+0;//add to convert the $new_array[$player] to int
$players[$player]['previous'] = $players[$player]['hand'];
$players[$player]['score'] = $players[$player]['score'] + $players[$player]['hand'];
}
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" class="formAcc">
<ul style= "text-align: center">
<article>
<li>
<label>Current Standing</label>
<?php
echo '<table id="db_results">
<colgroup>
<col class="col15" />
<col class="col15" />
<col class="col15" />
<col class="col15" />
</colgroup>
<tr>
<th>Name</th>
<th>Previous Score</th>
<th>Current Score</th>
<th>Hand</th>
</tr>';
foreach ($players as $player =>$i) {
echo "<tr class=\"center\">
<td>". $player . "</td>
<td>". $i['previous'] . "</td>
<td>". $i['score'] . "</td>"
. '<td><input type="text" name="player['.$player.']" value="0"/></td>
</tr>';
}
echo "</table>";
?>
<input type="submit" name="<?php echo $submit?>" value="Input Scores" style="margin-top: 5px;"/>
</li>
</article>
</ul>
</form>
答案 0 :(得分:1)
在单击“提交”时,您没有将更改保存到$_SESSION
变量中,请尝试以下操作以提交提交处理程序:
if (isset($_POST[$submit])) {
$new_array = $_POST['player'];
foreach ($players as $player =>$i) {
$players[$player]['hand'] = $new_array[$player]+0;//add to convert the $new_array[$player] to int
$players[$player]['previous'] = $players[$player]['hand'];
$players[$player]['score'] = $players[$player]['score'] + $players[$player]['hand'];
}
/* overwrite $_SESSION['players'] */
$_SESSION['players'] = $players;
}