Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make UL/LI tree from a normal array

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 206
    Comment on it

    We usally wants to male a ul/li tree from an array having parent id within each individual record :

    $subjecttopic=Array
    (
        [0] => Array
            (
                [id] => 52b28146f484a5a410000029
                [name] => Addtion
                [parent] => 0
                [updated] => 2013-12-23 09:37:43
            )
        [1] => Array
            (
                [id] => 52b28150f484a5a41000002a
                [name] => Subtraction
                [parent] => 0
                [updated] => 2013-12-23 09:37:49
            )
        [2] => Array
            (
    
                [id] => 52b281c2f484a59c1000002f
                [name] => 2 Digit Simple Addition
                [parent] => 52b28184f484a59c1000002b
                [updated] => 2013-12-23 09:37:26
            )
        [3] => Array
            (
    
                [id] => 52b281c9f484a59c10000030
                [name] => 3 Digit Simple Addition
                [parent] => 52b28189f484a59c1000002c
                [updated] => 2013-12-23 09:37:58
            )
        [4] => Array
            (
    
                [id] => 52b281d4f484a59c10000031
                [name] => 3 Digit Carry Addition
                [parent] => 52b28189f484a59c1000002c
                [updated] => 2013-12-23 09:38:06
            )
        [5] => Array
            (
    
                [id] => 52b281a6f484a59c1000002d
                [name] => 2 Digit Subtraction
                [parent] => 52b28150f484a5a41000002a
                [updated] => 2013-12-23 09:38:16
            )
        [6] => Array
            (
    
                [id] => 52b28210f484a5cc1000002a
                [name] => 2 Digit Borrow Subtraction
                [parent] => 52b281a6f484a59c1000002d
                [updated] => 2013-12-23 09:38:49
            )
        [7] => Array
            (
    
                [id] => 52b28218f484a5cc1000002b
                [name] => 3 Digit Borrow Subtraction
                [parent] => 52b281abf484a59c1000002e
                [updated] => 2013-12-23 09:38:54
            )
        [8] => Array
            (
    
                [id] => 52b281abf484a59c1000002e
                [name] => 3 Digit Subtraction
                [parent] => 52b28150f484a5a41000002a
                [updated] => 2013-12-23 09:38:59
            )
        [9] => Array
            (
    
                [id] => 52b2821ff484a5cc1000002c
                [name] => 3 Digit Simple Subtraction
                [parent] => 52b281abf484a59c1000002e
                [updated] => 2013-12-23 09:39:04
            )
        [10] => Array
            (
    
                [id] => 52b28189f484a59c1000002c
                [name] => 3 Digit Addition
                [parent] => 52b28146f484a5a410000029
                [updated] => 2013-12-23 09:40:24
            )
        [11] => Array
            (
    
                [id] => 52b281dbf484a59c10000032
                [name] => 2 Digit Carry Addition
                [parent] => 52b28184f484a59c1000002b
                [updated] => 2013-12-23 09:40:41
            )
        [12] => Array
            (
                [id] => 52b28184f484a59c1000002b
                [name] => 2 Digit Addition
                [parent] => 52b28146f484a5a410000029
                [updated] => 2013-12-23 09:40:56
            )
        [13] => Array
            (
                [id] => 52b28208f484a5cc10000029
                [name] => 2 Digit Simple Subtraction
                [parent] => 52b281a6f484a59c1000002d
                [updated] => 2013-12-23 09:41:01
            )
    )
    public function buildTopicTree($rows, $parent = 0) {
            $result = "
      "; foreach ($rows as $row) { if ($row['parent'] == $parent) { if (count($row['children']) > 0){ $result.= "
    • {$row['name']}"; $result.= $this->buildTopicTree($rows,$row[_id]->{'$id'}); }else{ $result.= "
    • {$row['name']}"; $result.= $this->buildTopicTree($rows,$row[_id]->{'$id'}); } $result.= "
    • "; } } $result.= "
    "; return $result; } $resultstr=$this->buildTopicTree($subjecttopic);

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: