使用phpgroup的optgroup生成下拉列表

时间:2018-07-21 04:36:31

标签: php arrays laravel

我正在使用laravel创建一个电子商务网站。我有一个问题要从数组集生成下拉选择。下面是数组:

array(

 [0] => Array
    (
        [name] => Women's Clothing
        [id] => 16
        [parentid] => 0
        [children] => Array
            (
                [0] => Array
                    (
                        [name] => Tops
                        [id] => 411
                        [parentid] => 16
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [name] => Blouse
                                        [id] => 6556
                                        [parentid] => 411
                                    )

                                [1] => Array
                                    (
                                        [name] => Crop Tops
                                        [id] => 6557
                                        [parentid] => 411
                                    )

                            )

                    )

                [1] => Array
                    (
                        [name] => Women's Outerwear
                        [id] => 2262
                        [parentid] => 16
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [name] => Sweaters
                                        [id] => 6570
                                        [parentid] => 2262
                                    )

                                [1] => Array
                                    (
                                        [name] => Cardigans
                                        [id] => 6571
                                        [parentid] => 2262
                                    )

                            )

                    )
          )
)
[1] => Array
    (
        [name] => Health & Beauty
        [id] => 129
        [parentid] => 0
        [children] => Array
            (
                [0] => Array
                    (
                        [name] => Face Make Up
                        [id] => 2450
                        [parentid] => 129
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [name] => Powder & Compacts
                                        [id] => 6616
                                        [parentid] => 2450
                                    )

                                [1] => Array
                                    (
                                        [name] => Foundation
                                        [id] => 6617
                                        [parentid] => 2450
                                    )

                            )

                    )
          )
  )

)

如何从这组数组中生成下拉菜单,如下所示:

<select name='select_name'>
  <option value="">-- please select --</option>
  <optgroup label="Women's Clothing"></label>
    <option value="6556">Tops > Blouse</option>
    <option value="6557">Tops > Crop Tops</option>
    <option value="6570">Women's Outerwear > Sweaters</option>
    <option value="6571">Women's Outerwear > Cardigans</option>
  <optgroup label="Health & Beauty"></label>  
    <option value="6616">Face Make Up > Powder & Compacts</option>
    <option value="6617">Face Make Up > Foundation</option>
</select>

对于数组本身,所有parentid = 0都应放置在select格式的optgroup中。现在对我来说,挑战所在是如何循环孩子的名字并附加'>'符号并循环到最后一个孩子。选项值应为最后一个子ID。请帮忙。我真的不知道如何解决我的问题。

2 个答案:

答案 0 :(得分:3)

这是双向,您可以在laravel blade中生成一个下拉列表。

#第一种方式 (以静态方式访问项目)

<select name='select_name'>
 <option value="">-- please select --</option>
    @foreach($data_array as $parents){ //$data_array is your original array

      <optgroup label="{{ $parents['name'] }}">

        if(is_array($parents['children'])){

            foreach($parents['children'] as $children){

                if(is_array($children['children'])){

                    foreach($children['children'] as $items){
                      <option value="{{ $items['id'] }}">{{ $children['children']['name'] }} > {{ $items['name'] }}</option>
                    @endforeach
                 @endif
            @endforeach
        @endif

       </optgroup>
    @endforeach
</select>

#第二种方式 (动态访问项目)

  

如果项目的嵌套程度更高,则只需使用函数递归即可。

在控制器中创建下拉列表

 <?php

......
.......
class TestController extends Controller
{
    public $names = ""; //Sub category names
    public $dropdownList = ""; //options dropdown list

    public function index()  //function call using route
        {
            $dropdownlist = $this->getLastChildren($items_array,1);
            return view('index', compact('dropdownlist'));
        }
    //make dropdown list 
    public function getLastChildren($items,$parent=0){
        foreach($items as $item){
            if($parent == 1){  //set optgroup title
                $this->dropdownList .= "<optgroup label=".$item['name'].">";
                $this->names = '';
            }
            if(isset($item['children']) && is_array($item['children'])){
                if(!empty($item['name']) && $parent == 0){
                    $this->names .=$item['name'].'>'; //set subcategory names
                }
                $this->getLastChildren($item['children'],0); //call this function recursively for get last children
            }else{ 
                // Confirm it's last item
                $this->dropdownList .="<option value=".$item['id'].">".$this->names.$item['name']."</option>";
            }
            if($parent == 1){
                $this->dropdownList .= "</optgroup>";
            }

        }
        return $this->dropdownList; //return dopdown list
      }


}

在刀片文件中,只需打印此下拉列表

<select>
       <option value=''>-- please select --</option>
        {!! $dropdownlist !!} //list from controller
</select>

答案 1 :(得分:0)

基于您的评论,即子元素的深度未知,唯一的方法是使用回调函数递归。

在下面尝试此代码。

function getChildren($array, $category = NULL){  

  foreach($array as $child){

    if(isset($child['children']) && $child['children']){

      if($category != NULL){

        $newCategory = $category . ' > ' . $child['name'];

      }else {

          $newCategory = $child['name'];

        }

      getChildren($child['children'], $newCategory);

    } else {

      echo '<option value="' . $child['id'] . '">' . $category . ' > ' . $child['name'] . '</option>';

    }

  }

  unset($category);

}


echo
'<select name="select_name">
  <option value="">-- please select --</option>';

  for($i = 0; $i < count($array); $i++){

    echo
    '<optgroup label="' . $array[$i]['name'] . '"></label>';

    getChildren($array[$i]['children']);    

  }

echo
'</select>';