2012-12-04 3 views
0
$tree = taxonomy_get_tree($vid); 
print "<li>";     // 1 line 
foreach ($tree as $term) {  // 2 lines 
    $diffdepth=0; 
    if ($term->depth > $depth) { 
     print "<ul class='haschild'><li>"; 
     $depth = $term->depth; 
    } 

위의 코드는 원래 $term->depth > $depth에 따라 출력하고 싶습니다. (print "<li>";)이 줄. 즉조건 작성 방법은?

,

if ($term->depth > $depth) { 
    echo '<li class="parent">'; 
} 
else { 
    print "<li>"; 
} 

하지만 $term->depth는 foreach 루프 후 사용할 수 있습니다,하지만 난 한 줄을 사용하려면, 어떻게해야합니까?

+0

2 줄의 결과가 나오면 2 라인을 의미합니까? –

답변

0

인라인으로 print 대신에 원하는 출력을 변수에 할당 한 다음 로직 완료 후 브라우저로 보내십시오.

$tree = taxonomy_get_tree($vid); 
$parent = ""; // 1 line 
$child = ""; 
foreach ($tree as $term) { // 2 line 
    $diffdepth=0; 
    if ($term->depth > $depth) { 
     $parent = "<li class='parent'>"; 
     $child .= "<ul class='haschild'><li>"; 
     $depth = $term->depth; 
    } else { 
     $parent = "<li>"; 
    } 
} 
echo $parent . $child; 

는 적용 가능한 모든 </li>들과 이것 저것을 추가하여 마무리해야합니다 있습니다, 그러나 이것은 당신이 시작하는 것이다. 당신이 당신의 조건에 따라 많은 차이를 작성해야하는 경우

$tree = taxonomy_get_tree($vid); 
$counter = 0; 
foreach ($tree as $term) 
{ 
    ... 
    if ($term->depth > $depth) 
    { 
     if ($counter == 0) { echo '<li class="parent">'; } 
     else { echo '<li>'; } 
     print "<ul class='haschild'><li>"; 
     $depth = $term->depth; 
    } 
    ... 
    $counter++; 
} 


, 당신은에 위 그쪽을 설정할 수 있습니다 :

0

카운터를 사용하여 당신이 뭔가를 구축하려는 경우

$tree = taxonomy_get_tree($vid); 
$counter = 0; 
foreach ($tree as $term) 
{ 
    ... 
    if ($term->depth > $depth) 
    { 
     if ($counter == 0) { echo '<li class="parent">'; } 
     print "<ul class='haschild'><li>"; 
     $depth = $term->depth; 
    } 
    else 
    { 
     if ($counter == 0) { echo '<li>'; } 
     ... 
    } 
    ... 
    $counter++; 
} 
0

상위 하위 계층 구조를 확인해야합니다. by click here