-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample-1.php
108 lines (94 loc) · 3.25 KB
/
example-1.php
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
<?php
include('tree.php');
/**
* JTreeRecursiveIterator
*
* To use a recursive iterator you have to extend of the RecursiveIteratorIterator
* As an example I have built an unordered list
* For detailed information on please see RecursiveIteratorIterator
* http://us.php.net/manual/en/class.recursiveiteratoriterator.php
*
* @package JTree
* @author Jayesh Wadhwani
* @copyright Jayesh Wadhwani
* @license GNU GENERAL PUBLIC LICENSE 3.0
* @version 1.0 2011
*/
class JTreeRecursiveIterator extends RecursiveIteratorIterator {
/**
* @var _jTree the JTree object
*/
private $_jTree;
/**
* @var _str string with ul/li string
*/
private $_str;
/**
* JTreeRecursiveIterator::__construct()
*
* @param mixed $jt - the tree object
* @param mixed $iterator - the tree iterator
* @param mixed $mode
* @param integer $flags
* @return
*/
public function __construct(JTree $jt, $iterator, $mode = RecursiveIteratorIterator::LEAVES_ONLY, $flags = 0) {
parent::__construct($iterator, $mode, $flags);
$this->_jTree = $jt;
$this->_str = "<ul>\n";
}
/**
* JTreeRecursiveIterator::endChildren()
* Called when end recursing one level.(See manual)
* @return void
*/
public function endChildren() {
parent::endChildren();
$this->_str .= "</ul></li>\n";
}
/**
* JTreeRecursiveIterator::callHasChildren()
* Called for each element to test whether it has children. (See Manual)
*
* @return mixed
*/
public function callHasChildren() {
$ret = parent::callHasChildren();
$value = $this->current()->getValue();
if($ret === true) {
$this->_str .= "<li>{$value}<ul>\n";
} else {
$this->_str .= "<li>{$value}</li>\n";
}
return $ret;
}
/**
* JTreeRecursiveIterator::__destruct()
* On destruction end the list and display.
* @return void
*/
public function __destruct() {
$this->_str .= "</ul>\n";
echo $this->_str;
}
}
$categories = array();
$categories[] = array('id' => 1, 'weather_condition' => 'weather', 'parent_id' => 9999);
$categories[] = array('id' => 2, 'weather_condition' => 'Earthquakes', 'parent_id' => 1);
$categories[] = array('id' => 3, 'weather_condition' => 'Major', 'parent_id' => 2);
$categories[] = array('id' => 4, 'weather_condition' => 'Minor', 'parent_id' => 2);
$categories[] = array('id' => 5, 'weather_condition' => 'Fires', 'parent_id' => 9);
$categories[] = array('id' => 6, 'weather_condition' => 'Rain', 'parent_id' => 1);
$categories[] = array('id' => 7, 'weather_condition' => 'Flooding', 'parent_id' => 6);
$categories[] = array('id' => 8, 'weather_condition' => 'Washout', 'parent_id' => 6);
$categories[] = array('id' => 9, 'weather_condition' => 'Hurricanes', 'parent_id' => 1);
//create a new tree object
$jt = new JTree();
//iterate building the tree
foreach($categories as $category) {
$uid = $jt->createNode($category['weather_condition'],$category['id'], $category['parent_id']);
}
//update: removed third variable. Use defaults
$it = new JTreeRecursiveIterator($jt, new JTreeIterator($jt->getTree()));
//iterate to create the ul list
foreach($it as $k => $v) {}