Treeview using Bootstrap Treeview, PHP and MySQL

I got huge response of treeview tutorials How to Create Dynamic Tree View Menu from readers and find request to create bootstrap treeview using php and mysql. This treeview menu tutorials help to create beautiful tree menu using bootstrap 3, php and mysql. I will create JSON data from mysql table data which are containing parent and child relation as mentioned my previous tutorials.

I have found Bootstrap treeview plugin to create tree view menu using bootstrap 3. Bootstrap treeview is very simple and elegant solution to displaying hierarchical tree structures using bootstrap.

This tutorials will use following dependencies

  • Bootstrap v3.3.4 (>= 3.0.0)
  • jQuery v2.1.3 (>= 1.9.0)
  • PHP(>= 5.2.0)
  • MySQL(>= 5.3.0)

As bootstrap treeview described in his docs, we need to create a nested JavaScript JSON object which will contains all nodes information with all options.We need to define following types JSON Object to create the hierarchical structure.

Where as:
text : this will use for display node name/label.
node[] : This array will contains all children nodes of parent node.

We can define all properties or options on nodes,color etc with each nodes as like below,

As you can see to above single json object, You can customized your node very easily using above json properties.There are number of options available to create custom option.

You can also check other tutorial of TreeView Menu,

How To create Dynamic Tree using Bootstrap Treeview,PHP and MySQL

So we are using below files to create dynamic treeview structure using bootstrap jquery tree plugin, php and mysql. Its very easy and simple,The project structure are follows,

  1. index.php : This file is use to display tree
  2. response.php : This file is use to fetch tree nodes from database and convert them into json object.

Create Database Table and Connection Using PHP and MySQL

First we will create database connection to get table nodes from mysql using php.I am assuming you have 'test', if you dot have please create new database and fill name 'test'.

Step 1: Create Table structure to stored tree menu nodes.

Step 2: Create connection string using php with mysql in (response.php) file.We have passed $servername, $username, $password and $dbname parameters to mysql connection object.

Step 3: Included all js/css files of bootstrap treeview into index.php file.

Step 4: Created tree menu container in index.php.

Step 5: We will use ajax request to get JSON data from php server side.I will use GET type Ajax request and passed json data to bootstrap treeview method.

Step 6: We will call bootstrap treeview method on div container where we want to render hierarchical structure.The container id is '#treeview_json'.

Now we will work on server side using PHP,I will create response.php file which are used in above AJAX request.We will create connection string with mySQL as stated in Step 2.

We will get all nodes information from MySQL rather than Constant JSON and set node options as we needed to bootstrap treeview JSON node, finally we will create JSON hierarchical structure as need for bootstrap treeview.

Step 7: created PHP array of all tree nodes and encode into json(response.php).

Conclusion :

I have created treeview menu using bootstrap treeview plugin.Its very easy to use and more customizable options available for tree view node.I used PHP and MySQL to get JSON tree nodes data from server side and parse json as required for bootstrap treeview JSON data.

You can download source code and Demo from below link.