如何从PHP循环中的不同行获取4个下拉列表值?

时间:2016-03-04 18:57:57

标签: php drop-down-menu foreach

我必须从循环中的不同行获取不同下拉列表的值。循环总是不相同,例如一次循环运行5次,下次循环可能是50次。请帮助我如何获得价值观。谢谢 这是代码:

'<th>
   <select name= "$allocation[]" class="form-control ">
       <option>Select Faculty</option>
   <?php
   foreach ($faculty_selected_course as $faculty) { ?>
       <option><?php echo $faculty;  ?></option>
   <?php
   }
   ?>
   </select> 
   </th>

   <th>
      <select name= "$allocation[]" class="form-control ">
            <option>Select Other Faculty</option>
      <?php
      foreach ($other_faculty as $otherfaculty) { ?>
            <option><?php echo $otherfaculty;  ?></option>
      <?php
      }
      ?>
      </select>
      </th>

      </tr>
      <?php goto again;
      }
      ?>
      </th>

      <th>
          <select name= "$allocation[]" class="form-control ">
              <option>Select Faculty</option>
          <?php
          foreach ($faculty_selected_course as $faculty) { ?>
              <option><?php echo $faculty;  ?></option>
          <?php
          }
          ?>
          </select>
          </th>

          <th>
              <select name= "$allocation[]" class="form-control ">
                  <option>Select Other Faculty</option>
          <?php
              foreach ($other_faculty as $otherfaculty) { ?>
              <option><?php echo $otherfaculty;  ?></option>
              <?php
              }
              ?>
         </select>
         </th>'

1 个答案:

答案 0 :(得分:0)

如果您有这样的数组:

$dropdown = array(
    'Faculty 1' => array(
        0 => 'f1 course zero',
        1 => 'f1 course one',
        2 => 'f1 course two'
    ),
    'Faculty 2' => array(
        4 => 'f2 course four',
        5 => 'f2 course five',
        6 => 'f2 course six'
    ),
    'Faculty 3' => array(
        0 => 'f3 course zero',
        1 => 'f3 course one',
        2 => 'f3 course two',
        4 => 'f3 course four'
    )
);

然后你可以生成这样的下拉列表:

$htmlstr = '';
foreach($dropdown as $faculty => $list){
    $htmlstr .= "<select name=\"".$faculty."\">\r\n";
    foreach($list as $key => $name){
        $htmlstr .= "<option value\"".$key."\">".$name."</option>\r\n";
    }
    $htmlstr .= "</select>\r\n";
}
var_dump($htmlstr);
//echo $htmlstr;