php - 从表单运行数组

标签 php mysql sql arrays

我制作了一个基本的赛程生成器,它还为每个团队的得分生成输入字段。目标是让这些分数更新排行榜。

我已经快到了,但我被困在一个地方。注意:下面的代码暂时不会更新联赛表或进一步发送分数,因为我只是想先测试输出以确保其正常工作。

我在赛程页面上提交分数,提交后,我有一个循环,应该循环遍历每一行(例如团队 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>";

现在我遇到问题的代码,它输出最后一行的分数一次,然后输出平局/获胜/失败语句 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>";

关于php - 从表单运行数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21061634/

相关文章:

php - 创建可人工输入的、非顺序的唯一 ID 的良好做法

php - 仅从数组中显示一次

php - MySQL Select 内部函数访问其他地方的变量

sql - "IF"处或附近的 Postgres 语法错误

sql - 在 MySQL 中更新多个值

php - Twig - 在数据描述中的 href 中替换双引号

php - 在初始化时从 Mysql 获取数据

php - 使用 SimpleXML 获取属性和值

java - 我的 JDBC mySQL 语句有什么问题?显然我的 WHERE 子句搞砸了

php - 无法将 CSV 文件上传到数据库