创建PHP数组的下拉列表中选择现场嵌套列表 [英] Create nested list from PHP array for dropdown select field
问题描述
我有一个数组我想变成一个嵌套&LT挣扎;选择>
我需要:
<选择>
<期权价值=1>&TOP1 LT; /选项>
<期权价值=2>最上方的两个< /选项>
<期权价值=9>&Top3的LT; /选项>
<期权价值=7> - - Top3.1< /选项>
<期权价值=5> - - Top3.2< /选项>
<期权价值=12> - - - - - Top3.2.1 LT; /选项>
<期权价值=6>&页首4. LT; /选项>
<期权价值=4>&TOP5 LT; /选项>
<期权价值=8> - - Top5.1< /选项>
<期权价值=3> - - Top5.2< /选项>
我不能工作OPTGROUP,因为一切都是可选的。据我所知,你不能选择OPTGROUP标签。
我的数组是这样的:
[44] =>排列
(
[ID] => 1
[名] =>测试
[塞] =>测试
[家长] => 0
)[45] =>排列
(
[ID] => 2
[名] =>测试子
[塞] =>测试分
[家长] => 1
)[46] =>排列
(
[ID] => 3
[名] =>测试分分
[塞] =>测试分分
[家长] => 2
)
我感觉就像我已经试过许多variantions的,但我不能建立我的形式选择权。
这是我最后一次尝试:
函数toDropdown($ ARR)
{
的foreach($改编为$行){
$猫[$行['身份证'] = $行[名称];
如果($行[父]!= 0){
$猫[$行['身份证'] =' - '。 $行[名称];
}
}
返回$猫;
}
但是这样一来,它是由该ID有序和嵌套失去了意义。
我会尽力去,但如果有人能帮助我AP preciate任何帮助!
编辑:PHP数据
我的函数从数据库获取所有类别:
函数get_categories($父='全部')
{
$这个 - > DB-GT&;选择('categories.id,categories.name,categories.slug,categories.parent');
$这个 - > DB->从('类'); 如果($查询= $这个 - > DB-GT&;得到())
{
返回$查询 - > result_array();
} 返回FALSE;
}
我的view.php,在这里我输出的所有数据:
$查询= $这个 - > datei_model-> get_categories('所有');的foreach($查询作为$行)
{
$家长[] = $行;
}$树= buildTree($父母);打印(<选择> \\ n);
printTree($树);
打印(< /选择>);
试试这个;
函数buildTree(数组$数据,$父= 0){
$树=阵列();
的foreach(如$ D $的数据){
如果($ D ['父'] == $父){
$儿童= buildTree($数据,$ D ['身份证']);
//设置一个微不足道的关键
如果(!空($子女)){
$ D ['_儿童'] = $儿童;
}
$树[] = $ D组;
}
}
返回$树;
}
$行=阵列(
阵列('ID'=大于1,'名'=>'测试1','父'= 0),
阵列('ID'=大于2,'名'=>'测试1.1','父'=> 1)
阵列('ID'= GT; 3,'名'=>'测试1.2','父'=> 1)
阵列('ID'=> 4,'名'= GT;'测试1.2.1','父'=> 3)
阵列('ID'=> 5,'名'= GT;'测试1.2.2','父'=> 3)
阵列('ID'=> 6,'名'=>'测试1.2.2.1','父'=大于5)
阵列('ID'=大于7,'名'=>'测试2','父'= 0),
阵列('ID'=→8,'名'=>'测试2.1','父'=大于7)
);$树= buildTree($行);
//的print_r($树);功能printTree($树,$ R = 0,$ P = NULL){
的foreach($树$ I => $ T){
$破折号=($ T ['父'] == 0)? '':str_repeat(' - ',$ R)。 ';
的printf(\\ T<期权价值=%d个'>%s%S< /选项> \\ n,$ T ['身份证'],$破折号,$ T ['名']);
如果($ T ['父'] == $ P){
//复位$ R
$ R = 0;
}
如果(使用isset($ T ['_儿童'])){
printTree($ T ['_儿童'],++ $ R,$ T ['父']);
}
}
}
打印(<选择> \\ n);
printTree($树);
打印(< /选择>);
输出;
<选择>
<期权价值='1'>试验1 LT; /选项>
<期权价值='2'> - 测试1.1< /选项>
<期权价值='3'> - 测试1.2 LT; /选项>
<期权价值=4> - 测试1.2.1< /选项>
<期权价值=5> - 测试1.2.2< /选项>
<期权价值='6'> ---测试1.2.2.1< /选项>
<期权价值=7>测试2版; /选项>
<期权价值=8> - 测试2.1< /选项>
< /选择>
和你的情况;
<选择>
<期权价值='1'>巴登 - 符腾堡州< /选项>
<期权价值='2'> - DMP-Verträge< /选项>
<期权价值=50> - S prechstundenbedarf< /选项>
<期权价值=52> - Richtgrößen< /选项>
<期权价值='53'> - Prüfungen< /选项>
<期权价值='54'> - DMP-Verträge< /选项>
<期权价值='55'> - SONSTIGEVerträge< /选项>
<期权价值='3'>&柏林LT; /选项>
<期权价值='62'> - DMP-Verträge< /选项>
<期权价值='63'> - Prüfungen< /选项>
<期权价值='64'> - Richtgrößen< /选项>
<期权价值='65'> - SONSTIGEVerträge< /选项>
<期权价值='66'> - S prechstundenbedarf< /选项>
<期权价值=4>&勃兰登堡LT; /选项>
<期权价值='67'> - DMP-Verträge< /选项>
<期权价值='68'> - Prüfungen< /选项>
<期权价值='69'> - Richtgrößen< /选项>
<期权价值=70> - SONSTIGEVerträge< /选项>
<期权价值='71'> - S prechstundenbedarf< /选项>
<期权价值=5>不来梅< /选项>
<期权价值='72'> - DMP-Verträge< /选项>
<期权价值='73'> - Prüfungen< /选项>
<期权价值='74'> - Richtgrößen< /选项>
<期权价值=75> - SONSTIGEVerträge< /选项>
<期权价值='76'> - S prechstundenbedarf< /选项>
<期权价值=7>&黑森州LT; /选项>
<期权价值='6'>汉堡< /选项>
<期权价值=8>梅克伦堡 - 前波莫瑞州< /选项>
<期权价值='9'>下萨克森州< /选项>
<期权价值='10'>北莱茵< /选项>
<期权价值='11'>莱茵兰 - 普法尔茨< /选项>
<期权价值='12'>萨尔< /选项>
<期权价值=13>&萨克森LT; /选项>
<期权价值='14'>萨克森 - 安哈特< /选项>
<期权价值='15'>石勒苏益格 - 荷尔斯泰因< /选项>
<期权价值=16>与图林根州LT; /选项>
<期权价值=17>威斯特法伦 - 利珀< /选项>
<期权价值='51'>Richtgrössen< /选项>
<期权价值='56'>&拜仁LT; /选项>
<期权价值='57'> - DMP-Verträge< /选项>
<期权价值='58'> - Prüfungen< /选项>
<期权价值='59'> - Richtgrößen< /选项>
<期权价值=60> - SONSTIGEVerträge< /选项>
<期权价值='61'> - S prechstundenbedarf< /选项>
< /选择>
I am struggling with an array I want to turn into a nested < select >
I need:
<select>
<option value="1">Top1</option>
<option value="2">Top2</option>
<option value="9">Top3</option>
<option value="7"> - - Top3.1</option>
<option value="5"> - - Top3.2</option>
<option value="12">- - - - Top3.2.1</option>
<option value="6">Top4</option>
<option value="4">Top5</option>
<option value="8"> - - Top5.1</option>
<option value="3"> - - Top5.2</option>
I can't work with optgroup, because everything is selectable. As far as I know, you can't select optgroup labels.
My array looks like this:
[44] => Array
(
[id] => 1
[name] => Test
[slug] => test
[parent] => 0
)
[45] => Array
(
[id] => 2
[name] => Test-Sub
[slug] => test-sub
[parent] => 1
)
[46] => Array
(
[id] => 3
[name] => Test-Sub-Sub
[slug] => test-sub-sub
[parent] => 2
)
I am feeling like I have tried dozens of variantions, but I can't build my form select right.
That was my last try:
function toDropdown($arr)
{
foreach ($arr as $row) {
$cat[$row['id']] = $row['name'];
if ($row['parent'] != 0) {
$cat[$row['id']] = '--' . $row['name'];
}
}
return $cat;
}
But this way, it is ordered by the ID and the nesting loses its meaning.
I'll try to go on, but if someone can help I appreciate any help!
EDIT: PHP Data
My function to get all categories from the DB:
function get_categories($parent = 'all')
{
$this->db->select('categories.id, categories.name, categories.slug, categories.parent');
$this->db->from('categories');
if ($query = $this->db->get())
{
return $query->result_array();
}
return FALSE;
}
My view.php, where I output all data:
$query = $this->datei_model->get_categories('all');
foreach ($query as $row)
{
$parents[] = $row;
}
$tree = buildTree($parents);
print("<select>\n");
printTree($tree);
print("</select>");
Try this;
function buildTree(Array $data, $parent = 0) {
$tree = array();
foreach ($data as $d) {
if ($d['parent'] == $parent) {
$children = buildTree($data, $d['id']);
// set a trivial key
if (!empty($children)) {
$d['_children'] = $children;
}
$tree[] = $d;
}
}
return $tree;
}
$rows = array(
array ('id' => 1, 'name' => 'Test 1', 'parent' => 0),
array ('id' => 2, 'name' => 'Test 1.1', 'parent' => 1),
array ('id' => 3, 'name' => 'Test 1.2', 'parent' => 1),
array ('id' => 4, 'name' => 'Test 1.2.1', 'parent' => 3),
array ('id' => 5, 'name' => 'Test 1.2.2', 'parent' => 3),
array ('id' => 6, 'name' => 'Test 1.2.2.1', 'parent' => 5),
array ('id' => 7, 'name' => 'Test 2', 'parent' => 0),
array ('id' => 8, 'name' => 'Test 2.1', 'parent' => 7),
);
$tree = buildTree($rows);
// print_r($tree);
function printTree($tree, $r = 0, $p = null) {
foreach ($tree as $i => $t) {
$dash = ($t['parent'] == 0) ? '' : str_repeat('-', $r) .' ';
printf("\t<option value='%d'>%s%s</option>\n", $t['id'], $dash, $t['name']);
if ($t['parent'] == $p) {
// reset $r
$r = 0;
}
if (isset($t['_children'])) {
printTree($t['_children'], ++$r, $t['parent']);
}
}
}
print("<select>\n");
printTree($tree);
print("</select>");
Output;
<select>
<option value='1'>Test 1</option>
<option value='2'>- Test 1.1</option>
<option value='3'>- Test 1.2</option>
<option value='4'>-- Test 1.2.1</option>
<option value='5'>-- Test 1.2.2</option>
<option value='6'>--- Test 1.2.2.1</option>
<option value='7'>Test 2</option>
<option value='8'>- Test 2.1</option>
</select>
And in your case;
<select>
<option value='1'>Baden-Württemberg</option>
<option value='2'>- DMP-Verträge</option>
<option value='50'>- Sprechstundenbedarf</option>
<option value='52'>- Richtgrößen</option>
<option value='53'>- Prüfungen</option>
<option value='54'>- DMP-Verträge</option>
<option value='55'>- Sonstige Verträge</option>
<option value='3'>Berlin</option>
<option value='62'>- DMP-Verträge</option>
<option value='63'>- Prüfungen</option>
<option value='64'>- Richtgrößen</option>
<option value='65'>- Sonstige Verträge</option>
<option value='66'>- Sprechstundenbedarf</option>
<option value='4'>Brandenburg</option>
<option value='67'>- DMP-Verträge</option>
<option value='68'>- Prüfungen</option>
<option value='69'>- Richtgrößen</option>
<option value='70'>- Sonstige Verträge</option>
<option value='71'>- Sprechstundenbedarf</option>
<option value='5'>Bremen</option>
<option value='72'>- DMP-Verträge</option>
<option value='73'>- Prüfungen</option>
<option value='74'>- Richtgrößen</option>
<option value='75'>- Sonstige Verträge</option>
<option value='76'>- Sprechstundenbedarf</option>
<option value='7'>Hessen</option>
<option value='6'>Hamburg</option>
<option value='8'>Mecklenburg-Vorpommern</option>
<option value='9'>Niedersachsen</option>
<option value='10'>Nordrhein</option>
<option value='11'>Rheinland-Pfalz</option>
<option value='12'>Saarland</option>
<option value='13'>Sachsen</option>
<option value='14'>Sachsen-Anhalt</option>
<option value='15'>Schleswig-Holstein</option>
<option value='16'>Thüringen</option>
<option value='17'>Westfalen-Lippe</option>
<option value='51'>Richtgrössen</option>
<option value='56'>Bayern</option>
<option value='57'>- DMP-Verträge</option>
<option value='58'>- Prüfungen</option>
<option value='59'>- Richtgrößen</option>
<option value='60'>- Sonstige Verträge</option>
<option value='61'>- Sprechstundenbedarf</option>
</select>
这篇关于创建PHP数组的下拉列表中选择现场嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!