I had a module that required display of a set of categories organised by id and parent id. To create a tree view I created a node object and small bit of code to render a HTML drop down form element.
Of course after creating this code I realised this information was in fact surplus to requirements and would potentially confuse users.
The code took too much love to delete so here it is for next time…
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
class UKOLCatTree { public $id; public $parentid; public $title; public $children; public $level; function __construct($data) { $this->id = $data['id']; $this->parentid = $data['parent_id']; $this->title = $data['title']; $this->children = array(); $this->level = 0; } function addChild($cat) { array_push($this->children, $cat); $cat->level = $this->level + 1; } function show() { // Show self $this->showSelf(); // Show kids foreach ($this->children as $kids) { $kids->show(); } } function showSelf() { echo '<p>'; for ($i = 0; $i level; $i++) { echo " "; echo " "; if ($i == ($this->level - 1)) { echo "|_"; } else { echo ". "; } } echo $this->title . '[' . $this->id . '-' . $this->parentid . ']</p>'; } function getOptionHTML() { // Show self $r = $this->getSelfOptionHTML(); // Show kids foreach ($this->children as $kids) { $r .= $kids->getOptionHTML(); } return $r; } function getSelfOptionHTML() { $r = 'id . '">'; for ($i = 0; $i level; $i++) { $r .= " "; if ($i == ($this->level - 1)) { $r .= "|_"; } else { $r .= ". "; } } $r .= $this->title . '' . "n"; return $r; } } $db =& JFactory::getDBO(); $db->setQuery('SELECT * FROM `#__categories` WHERE section="com_docman"'); // Create root object, start object array and place root in first. $root = new UKOLCatTree(array('id' => 0, 'parent_id' => 0, 'title' => 'Top')); $cats = array($root); // Load rows into UKOLCatTree objects, store objects in array with id as key $rows = $db->loadAssocList(); foreach ($rows AS $row) { // Create object $cat = new UKOLCatTree($row); // Store in array with id as key $cats[$cat->id] = $cat; } // Go through array and add each to it's corresponding parent foreach ($cats AS $cat) { // Get parent object if (isset($cats[$cat->parentid])) { // Don't add the root to itself! if ($cat->id != 0) { $dad = $cats[$cat->parentid]; $dad->addChild($cat); } } else { echo '<p>No parent [' . $cat->parentid . '] for cat [' . $cat->id . ']</p>'; } } // Walk object heirachy to build category tree // $root->show(); $sel = '' . "n"; $sel .= $root->getOptionHTML(); $sel .= '' . "n"; echo $sel; |