我有一个如下的函数,它以分层(缩进)的方式将选项输出到下拉表单:
<?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中放置一个多选择表单(CASE 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,行以上和行以下的所有内容都按预期工作。
UPDATE x2:当与CASE 2一起使用时,函数看起来没有循环。然而,如果我从所有函数的代码中完全删除$item,它工作得很好。关于$item的某些东西正在阻止我的函数循环。问题是为什么?
修复方法很简单:
<?php printTerms($terms, 0, 0, 0, $item[2]); ?>