PHP 板


LINE

看板 PHP  RSS
※ 引述《dinos (守护神)》之铭言: : ※ 引述《kingoface (Vincent)》之铭言: : : 状况是这样的, : : 需要做一个类似目录的东西。 : : 以下是类似的示意图(不好意思,有点丑)比如: : : A : : A_test : : A-1-1 : : A-file : : B : : B-111 : : B-123 : : 222 : : B-test2 : : B-BB : : B-file : : 目标是B-file : : 那在资料表中 : : 该怎麽纪录他是在 B-> 222 -> B-test2->B-BB 底下呢? : : 是要在一个栏位中写成 : : B:222:B-test2:B-BB : : 然後利用:切割字串,还是有更好的方法吗? : 树的话可以考虑MPTT(Modified Preorder Tree Traversal) : 原理是使用节点的左右两侧纪录上下关系,例如原本的树长这样 : A : ┌──┴────┐ : B C : ┌──┼──┐ ┌─┴─┐ : D E F G H : ┌─┴─┐ : I J : 分别在从顶端节点(A)的左侧开始纪录流水号,当有下层时先由下一层的最左侧+1 : 当无下层时再往右侧+1,若无右侧时再到上层+1,直到回到 A : 1A20 : ┌──┴────┐ : 2B9 10C19 : ┌──┼──┐ ┌─┴─┐ : 3D4 5E6 7F8 11G16 17H18 : ┌─┴─┐ : 12I13 14J15 : 比如要找 C 的子孙节点有哪些,那只要找左侧数字大於10,且右侧数字小於19 : G 父 小於11 大於16 : 跟另外两种方式 一:像你的作法,纪录完整节点 : 二:只纪录 parent node : 比较起来,mysql 的查询操作上,特别是固定长度的资料表时 : 在新增或搬移节点时,MPTT 会比一二都来得没效率 : 但在寻找或删节点除时,MPTT 会比一二效率表现好上很多 : 若你的资料是不常异动(insert/update/delete)且经常要用来寻找(select)的话, : 可以考虑一下 MPTT 依照大大提供的方法实作,找到了wiki的教学 建立好资料表後 id node left right level 1 index 1 20 0 2 pageB 2 13 1 3 pageC 3 6 2 4 pageD 4 5 3 5 pageE 7 12 2 7 pageF 8 11 3 8 pageG 9 10 4 9 pageBBB 14 19 1 10 pageCCC 15 18 2 11 pageDDD 16 17 3 利用 $sql="SELECT Child.Node, Child.LEFT, Child.RIGHT FROM menu AS Child, menu AS Parent WHERE Child.level = Parent.level + 1 AND Child.LEFT > Parent.LEFT AND Child.RIGHT < Parent.RIGHT AND Parent.LEFT = 1"; 可以找到index底下有两个子目录. 我分别在第一层和第二层做了两次的搜寻 但是问题来了..... 如果今天可能有5~7层或是更多,那不就要有更多的搜寻语法 这时候可能需要有巢状回圈的概念,但是我完全不知道该把回圈写在哪... 以下是我的程式码,请各位大大指点一下...m(_ _)m //$sql 是指定找出index的子目录 $sql="SELECT Child.Node, Child.LEFT, Child.RIGHT FROM menu AS Child, menu AS Parent WHERE Child.level = Parent.level + 1 AND Child.LEFT > Parent.LEFT AND Child.RIGHT < Parent.RIGHT AND Parent.LEFT = 1"; $result_for = $mysqli->query($sql); $html_tree.='<ol class="tree"><li><label for="subsubfolder2">index</label> <input type="checkbox"/><ol>'; while($row_ans = $result_for->fetch_array()){ //$sql_2找出子目录以後,再找一次子目录 $sql_2="SELECT Child.Node, Child.LEFT, Child.RIGHT FROM menu AS Child, menu AS Parent WHERE Child.level = Parent.level + 1 AND Child.LEFT > Parent.LEFT AND Child.RIGHT < Parent.RIGHT AND Parent.LEFT = '{$row_ans[1]}'"; $result_sub = $mysqli->query($sql_2); $hmtl_tree_2='<ol>'; while($row_ans_2 = $result_sub->fetch_array()){ $hmtl_tree_2.='<li><label for="subsubfolder2">'.$row_ans_2['m_Node'].'</label> <input type="checkbox" /> </li>'; } $hmtl_tree_2.='</ol>'; $html_tree.='<li><label for="subsubfolder2">'.$row_ans['m_Node'].'</label> <input type="checkbox" " />'.$hmtl_tree_2.'</li>'; } $html_tree.='</ol></li></ol>'; 我现在卡在子目录中的子目录,或是更多的子目录,我应该怎麽找呢!? 是写另外一个function吗? --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 59.124.195.130
1F:→ gname:recursive call 06/18 11:32
2F:推 rickysu:其实我想说 Propel 已经内建了,不用自己手刻的要死要活 06/18 12:14
3F:→ dinos:你要找什麽?输出树状结构图吗? 06/18 12:16
4F:→ kingoface:恩,做出像档案总管的目录,让使用者可以一层一层点. 06/18 12:25
5F:→ kingoface:我正在研究1F大大提供的方法,只差把回圈写出来溜! 06/18 12:25







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Boy-Girl站内搜寻

TOP