-
Notifications
You must be signed in to change notification settings - Fork 2
Add a new element to the tree
Benjamin Kaiser edited this page Apr 15, 2021
·
6 revisions
You can use the "add()" method of the class to add a new element to the tree:
mixed add ( integer $parent , string $title , [ integer $position = false] )
Type | Name | Description |
---|---|---|
integer | $parent | The ID of the parent element. Use "0" to add a topmost element. |
string | $title | The title of the element. |
integer | $position | (Optional) The position the element will have among the parent element's children elements. When parent element is given as "0", this refers to the position the element will have among the topmost elements. The values are 0-based, meaning that if you want the element to be inserted as the first child of the target element, you have to use "0", if you want it to be second, use "1", and so on. If not given (or given as boolean FALSE), the element will be inserted as the last of the parent element's children elements. |
Return: Returns the ID of the newly inserted element or FALSE on error.
// add a new topmost element
$element = $f3t->add(0, 'Main');
// add a child element
$f3t->add($element, 'Child 1');
// add another child element
$f3t->add($element, 'Child 2');
// insert a third child element
// notice the "1" as the last argument, instructing the script to insert the child element
// as the second child element, after "Child 1"
// remember that the trees are 0-based, meaning that the first element in a tree has the index 0!
$f3t->add($element, 'Child 3', 1);
// and finally, insert a fourth child element
// notice the "0" as the last argument, instructing the script to insert the child element
// as the very first child element of the parent element
// remember that the trees are 0-based, meaning that the first element in a tree has the index 0!
$f3t->add($element, 'Child 4', 0);