如何渲染一个树在html + php(codeIgniter) [英] How render a Tree in html + php(codeIgniter)

查看:149
本文介绍了如何渲染一个树在html + php(codeIgniter)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an adjacency model list and this is the query:

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级深度的查询

this is a query to make 24 levels of depth using the adjacency model list

之后,我会:

<? 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; ?>

它只渲染每一行的字段,我真的不知道热使它的hiarcharchical,我使用codeigniter,什么是更好地使用这里行或对象

it render just the fields of every row, and i really don't know hot make it hieararchical, i am using codeigniter, what is better to use here rows or objects??

我需要这样的东西:

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
   }
}

没有重复是可能的?

推荐答案

看起来有点复杂和不灵活你在这里做什么Gilles。是不是更好地创建您的数据库表,每行有自己的category_id和parent_id?即给予顶级父亲parent_id为0,并且孩子采用他们的父类的category_id的parent_id。这将给你无限的深度,将更容易编码,当你渲染树。例如:

Seems a bit complicated and inflexible what you're doing here Gilles. Would it not be better to create your database table with each row having its own category_id and a parent_id? ie give the top level parents a parent_id of 0 and the children take the parent_id of their parent's category_id. This would give you unlimited depth and will be easier to code when you're rendering the tree. For example:

您的第一层次结构的SQL -

SQL for your first level of hierarchy -

SELECT * FROM (your_table)WHERE parent_id = 0

SELECT * FROM (your_table) WHERE parent_id=0

您的第二层次的SQL -

SQL for your second level of hierarcy -

SELECT * FROM(your_table)WHERE parent_id =(第一级的category_id)

SELECT * FROM(your_table)WHERE parent_id =(第二级别的category_id)

等等...

这篇关于如何渲染一个树在html + php(codeIgniter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆