Jump to content

Two-dimensional Array - nesting posts problem


stopblackholes

Recommended Posts

I've been messing around with this for awhile. What i'm trying to do is get the array to output in  a function using div tags instead of formatting it in a list.  like so

 

<ul><li> no good </li><ul>

 

<div class="topic"> ok</div>

<div class="reply"> ok</div>

but i have to order then check the parent id then if it has parent output reply. Also probably use depth of reply by checking id against parent id so i can adjust the margins.

 

 

right now im just using a messy recursive function.

any ideas?

 

 

<?php
$tree = array( array( id => "1", 
                      name => "1st topic",
                      parentid => "0" 
                    ),
               array( id => "2", 
                      name => "reply",
                      parentid => "1"
                    ),
               array( id => "3", 
                      name => "reply",
                      parentid => "1" 
                    ),
		   array( id => "4", 
                      name => "2nd topic",
                      parentid => "0" 
                    ),
		   array( id => "5", 
                      name => "reply 2nd",
                      parentid => "4" 
                    ),		
		   array( id => "6", 
                      name => "reply",
                      parentid => "1" 
                    )	
             );

function itemsFormat($parent, $tree) {
       foreach ($tree as $item) {
        if($parent == $item['parentid']) {
          $output .= '<ul>';
          $output .= '<li>' . $item['name'] . '</li>';
          $output .= itemsFormat($item['id'], $tree);
          $output .= '</ul>';
        }
      }

      return $output;
    }
echo itemsFormat(0, $tree);



///output raw array for testing
print '<pre>'; var_dump($tree); print '</pre>';

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.