php-array-tree

Gesponsorde koppelingen

PHP script bestanden

  1. php-array-tree

« Lees de omschrijving en reacties

arrayTree.php

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php

class Tree {

  /**
  * @desc array with elements
  * @type array
  * @access private
  **/

  var $aElements;

  /**
  * @desc backbone array
  * @type array
  * @access private
  **/

  var $result;
  
  /**
   * Register the levels, to calculate the max level for example
   *
   * @var array
   */

  
  var $levels;  
  
  /**
   * The elements ordered by level
   *
   * @type array
   */

  
  var $lElements;
  
  
    /**
    * @desc constructor method
    * @param
    * @access public
    * @return void
    **/

    function Tree(){
        $this->aElements  = array();
        $this->lElements  = array();
        $this->result     = array();
        $this->levels       = array();
    }


    /**
     * Add an element to the array
     *
     * @param numeric $iId (The id of the element)
     * @param array $properties    (All kind of extra properties)
     * @param numeric $iParentId (The parent id)
    **/



    function addNode($iId, $properties, $iParentId = 0){
        $this->aElements[$iId] = array('properties' => $properties, 'parent' => $iParentId);
    }


    /**
     * Gets the parens ids
     *
     * @param string/numeric $iId
    **/

    
    function getParents($iId){
        $dad = $this->aElements[$iId]['parent'];
        $parent[] = $dad;
        while($dad){
            if($dad = $this->getParent($dad)){
                $parent[] = $dad;
            }            
        }

        $this->aElements[$iId]['parents'] = $parent;
        return $parent;
    }


    /**
     * Get the parent ID
     *
     * @param string/numeric $iId
     * @return The parent ID or false
    **/

    
    function getParent($iId){
        if(!empty($this->aElements[$iId]['parent'])){
            return $this->aElements[$iId]['parent'];
        }
else{
            return false;
        }
    }

    
    /**
     * Counts the level depth
     *
     * @param string/numeric $iId
     * @return numeric
    **/

    
    function level($iId){
        $level = count($this->getParents($iId));
        $this->aElements[$iId]['level'] = $level;
        $this->levels[$iId] = $level;
        if(!$this->getParent($iId)){
            $this->lElements[1][$iId] = $this->aElements[$iId];
        }
else{
            $this->lElements[$level+1][$iId] = $this->aElements[$iId];
        }

        return $level;
    }

    
    /**
     * Returns the max level.
     *
     * @return number with the max level depth
    **/

    
    function maxLevel(){
        $max = 0;
        foreach($this->levels as $level){
            if($level > $max){
                $max = $level;
            }
        }

        return ($max+1);
    }

    
    /**
     * Generates the tree Array
     *
     * @return array (within the tree array)
     */

    
    function getArray() {

        foreach($this->aElements as $iId => $element){
            $this->level($iId);
        }

        
        foreach($this->lElements[1] as $id => $element){
            $this->result[$id] = '';
        }

        
        for($i=$this->maxLevel();$i>=1;$i--){
            if(isset($this->lElements[$i]) and is_array($this->lElements)){
                foreach($this->lElements[$i] as $id => $element){
                    $parent = $this->getParent($id);
                    if($parent != 0){
                        // Als de dochter een parent heeft
                        $this->result[$id]['properties'] = $this->aElements[$id]['properties'];
                        if(!isset($this->result[$id])){                    
                            // Als de pagina nog niet bestond
                            $this->result[$parent]['childs'][$id]['properties'] = $element['properties'];
                        }
else{
                            // De pagina bestond dus wel al
                            $this->result[$parent]['childs'][$id] = $this->result[$id];
                        }

                        // Overige verwijderen
                        unset($this->result[$id]);
                    }
else{
                        $this->result[$id]['properties'] = $this->aElements[$id]['properties'];
                    }
                }
            }
        }

        return $this->result;
    }
    
    function
getLElements(){
        return $this->lElements;
    }
}


?>



example.php

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php

include('arrayTree.php');

$tree = new Tree();
$sql = "SELECT id,titel,parent,menuid FROM paginas ORDER BY menuid";
$res = mysql_query($sql);
while($page = mysql_fetch_assoc($res)){
    $tree->addNode($page['id'],$page,$page['parent']);
}

$paginas = $tree->getArray();

echo '<pre>';
print_r($paginas);
echo '</pre>';

?>


Ik heb ook een voorbeeld class gemaakt waarmee je deze array kunt uitlezen

readTreeArray.php
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
    class printArrayTree {
        
        var
$buffer;
        var
$array;        
        
        function
printArrayTree(){
            $this->array = array();
            $this->buffer = '';
        }        
        
        function
make_row($array,$level=0){
            foreach($array as $element){            
                $this->buffer .= '<tr><td>'.$element['properties']['id'].'</td>
                        <td style="padding-left: '
.(15*$level).'px;">'.($level != 0 ? '&rsaquo; ' : '').' '.$element['properties']['titel'].'</td>
                    </tr>'
;

                if(isset($element['childs']) and is_array($element['childs'])){
                    $this->make_row($element['childs'],($level+1));                
                }
            }        
        }
                
        function
print_table(){
            return '<table width="50%" cellspacing="0">
                <tr>
                <th>ID</th>
                <th>Pagina naam</th>
            </tr>'
.$this->buffer.'</table>';
        }
        
    }

?>



En deze gebruik je uiteindelijk dan zo:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php

include('arrayTree.php');

$tree = new Tree();
$sql = "SELECT id,titel,parent,menuid FROM paginas ORDER BY menuid";
$res = mysql_query($sql);
while($page = mysql_fetch_assoc($res)){
    $tree->addNode($page['id'],$page,$page['parent']);
}

$paginas = $tree->getArray();

echo '<pre>';
print_r($paginas);
echo '</pre>';

include('readTreeArray.php');

if(count($paginas) >= 1){        
    $table = new printArrayTree;
    $table->make_row($paginas);
    echo $table->print_table();        
}



?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.