我从mysql中获取了一些数据我需要以表树格式显示数据,但它无法正常工作。但是我尝试在列表的帮助下显示它,但实际上需要表格格式。
<?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);
?>
我得到的输出是:
我已经修改了buildMenu函数:我需要在表中缩进我得到的只是一列
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;
}
并将输出设为:
我的目标是获得这样的结构:
答案 0 :(得分:1)
谢谢大家的努力:
以下是我期待的代码
我的最终buildMenu功能:
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>";
最终结果:1:折叠树2:扩展树
希望如果有人面临同样的问题,这会有所帮助。
对于TreeTable我使用了Github插件Github: TreeTable Plugin
答案 1 :(得分:0)
好吧,如果只是看起来然后相应地设置表格以使你的缩进正确,例如
td {padding-left:10px}
我认为语义上的“正确”方式是将子弹列表样式(作为您的第一个示例),并根据需要删除子弹和样式
ul {
list-style-type: none;
padding: 0px;
margin: 0px;
}
ul li {
padding-left: 15px;
}