Sumeet Gavhale Sumeet Gavhale - 1 year ago 118
PHP Question

Display Parent-Child data in Table

I fetched some data from mysql I need to display the data in table-tree format but its isn't working. However I tried to display it with the help of list but actually a table format is required.

<?php
session_start();
include 'dbconn.php';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_connect('localhost','root','root');
mysql_select_db('cms');
//select all rows from the main_menu table
$result = mysql_query("select id, label as title, parent as parentid,link from pages");

//create a multidimensional array to hold a list of menu and parent menu
$menu = array(
'menus' => array(),
'parent_menus' => array(),

);

//build the array lists with data from the menu table
while ($row = mysql_fetch_assoc($result)) {
//creates entry into menus array with current menu id ie. $menus['menus'][1]
$menu['menus'][$row['id']] = $row;
//creates entry into parent_menus array. parent_menus array contains a list of all menus with children
$menu['parent_menus'][$row['parentid']][] = $row['id'];
}

// Create the main function to build milti-level menu. It is a recursive function.
function buildMenu($parent, $menu) {
$html = "";
$char=" ";
if (isset($menu['parent_menus'][$parent])) {
foreach ($menu['parent_menus'][$parent] as $menu_id) {
if (!isset($menu['parent_menus'][$menu_id])) {
$html .= "<li>".$menu['menus'][$menu_id]['title']."</li>";
}
if (isset($menu['parent_menus'][$menu_id])) {
$html .= "<li>".$menu['menus'][$menu_id]['title'];
$html .= "<ul>";
$html .= buildMenu($menu_id, $menu);
$html .= "</ul>";
$html .= "</li>";
}
}
}
return $html;
}
echo buildMenu(0, $menu);
?>


the output that I get is:

This is the output I get

I have modified the buildMenu function a bit: I need the indenting in the table what I get is just a column

function buildMenu($parent, $menu) {
$html = "";
$char=" ";
$html.="<table border='3px'>";
if (isset($menu['parent_menus'][$parent])) {
foreach ($menu['parent_menus'][$parent] as $menu_id) {
if (!isset($menu['parent_menus'][$menu_id])) {
$html .= "<tr><td>".$menu['menus'][$menu_id]['title']."</td></tr>";
}
if (isset($menu['parent_menus'][$menu_id])) {
$html .= "<tr><td>".$menu['menus'][$menu_id]['title'];
$html .= "<tr><td>";
$html .= buildMenu($menu_id, $menu);
$html .= "</td></tr>";
$html .= "</td></tr>";
}
}
}
$html.="</table>";
return $html;
}


and getting the output as:

enter image description here

My Aim is to get such structure:

enter image description here

Answer Source

Thank you all for your efforts:

Here is the code what I was expecting

My Final buildMenu function:

echo $html="<table class='test2'><tr data-tt-id='0'><td>Root</td></tr>";

function buildMenu($parent, $menu) {
    $char=" ";
    if (isset($menu['parent_menus'][$parent])) {



   foreach ($menu['parent_menus'][$parent] as $menu_id) {
            //echo "id:".$menu['menus'][$menu_id]['id']."par:".$menu['menus'][$menu_id]['parentid'];
            if (!isset($menu['parent_menus'][$menu_id])) {
                $html .= "<tr data-tt-id='".$menu['menus'][$menu_id]['id']."' data-tt-parent-id='".$menu['menus'][$menu_id]['parentid']."'><td>".$menu['menus'][$menu_id]['title']."</td></tr>";
            }
            if (isset($menu['parent_menus'][$menu_id])) {
                $html .= "<tr data-tt-id='".$menu['menus'][$menu_id]['id']."' data-tt-parent-id='".$menu['menus'][$menu_id]['parentid']."'><td>".$menu['menus'][$menu_id]['title'];
                $html .= "<td>";
                $html .= buildMenu($menu_id, $menu);
                $html .= "</td>";
                $html .= "</td></tr>";
            }
        }
    }
    return $html;
}

echo buildMenu(0, $menu);
echo "</table>";

Final Result: 1:Collapsed Tree 2:Expanded Tree

Collapsed Tree

Expanded Tree

Hope this helps if someone is facing same issue.

And yes for the TreeTable I used a Github Plugin Github: TreeTable Plugin

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download