PHP code example of ben-nsng / nestedset-php

1. Go to this page and download the library: Download ben-nsng/nestedset-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ben-nsng / nestedset-php example snippets


# Install Composer
curl -sS https://getcomposer.org/installer | php

# Add nestedset-php as a dependency
php composer.phar 


$pdo = new PDO('mysql:host=localhost;dbname=tree', 'tree', 'tree');
$treeModel = new NestedSet($pdo);
$treeModel->changeTable('tree'); // default table is tree


// init table for first creating table
$treeModel->addRoot();

// add new node to root node, return node id
$treeModel->addNode($label);

// add new node into parent node return node id
$treeModel->addNode($label, $parent_id);

// return database statement object
$nodes = $treeModel->selectAll();
// array of nodes (stdclass)
$nodes->result();

// move existing node into parent node
$treeModel->addChild($node_id, $parent_id);

// move existing node before next node
$treeModel->addBefore($node_id, $next_id);

// move existing node after last node
$treeModel->addBefore($node_id, $last_id);

// delete existing node, including nodes inside node
$treeModel->deleteNode($node_id)