从2个数组填充的下拉列表中获取表单值时遇到问题

时间:2018-01-08 11:30:06

标签: php html arrays forms form-submit

Staff数组包含具有唯一ID的员工姓名。 名册数组使用唯一ID来确定当天的工作人员。

我试图获得更新的名单'按下提交时发送的数组,但我似乎无法从表单中获取任何值。我做了一些愚蠢的事,但我看不到树上的福雷斯特。

$roster array 
Array
(
[0] => Array
    (
        [0] => date
        [1] => id[s]
    )

[1] => Array
    (
        [0] => 25/12/2017-31/12/2017
        [1] => 1
        [2] => 2
    )

[2] => Array
    (
        [0] => 01/01/2018-07/01/2018
        [1] => 2
        [2] => 3
    )

[3] => Array
    (
        [0] => 08/01/2018-14/01/2018
        [1] => 2
        [2] => 1
    )

[4] => Array
    (
        [0] => 15/01/2018-21/01/2018
        [1] => 8
        [2] => 1
        [3] => 3
    )

[5] => Array
    (
        [0] => 22/01/2018-28/01/2018
        [1] => 2
    )

[6] => Array
    (
        [0] => 29/01/2018-04/02/2018
        [1] => 2
        [2] => 1
    )

)

$staff array
Array
(
[0] => Array
    (
        [Team] => RED
        [Name] => Person A
        [ID] => 001
    )

[1] => Array
    (
        [Team] => BLUE
        [Name] => Person B
        [ID] => 002
    )

[2] => Array
    (
        [Team] => RED
        [Name] => Person C
        [ID] => 003
    )

[3] => Array
    (
        [Team] => BLUE
        [Name] => Person D
        [ID] => 004
    )

[4] => Array
    (
        [Team] => RED
        [Name] => Person E
        [ID] => 005
    )

[5] => Array
    (
        [Team] => Test Group
        [Name] => Test Person
        [ID] => 006
    )

 )

<?php
$teams=array();
$last = count($staff) - 1;
foreach ($staff as $i => $row)
{
    $isFirst = ($i == 0);
    $isLast = ($i == $last);
    array_push($teams,$row['Team']); //push all teams into on array to find the number of teams in total
}

echo "<pre>";
print_r($roster);

print_r($staff);

print_r($teams);

echo "</pre>";
$uniqueteams=array_unique($teams);

echo '<form method="post" action="save.php" id="rosterform">';

echo '<table border="1"><tr id="heading">';
//print the team names
print "<td>Week&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date</td>";
foreach ($uniqueteams as $i)
  {
  print "<td>".$i."</td>".PHP_EOL;
  }
echo "</tr>";

foreach (range(0, 52) as $wknumber) {
  echo "<tr>";
  echo "<td>";
  echo "week ".$wknumber;
  echo "</td>";

  foreach ($uniqueteams as $i)
    {
    echo "<td><div><select name=\"rostered\">".PHP_EOL;
    foreach ($staff as $j => $row){
      if ($row['Team']==$i){
         echo '<option value="'.$row['ID'].'">'.$row['Name'].'</option>'.PHP_EOL;
         }
      }
    echo "</select></div></td>".PHP_EOL;
    }
 }
echo '</table>';
//save the roster.csv file back
echo '<input type="submit" name="save" value="save">';
echo '</form>';

?>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

foreach ($uniqueteams as $i)
    {
        echo "<td><div><select name='rostered".$wknumber."[]'>".PHP_EOL;
        foreach ($staff as $j => $row){
            if ($row['Team']==$i){
                echo '<option value="'.$row['ID'].'">'.$row['Name'].'</option>'.PHP_EOL;
            }
        }
        echo "</select></div></td>".PHP_EOL;
    }