如何在 html + php(codeIgniter) 中渲染树

Posted

技术标签:

【中文标题】如何在 html + php(codeIgniter) 中渲染树【英文标题】:How render a Tree in html + php(codeIgniter) 【发布时间】:2012-07-08 06:33:03 【问题描述】:

我有一个邻接模型列表,这是查询:

SELECT t1.FIO AS lev1, t2.FIO AS lev2, t3.FIO AS lev3, t4.FIO AS lev4, t5.FIO AS lev5, t6.FIO AS lev6, t7.FIO AS lev7, t8.FIO AS lev8, t9.FIO AS lev9, t10.FIO AS lev10, t11.FIO AS lev11, t12.FIO AS lev12, t13.FIO AS lev13, t14.FIO AS lev14, t15.FIO AS lev15, t16.FIO AS lev16, t17.FIO AS lev17, t18.FIO AS lev18, t19.FIO AS lev19, t20.FIO AS lev20, t21.FIO AS lev21, t22.FIO AS lev22, t23.FIO AS lev23, t24.FIO AS lev24 FROM users AS t1 LEFT JOIN users AS t2 ON t2.parent_id = t1.id LEFT JOIN users AS t3 ON t3.parent_id = t2.id LEFT JOIN users AS t4 ON t4.parent_id = t3.id LEFT JOIN users AS t5 ON t5.parent_id = t4.id LEFT JOIN users AS t6 ON t6.parent_id = t5.id LEFT JOIN users AS t7 ON t7.parent_id = t6.id LEFT JOIN users AS t8 ON t8.parent_id = t7.id LEFT JOIN users AS t9 ON t9.parent_id = t8.id LEFT JOIN users AS t10 ON t10.parent_id = t9.id LEFT JOIN users AS t11 ON t11.parent_id = t10.id LEFT JOIN users AS t12 ON t12.parent_id = t11.id LEFT JOIN users AS t13 ON t13.parent_id = t12.id LEFT JOIN users AS t14 ON t14.parent_id = t13.id LEFT JOIN users AS t15 ON t15.parent_id = t14.id LEFT JOIN users AS t16 ON t16.parent_id = t15.id LEFT JOIN users AS t17 ON t17.parent_id = t16.id LEFT JOIN users AS t18 ON t18.parent_id = t17.id LEFT JOIN users AS t19 ON t19.parent_id = t18.id LEFT JOIN users AS t20 ON t20.parent_id = t19.id LEFT JOIN users AS t21 ON t21.parent_id = t20.id LEFT JOIN users AS t22 ON t22.parent_id = t21.id LEFT JOIN users AS t23 ON t23.parent_id = t22.id LEFT JOIN users AS t24 ON t24.parent_id = t23.id LEFT JOIN users AS t25 ON t25.parent_id = t24.id WHERE t1.id = 16

这是一个使用邻接模型列表进行 24 级深度的查询

然后我做了这个:

<? for($i = 0; $i < $query->num_rows(); $i++): ?>
<? $row = $query->row($i); ?>
    <? for($n = 1; $n < 25; $n++): ?>
    <? $lev = "lev$n"; ?>
    <?= $row->$lev; ?>
    <? endfor; ?>
<? endfor; ?>

它只渲染每一行的字段,我真的不知道热使它分层,我正在使用codeigniter,在这里使用行或对象更好吗??

我需要做这样的事情:

root_parent 
   parent_1 
      child_1.name
      child_2.name
      child_3.name
   

   parent_2 
      child_1.name
      child_2.name
      child_3.name
   

   parent_3 
      child_1.name
      child_2.name
      child_3.name
   

没有重复是可能的吗?

【问题讨论】:

neved 做了这样的事情,如果有人可以给我一个阅读链接或更好的小例子,我会很感激 使用 ajax 加载树不要加载完整的树。使用类似mbraak.github.com/jqTree 【参考方案1】:

Gilles 你在这里做的事情似乎有点复杂和不灵活。创建数据库表,每行都有自己的 category_id 和 parent_id 不是更好吗?即,给***父母的parent_id 为0,孩子们取父母的category_id 的parent_id。这将为您提供无限的深度,并且在您渲染树时更容易编码。例如:

第一级层次结构的 SQL -

SELECT * FROM (your_table) WHERE parent_id=0

第二级层次结构的 SQL -

SELECT * FROM (your_table) WHERE parent_id=(category_id of first level)

第三级层次结构的 SQL -

SELECT * FROM (your_table) WHERE parent_id=(category_id of second level)

等等……

【讨论】:

但是如果该 SELECT 的每个查询都返回多行并且 mysql 给我一个错误怎么办? ***.com/questions/4452472/… 刚刚用 Codeigniter 尝试了这个实现,效果很好。为 toUL 函数构建一个助手并更改数组操作以处理从查询返回的行。这样做的好处是您只需要一个查询来选择类别表中的所有行。

以上是关于如何在 html + php(codeIgniter) 中渲染树的主要内容,如果未能解决你的问题,请参考以下文章

如何修复 php codeigniter 中的 DataTables 分页和搜索框?

如何在每个页面中加载 CodeIgniter 助手?

CodeIgniter / PHP:如何在 .php 视图页面中获取 URI

如何在 Windows Azure 上重写 Codeigniter 的 index.php

如何删除codeigniter路径中的“index.php”

CodeIgniter - 如何在项目中加载 stripe-php 库?