【发布时间】:2017-09-27 13:58:52
【问题描述】:
我正在使用 Symfony,我想做一个 3 Levels optgroup 我想要的是:
第 1 类(已禁用)
--ChildCategory 1.1
--ChildCategory 1.2(已禁用)
----ChildChildCategory 1.2.1
第 2 类
但我得到的只是:
第 1 类(已禁用)
--ChildCategory 1.1
ChildCategory 1.2(已禁用)
--ChildChildCategory 1.2.1
第 2 类
我的 HTML 代码:
<select id="event_category" name="event[category]" class="form-control">
<optgroup label="Category 1">
<option value="8">Category 1.1</option>
</optgroup>
<optgroup label="Category 1.2">
<option value="7">Category 1.2.1</option>
</optgroup>
<option value="3">Category 2</option>
</select>
我正在使用此代码创建一个数组,以将其添加到我的 formType 中:
$arrayCategories = array();
foreach ($categories as $category)
{
if (count($category->getChildren()) > 0)
{
$arrayCategories[$category->getTitle()] = array();
foreach ($category->getChildren() as $child)
{
if (count($child->getChildren()) > 0)
{
$arrayCategories[$category->getTitle()][$child->getTitle()] = array();
foreach ($child->getChildren() as $child2)
{
$arrayCategories[$category->getTitle()][$child->getTitle()][$child2->getTitle().' - '. $child2->getLangLabel()] = $child2->getId();
}
}
else
{
$arrayCategories[$category->getTitle()][$child->getTitle().' - '. $child->getLangLabel()] = $child->getId();
}
}
}
else
{
$arrayCategories[$category->getTitle().' - '. $category->getLangLabel()] = $category->getId();
}
}
感谢您的帮助!
【问题讨论】:
标签: php forms symfony optgroup