【问题标题】:Dynamically Generating Select OptGroup Options in PHP在 PHP 中动态生成 Select OptGroup 选项
【发布时间】:2014-01-22 18:56:11
【问题描述】:

我有一个数组如下,我想构建一个嵌套的 Select DropDown 选项

array(
    (int) 0 => array(
        'ProductCategory' => array(
            'id' => '5',
            'name' => 'Mud',
            'parent_id' => '0'
        ),
        'children' => array(
            (int) 0 => array(
                'ProductCategory' => array(
                    'id' => '6',
                    'name' => 'Sand',
                    'parent_id' => '5'
                ),
                'children' => array(
                    (int) 0 => array(
                        'ProductCategory' => array(
                            'id' => '7',
                            'name' => 'Soil',
                            'parent_id' => '6'
                        ),
                        'children' => array()
                    )
                )
            )
        )
    ),
    (int) 1 => array(
        'ProductCategory' => array(
            'id' => '8',
            'name' => 'House',
            'parent_id' => '0'
        ),
        'children' => array(
            (int) 0 => array(
                'ProductCategory' => array(
                    'id' => '9',
                    'name' => 'Door',
                    'parent_id' => '8'
                ),
                'children' => array(
                    (int) 0 => array(
                        'ProductCategory' => array(
                            'id' => '11',
                            'name' => 'Latch',
                            'parent_id' => '9'
                        ),
                        'children' => array()
                    ),
                    (int) 1 => array(
                        'ProductCategory' => array(
                            'id' => '12',
                            'name' => 'Glass',
                            'parent_id' => '9'
                        ),
                        'children' => array(
                            (int) 0 => array(
                                'ProductCategory' => array(
                                    'id' => '13',
                                    'name' => 'Semi ',
                                    'parent_id' => '12'
                                ),
                                'children' => array()
                            )
                        )
                    )
                )
            ),
            (int) 1 => array(
                'ProductCategory' => array(
                    'id' => '10',
                    'name' => 'Window',
                    'parent_id' => '8'
                ),
                'children' => array()
            )
        )
    )
)

我正在寻找一种有效的方法来循环和创建 HTML 代码,如下所示。我不确定它会有多少级。所以我需要一个循环证明。

<select>
    <optgroup label="Level One">
     <option> A.1 </option>
     <optgroup label="Level Two">
      <option> A.B.1 </option>
     </optgroup>
     <option> A.2 </option>
    </optgroup>
</select>

通过下拉菜单的嵌套外观示例

News
--Sport
----Sky
----Football
------Football-1
--War
----War1
--tech
Article
--tech
----tech2
------tech3
--php
--linux

【问题讨论】:

    标签: php html cakephp option optgroup


    【解决方案1】:

    select标签中使用多级深度没有解决方案(HTML限制)。

    但是您可以使用HtmlHelper::nestedList(); 构建没有optgroup 的简单下拉列表

    【讨论】:

      【解决方案2】:

      来自 CI 表单助手:

      Click here

      function form_dropdown($name = '', $options = array(), $selected = array(), $extra = ''){
          if ( ! is_array($selected))
          {
              $selected = array($selected);
          }
      
          // If no selected state was submitted we will attempt to set it automatically
          if (count($selected) === 0)
          {
              // If the form name appears in the $_POST array we have a winner!
              if (isset($_POST[$name]))
              {
                  $selected = array($_POST[$name]);
              }
          }
      
          if ($extra != '') $extra = ' '.$extra;
      
          $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
      
          $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
      
          foreach ($options as $key => $val)
          {
              $key = (string) $key;
      
              if (is_array($val) && ! empty($val))
              {
                  $form .= '<optgroup label="'.$key.'">'."\n";
      
                  foreach ($val as $optgroup_key => $optgroup_val)
                  {
                      $sel = (in_array($optgroup_key, $selected)) ? ' selected="selected"' : '';
      
                      $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
                  }
      
                  $form .= '</optgroup>'."\n";
              }
              else
              {
                  $sel = (in_array($key, $selected)) ? ' selected="selected"' : '';
      
                  $form .= '<option value="'.$key.'"'.$sel.'>'.(string) $val."</option>\n";
              }
          }
      
          $form .= '</select>';
      
          return $form;
      }
      

      例子:

      <?php
      $options = Array(
          "value1" => "label1",
          "value3" => "label2",
          "value3" => "label3"
      );
      echo form_dropdown("my_field_name",$options,"value2","class='the_class_of_my_obj'");
      ?>
      

      示例 2:

      <?php
      $options = Array(
          Array(
              "value1.1" => "label 1.1",
              "value1.2" => "label 1.2"
          ),
          "value3" => "label2",
          "value3" => "label3"
      );
      echo form_dropdown("my_field_name",$options,"value2","class='the_class_of_my_obj'");
      ?>
      

      【讨论】:

      • 这可以创建多级吗?
      • 是的...我举个例子
      • 他不只生成 2 个级别。不幸的是我使用 cakephp 而不是 CI :(
      • select 标签 不能 超过 2 个级别,阅读我的答案。
      • 但是您不必使用CI来使用此功能,只需将其粘贴到您的代码中即可享受它!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-23
      • 1970-01-01
      • 2017-08-17
      • 1970-01-01
      • 1970-01-01
      • 2019-05-01
      • 2014-01-02
      相关资源
      最近更新 更多