提问人:softboxkid 提问时间:7/21/2018 更新时间:7/21/2018 访问量:947
使用 optgroup 从 PHP 数组生成下拉列表
Generate dropdown with optgroup from PHP array
问:
我正在使用 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。请帮忙。我真的不知道如何解决我的问题。
答:
3赞
Md Nasir Fardoush
7/21/2018
#1
您可以通过以下两种方式在 .
#First 方式(访问项目静态方式)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>
#Second 方式(动态访问项目)
如果项目更嵌套,则可以简单地使用函数递归来执行此操作。
在控制器中制作下拉列表
<?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>
评论
0赞
softboxkid
7/21/2018
阿尔罕杜利拉。TQVM的
0赞
Joseph_J
7/21/2018
这不适用于嵌套深度超过两个循环的项目。OP表示子级别不是固定的。
0赞
Md Nasir Fardoush
7/21/2018
嗨,@softboxkid我在问题发生后更新了我的答案。感谢您@Joseph_J的评论。access deeper items dynamically
0赞
Joseph_J
7/21/2018
#2
根据您关于子元素的深度未知的评论,执行此操作的唯一方法是使用回调函数递归。
试试下面的代码。
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>';
评论
0赞
Joseph_J
7/21/2018
@softboxkid 提供的第一个答案只会深入数组,因为有提供的循环。上面的函数将递归地遍历数组,并提供所有项目,无论它们有多远。
评论
Face Make Up > Foundation
Face Make Up > Foundation > Red > With sparkles