我有一个功能如下,它以分层(缩进)方式将选项输出到下拉表单中:
<?php
function printTerms($terms, $item, $parent = 0, $deep = 0, $selected=''){
if(count($terms[$parent]) > 0){
$indent = "";
for($i = 0; $i < $deep; $i++){
$indent .= " ";
}
foreach($terms[$parent] as $key => $term){
if(count($terms[$term->term_id]) > 0){
if($deep == 0){
// echo "<optgroup label='".$term->name."'></optgroup>"; <- Makes top level parents optgroups.
echo "<option class='toplevel' value='".$term->name."' ".$selected.">".$term->name."</option>"; // <- Makes top level parents options
_________________________________________________________________________
} else {
echo "<option class='child".$deep."' value='".$term->name."' ".$selected.">".$indent.$term->name."</option>"; // <- Outputs children that are also parents
}
printTerms($terms, $term->term_id, ($deep+1));
} else {
if ($item == '1x per person' && $term->slug == 'once'){
$selected = 'selected';
}
echo "<option class='solo' value='".$term->name."' ".$selected.">".$indent.$term->name."</option>"; // <- outputs bottom level children and solos.
}
}
}
}
?>
上面的函数与将表单放置在 HTML 中的代码结合使用时(我们称之为 CASE 1):
<form>
<div id="frequency" class="form">
Frequency: <?php echo $item[2]; ?>
<?php
$terms = array();
foreach($frequencies as $key => $term){
$terms[$term->parent][$term->term_id] = $term;
}
?>
<select name="frequencies">
<option value="empty"></option>
<?php printTerms($terms, $item[2]); ?>
</select>
</div>
</form>
当与在 HTML 中放置多选表单的代码结合使用时,上述功能不起作用(案例 2):
<form>
<div id="regions" class="form">
<?php
$terms = array();
foreach($regions as $key => $term){
$terms[$term->parent][$term->term_id] = $term;
}
?>
<select name="regions">
<!-- <option value="empty"></option> -->
<?php printTerms($terms, $item[6]); ?>
</select>
</div>
</form>
$frequencies 是一个对象数组,包含:
Array ( [0] => stdClass Object ( [term_id] => 435 [name] => Twice [slug] => twice [term_group] => 0 [term_taxonomy_id] => 435 [taxonomy] => frequency [description] => Twice the fun. [parent] => 0 [count] => 89 ) [1] => stdClass Object ( [term_id] => 449 [name] => Thrice [slug] => thrice [term_group] => 0 [term_taxonomy_id] => 449 [taxonomy] => frequency [description] => Thrice rhymes with mice. [parent] => 0 [count] => 3 ) [2] => stdClass Object ( [term_id] => 458 [name] => Once [slug] => once [term_group] => 0 [term_taxonomy_id] => 460 [taxonomy] => frequency [description] => Once a day keeps doctor away. [parent] => 0 [count] => 4 ) )
$regions 是一个对象数组,包含:
Array ( [0] => stdClass Object ( [term_id] => 436 [name] => Canada [slug] => canada [term_group] => 0 [term_taxonomy_id] => 436 [taxonomy] => regions [parent] => 548 [count] => 33 ) [1] => stdClass Object ( [term_id] => 439 [name] => United States of America [slug] => united-states-of-america [term_group] => 0 [term_taxonomy_id] => 439 [taxonomy] => regions [parent] => 548 [count] => 59 ) [2] => stdClass Object ( [term_id] => 452 [name] => Quebec [slug] => quebec [term_group] => 0 [term_taxonomy_id] => 452 [taxonomy] => regions [parent] => 436 [count] => 4 ) [3] => stdClass Object ( [term_id] => 28 [name] => Ontario [slug] => ontario [term_group] => 0 [term_taxonomy_id] => 453 [taxonomy] => regions [parent] => 436 [count] => 2 ) [4] => stdClass Object ( [term_id] => 453 [name] => New Brunswick [slug] => new-brunswick [term_group] => 0 [term_taxonomy_id] => 454 [taxonomy] => regions [parent] => 436 [count] => 3 ) [5] => stdClass Object ( [term_id] => 454 [name] => Nova Scotia [slug] => nova-scotia [term_group] => 0 [term_taxonomy_id] => 455 [taxonomy] => regions [parent] => 436 [count] => 3 ) [6] => stdClass Object ( [term_id] => 455 [name] => Prince Edward Island [slug] => prince-edward-island [term_group] => 0 [term_taxonomy_id] => 456 [taxonomy] => regions [parent] => 436 [count] => 3 ) [7] => stdClass Object ( [term_id] => 456 [name] => Newfoundland [slug] => newfoundland [term_group] => 0 [term_taxonomy_id] => 457 [taxonomy] => regions [parent] => 436 [count] => 3 ) [8] => stdClass Object ( [term_id] => 464 [name] => Alberta [slug] => alberta [term_group] => 0 [term_taxonomy_id] => 466 [taxonomy] => regions [parent] => 436 [count] => 1 ) [9] => stdClass Object ( [term_id] => 465 [name] => Saskatchewan [slug] => saskatchewan [term_group] => 0 [term_taxonomy_id] => 467 [taxonomy] => regions [parent] => 436 [count] => 1 ) [10] => stdClass Object ( [term_id] => 466 [name] => Manitoba [slug] => manitoba [term_group] => 0 [term_taxonomy_id] => 468 [taxonomy] => regions [parent] => 436 [count] => 1 ) [11] => stdClass Object ( [term_id] => 467 [name] => British Columbia [slug] => british-columbia [term_group] => 0 [term_taxonomy_id] => 469 [taxonomy] => regions [parent] => 436 [count] => 1 ) [12] => stdClass Object ( [term_id] => 468 [name] => Yukon [slug] => yukon [term_group] => 0 [term_taxonomy_id] => 470 [taxonomy] => regions [parent] => 436 [count] => 1 ) [13] => stdClass Object ( [term_id] => 469 [name] => Northwest Territories [slug] => northwest-territories [term_group] => 0 [term_taxonomy_id] => 471 [taxonomy] => regions [parent] => 436 [count] => 1 ) [14] => stdClass Object ( [term_id] => 470 [name] => Nunavut [slug] => nunavut [term_group] => 0 [term_taxonomy_id] => 472 [taxonomy] => regions [parent] => 436 [count] => 1 ) [15] => stdClass Object ( [term_id] => 548 [name] => World Wide [slug] => world-wide [term_group] => 0 [term_taxonomy_id] => 551 [taxonomy] => regions [parent] => 0 [count] => 20 ) )
那么有什么不同呢?在案例 1 中,我们有一个简单的下拉菜单,调用 printTerms($terms, $item[2]) - WORKS:输出所有父母及其子女的缩进。
在案例 2 中,我们有一个简单的下拉菜单,调用 printTerms($terms, $item[6]) - 不起作用:仅输出顶级父级,忽略所有子级。
这是 $item[6] 与 $item[2] 相比所包含的内容:
$item[2] contains '1x per rotation'
$item[6] contains 'Enter only if U.S. 18+'
除了上述之外,CASE 1 和 CASE 2 都是相同的,那么我该如何解决我的问题呢?
更新:在我的函数代码中插入虚线以证明行以上的所有内容在 CASE 2 中都有效,但行以下没有任何内容。对于 CASE 1,该行上方和下方的所有内容都按预期工作。
更新 x2:与 CASE 2 一起使用时的函数似乎没有循环。但是,如果我从所有函数的代码中完全删除 $item,它就可以正常工作。关于 $item 的一些事情正在阻止我的函数循环。问题是为什么?