次のような機能があり、オプションをドロップダウン形式で階層 (インデント) 形式で出力します。
<?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 ) )
では、何が違うのでしょうか?CASE 1 には、printTerms($terms, $item[2]) を呼び出す単純なドロップダウン メニューがあります。動作: すべての親とその子をインデント付きで出力します。
ケース 2 には、printTerms($terms, $item[6]) を呼び出す単純なドロップダウン メニューがあります。動作しません: トップ レベルの親のみを出力し、すべての子を無視します。
これは、$item[2] と比較して $item[6] に含まれるものです。
$item[2] contains '1x per rotation'
$item[6] contains 'Enter only if U.S. 18+'
上記以外は、CASE 1 も CASE 2 も同じですが、どうすれば問題を解決できますか?
更新: 関数コードに点線を挿入して、点線より上のすべてが CASE 2 で機能することを示しますが、線より下は機能しません。ケース 1 の場合、線の上と下のすべてが期待どおりに機能します。
UPDATE x2: CASE 2 で使用した関数はループしていないようです。ただし、すべての関数のコードから $item を完全に削除すると、完全に正常に動作します。$item に関する何かが、関数のループを妨げています。問題はなぜですか?