Bootstrap Treeview with PHP and MySQL

Posted on

In our previous tutorial you have learned How To Create Dynamic Tree View Menu in PHP, Today in this tutorial you will learn how to create Bootstrap treeview structure with PHP and MySQL.

With Bootstrap, you can easily create treeview menus using Bootstarp treeview plugin that needs JSON data to be passed to create hierarchical tree structure.

In this tutorial we have created JSON data from MySQL using PHP and passed to Bootstrap treeview plugin to create tree structure.

So let’s start the coding
Step 1: First we will create Table structure to stored tree menu nodes.
`name` varchar(200) NOT NULL,
`text` varchar(200) NOT NULL,
`link` varchar(200) NOT NULL,
`parent_id` varchar(11) NOT NULL,

Step 2: You need to inlcude Bootstrap libaray files and bootstrap treeview plugin files. In this tutorial, we have downloaded plugin and kept plugin JS/CSS files in dist directory.
<link rel="stylesheet" href="">
<script src=""></script>
<link rel="stylesheet" href="dist/bootstrap-treeview.min.css" type="text/css" media="all">
<script src="dist/bootstrap-treeview.min.js"></script>

Step 3: Now we will create tree menu container.

<div class="container">
<h2>Bootstrap Treeview with PHP and MySQL</h2>
<div class="row">
<div class="col-md-6" id="treeview"></div>

Step 4: We will make Ajax request to get JSON data from php server side tree_data.php and passed json data to bootstrap treeview plugin method treeview(). The methiod will set data on div container on which we want to render tree structure. Here we will render data on container id “#treeview”.

url: "tree_data.php",
cache: false,
success: function(response){
$('#treeview').treeview({data: response});

Step 5: Now in tree_data.php script, we will data from MySQL database and then create PHP array of all tree nodes and encode into JSON and returns.
$sql = "SELECT id, name, text, link as href, parent_id FROM treeview";
$res = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
//iterate on results row and create new index array of data
while( $row = mysqli_fetch_assoc($res) ) {
$data[] = $row;
$itemsByReference = array();
// Build array of item references:
foreach($data as $key => &$item) {
$itemsByReference[$item['id']] = &$item;
// Set items as children of the relevant parent item.
foreach($data as $key => &$item) {
if($item['parent_id'] && isset($itemsByReference[$item['parent_id']])) {
$itemsByReference [$item['parent_id']]['nodes'][] = &$item;
// Remove items that were added to parents elsewhere:
foreach($data as $key => &$item) {
if($item['parent_id'] && isset($itemsByReference[$item['parent_id']]))
// Encode:
echo json_encode($data);

You can view the live demo from the Demo link and can download the script from the Download link below.
Demo [sociallocker]Download[/sociallocker]

2 thoughts on “Bootstrap Treeview with PHP and MySQL

  1. Hi sir, thank you very much for this tutorial, its very useful, i have a problem sir i have inserted rows into db its worked fine as above, but after that i was trying to add row parent_id = 0 data, its giving “Uncaught TypeError: Cannot set property ‘nodeId’ of undefined” i dont know what to do please help me sir

Leave a Reply to Sam Cancel reply

Your email address will not be published. Required fields are marked *