2013-03-02 3 views
0

내가 한 내장 배열이 방법인쇄 다차원 배열과 스타일이

 
Array 
(
    [6] => Array 
     (
      [id] => 6 
      [parent_id] => 
      [text] => Top level comment 
      [level] => 1 
     ) 

    [1] => Array 
     (
      [id] => 1 
      [parent_id] => 
      [text] => Top level comment 
      [level] => 1 
      [children] => Array 
       (
        [2] => Array 
         (
          [id] => 2 
          [parent_id] => 1 
          [text] => Response to #1 
         ) 

        [3] => Array 
         (
          [id] => 3 
          [parent_id] => 1 
          [text] => Response to #1 
          [children] => Array 
           (
            [4] => Array 
             (
              [id] => 4 
              [parent_id] => 3 
              [text] => Response to #3 
             ) 

           ) 

         ) 

        [10] => Array 
         (
          [id] => 10 
          [parent_id] => 1 
          [text] => Response to #2 
          [children] => Array 
           (
            [11] => Array 
             (
              [id] => 11 
              [parent_id] => 10 
              [text] => Response to #10 
              [children] => Array 
               (
                [13] => Array 
                 (
                  [id] => 13 
                  [parent_id] => 11 
                  [text] => Response to #11 
                  [children] => Array 
                   (
                    [14] => Array 
                     (
                      [id] => 14 
                      [parent_id] => 13 
                      [text] => Response to #13 
                     ) 

                   ) 

                 ) 

               ) 

             ) 

            [12] => Array 
             (
              [id] => 12 
              [parent_id] => 10 
              [text] => Response to #10 
             ) 

           ) 

         ) 

       ) 

     ) 

    [5] => Array 
     (
      [id] => 5 
      [parent_id] => 
      [text] => Top level comment 
      [level] => 1 
     ) 

    [9] => Array 
     (
      [id] => 9 
      [parent_id] => 
      [text] => Top level comment 
      [level] => 1 
     ) 

) 

JSON

{"6":{"id":6,"parent_id":null,"text":"Top level comment","level":1},"1":{"id":1,"parent_id":null,"text":"Top level comment","level":1,"children":{"2":{"id":2,"parent_id":1,"text":"Response to #1"},"3":{"id":3,"parent_id":1,"text":"Response to #1","children":{"4":{"id":4,"parent_id":3,"text":"Response to #3"}}},"10":{"id":10,"parent_id":1,"text":"Response to #2","children":{"11":{"id":11,"parent_id":10,"text":"Response to #10","children":{"13":{"id":13,"parent_id":11,"text":"Response to #11","children":{"14":{"id":14,"parent_id":13,"text":"Response to #13"}}}}},"12":{"id":12,"parent_id":10,"text":"Response to #10"}}}}},"5":{"id":5,"parent_id":null,"text":"Top level comment","level":1},"9":{"id":9,"parent_id":null,"text":"Top level comment","level":1}}

가장 쉬운 그것을 반향 방법과 스타일은 무엇입니까? reddit처럼 말하십시오. 보기 파일에서 스타일을 지정하고 싶습니다.

답변

0

http://ellislab.com/codeigniter/user-guide/helpers/html_helper.html#ol_and_ul으로 시도해 볼 수는 있지만 배열을 반복하고 HTML을 생성하는 사용자 지정 함수로 이동하려고합니다.

편집 : 당신이 그 배열을 통해 루프를 거라고 방법

function draw_comments($comments, $level = 0) 
{ 
    echo '<ul class="level' . $level . '">'; 
    foreach($comments as $comment) 
    { 
     draw_comment($comment); 

     if(!empty($comment['children'])) 
      draw_comments($comment['children'], $level+1); 
    } 
    echo '</ul>'; 
} 

function draw_comment($comment) 
{ 
    echo '<li>'; 
    echo $comment['text']; 
    echo '</li>'; 
} 

draw_comments($comments_array); 
+0

당신이 공유하고 Whould? 나는 아이들의 숫자 = x 때문에 무엇을 해야할지 모르겠다. – Martin