从表单中遍历数组

DJC

我制作了一个基本的夹具生成器,它还为要输入的每个团队的得分生成输入字段。目的是让这些分数更新排名表。

我快到了,但是被困在一个部分上。注意:下面的代码暂时不会更新联盟表或发送任何比分,因为我只是想先测试输出以确保其正常工作。

我在比赛页面上提交分数,一旦提交,我就有一个循环循环(例如1-12队,2 -15队)并找出获胜者。现在问题出在这里-我的循环仅返回最后一行的分数并计算出获胜者,然后重复(第2队为获胜者)19次(有19行固定装置)。

我无法解决的是我的数据是否在循环的每次迭代中都被覆盖,或者可能(而且我认为这种可能性更大),因为数据不正确,因此仅考虑了最后一行数组格式可以循环通过。

这是一些代码。注意$ teams是来自上一页表单输入的数组(用户输入团队名称,下面的代码生成固定装置列表,带有用于输入分数的框);

$counter=0;

foreach ($teams as $team) {


  foreach ($teams as $opposition) {

    if ($team != $opposition) {

    $str = <<<EOF
    <input type="hidden" name="team1" value="$team[1]">
    <input type="hidden" name="team2" value="$opposition[1]">
    <tr><td>Row $counter<input type="hidden" value="$counter" name="row1"><td><input          type="hidden" name="team_id" class="invis" value="$team[0]"><td><input type="text"     name="team1_score"> $team[1] 
        <td>  versus  <td> <input type="hidden" value="$opposition[0]"><td> $opposition[1]  <td><input type="text" name="team2_score"><td>Row $counter<input type="hidden" value="$counter"    name="row2"></tr>
    <input type="hidden" name="fixtures" value="$counter">

EOF;

      echo $str;
      $counter++;

      }
      }
}

echo "<hr><input type=\"submit\" value=\"Go\">";
echo "</form>";
echo "</table>";

现在我遇到了问题的代码,它输出最后一行ONCE的得分,然后输出19次平局/胜利/失败陈述(固定装置的数量)...

$team1=$_POST['team1'];
$team2=$_POST['team2'];
$row1=$_POST['row1'];
$row2=$_POST['row2'];
$fixtures=$_POST['fixtures'];
$team_id=$_POST['team_id'];
$team1_score=$_POST['team1_score'];
$team2_score=$_POST['team2_score'];
$games=$_POST['games'];


$games=array('TeamOne: ' =>$team1, 'Goals: '=> $team1_score, 'TeamTwo: ' => $team2, 'Goals2:      '=>$team2_score);
$row=0;
while ($row<$fixtures) {
foreach ($games as $key=>$value) {
 echo "$key  $value  <br>";
} 


if ($team1_score > $team2_score) {
  echo "$team1 are the winners";
  $row++;
}
else if ($team2_score > $team1_score) {
  echo "$team2 are the winners";
  $row++;
}
else {
echo "Drawed";
$row++;
}
}

因此,这会输出团队和页面上最后一场比赛的得分,然后(取决于得分)将获胜者重复或抽签19次。

任何帮助将非常感激。

非常感谢

鲁本

您以相同的格式编写了19次相同的属性名称,因此您将只收到一个元素。尝试更改数组的输入,然后您将收到一个可以在PHP中正常迭代的元素数组。

    $counter=0;

    foreach ($teams as $team) {


      foreach ($teams as $opposition) {

        if ($team != $opposition) {

        $str = <<<EOF
        <input type="hidden" name="team1[]" value="$team[1]"/>
        <input type="hidden" name="team2[]" value="$opposition[1]"/>

        <input type="hidden" name="team1_score[]" />
        <input type="hidden" name="team2_score[]" />

// etc...

    EOF;

          echo $str;
          $counter++;

          }
          }
    }

    echo "<hr><input type=\"submit\" value=\"Go\">";
    echo "</form>";
    echo "</table>";

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章