2013-01-12 88 views
0

失敗我有一個函數,如下所示,其輸出以分層(縮進)方式選擇成下拉形式:PHP函數在第二個實例

<?php 
function printTerms($terms, $item, $parent = 0, $deep = 0, $selected=''){ 
    if(count($terms[$parent]) > 0){ 
     $indent = ""; 
     for($i = 0; $i < $deep; $i++){ 
      $indent .= "&nbsp;&nbsp;&nbsp;"; 
     } 

     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):

:當與該代碼置於HTML多選形式(情況2)耦合

<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> 

上述功能不起作用

$頻率是一個對象數組,包含:

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)) 

$區是一個對象數組,包含:

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])的簡單下拉菜單 - 不工作:只輸出頂級父母,忽略所有孩子。

這是$項目[6]相比含有$項目[2]:

$item[2] contains '1x per rotation' 
$item[6] contains 'Enter only if U.S. 18+' 

上述以外,無論CASE 1和CASE 2是相同的,所以我怎麼解決我的問題?

UPDATE:在我的函數代碼中插入虛線以表明線上的所有東西都在CASE 2中工作,但線下沒有任何東西。對於案例1,線條上下的所有內容都按預期工作。

UPDATE x2:與CASE 2一起使用時的函數看起來並沒有循環。但是,如果我完全從所有函數的代碼中刪除$ item,它就可以很好地工作。關於$ item的事情正在阻止我的循環功能。問題是爲什麼?

+2

什麼'$ frequencies'和'$ regions'包含哪些內容? –

+0

查看更新的問題。 – Sweepster

回答

0

的修復很簡單:

<?php printTerms($terms, 0, 0, 0, $item[2]); ?> 
相關問題