我有一个代表伪目录系统的 mysql 表:
CREATE TABLE `file_directories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parent_id` int(11) DEFAULT NULL,
`name` varchar(255) NOT NULL,
`level` int(11) NOT NULL DEFAULT '1',
`created` datetime NOT NULL,
PRIMARY KEY (`name`,`id`),
KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1
当用户浏览此系统时,我们的函数会收到一条由 name
列中的条目组成的路径。
因此,像 first/child of first/grandchild
或 second/child of secondary/grandchild
这样的路径将是有效路径,并且在数据库中看起来像这样。
/----------------------------------------------------\
| id | parent_id | name | level | created |
|----|-----------|-----------------|-------|---------|
| 1 | NULL | First | 1 | ... |
| 2 | 1 | Child of First | 2 | ... |
| 3 | 2 | Grandchild | 3 | ... |
| 4 | NULL | Second | 1 | ... |
| 5 | 4 | Child of Second | 2 | ... |
| 6 | 5 | Grandchild | 3 | ... |
\----------------------------------------------------/
现在,如果我想列出子目录,我的流程是这样的:
$path = 'first/child of first'; // demo data
$path = explode('/', $path); //array('first', 'child of first');
$level = count($path);
$name = end($path);
//query is not actually built like this, it uses the Codeigniter Active Records library
//but this is effectively the end result,
$sql = "SELECT * FROM `file_directories` WHERE `name` = '$name' AND `level` = $level";
///etc
这工作正常,直到我们处理具有相同名称且存在于同一级别的孙
目录。
目录结构强制只能存在一个具有相同 parent_id
和 name
的目录,但具有不同 的相同
可以存在于同一name
目录Parent_id级别
。
我无法更改传递的数据,因此我能想到的唯一方法是从根节点开始,然后循环执行多个查询以找到正确的子节点。
因此,对于第二个孙子,查询将是。
$parent_id = NULL;
foreach($path as $seg){
$id = SQL: SELECT `id` FROM `file_directories` WHERE `name` = '$seg' AND `parent_id` = (IS NULL for root node) $parent_id;
}
//Get the actual node
$node = SQL: SELECT `*` FROM `file_directories` WHERE `id` = '$id';
但是,这是很多查询,因此,在不更改给定的数据的情况下,是否有更好的方法来跟踪树?或者选择正确的节点?
最佳答案
了解递归函数的奇妙之处: 这段代码稍微简化了。
function select_child($node_id, $level, $target_level) {
if(SELECT new_node_id WHERE parent_id = $node_id)
if($level = $target_level) {
return($new_node);
} else {
$results->add(select_child($new_node_id, ($level +1), $target_level);
}
} else {
return(NULL);
}
}
此函数循环遍历给定节点的子节点,直到找到“叶子”(没有子节点的节点)或达到目标级别。如果您正在使用树,则大多数函数都必须是递归的(调用自身直到匹配指定的条件)。
关于php - 如何通过mysql表跟踪parent_id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14547246/