我正在创建一个带有父母的类别的树结构,它可以从儿童中以这样的方式调用:
ID | Name | ParentID
1 1 0
2 2 1
3 3 2
4 4 1其结果是:
1 = 1
2 = 1 -> 2
3 = 1 -> 2 -> 3
4 = 1 -> 4这意味着3是2的子级,它是1的子级。
当我试图得到这个想法时(用->来显示设置了什么关系),我只得到二年级(1 -> 2),但没有到第三(1->2->3),因为我使用了循环函数。
//put all ID's in an array
while ($row2 = $connector->fetchArray($result2)){
$id = $row2['ID'];
$parents[$id] = $row2['name'];
}
// show the tree-structure
while ($row = $connector->fetchArray($result)){
if($row['parentid']!=0)echo $parents[$row['parentid']].' -> ';
echo $row['name'].' - ';
echo '<br>';
}我想改变两件事:
SELECT ID,name,parentid FROM categories从…获取结果。我只想申报一次。
感谢所有的好答案。我使用了最简单、代码较少的实现方法:。
$result = $connector->query('SELECT ID,name,parentid FROM categories');
// Get an array containing the results.
$parents = array();
while ($row = $connector->fetchArray($result)){
$id = $row['ID'];
$parents[$id] = array('ID' => $row['ID'],'name' => $row['name'],'parentid' => $row['parentid']);
}
foreach ($parents as $id => $row){
$pid=$id;
$arrTmp= array();
do { // iterate through all parents until top is reached
$arrTmp[]=$pid;
$pid = $parents[$pid]['parentid'];
}while ($pid != 0);
$arrTmp = array_reverse($arrTmp);
foreach($arrTmp as $id){
echo $parents[$id]['name'].' -> ';
}
echo '<br>';
}发布于 2009-08-04 13:37:11
如果你真的想用父ids来做象形文字(只适用于少数项目/象形文字)
我稍微修改了您的代码(我没有测试它,因此可能会出现一些语法错误):
//put all recordsets in an array to save second query
while ($row2 = $connector->fetchArray($result2)){
$id = $row2['ID'];
$parents[$id] = array('name' => $row2['name'],'parent' => $row2['parentid']);
}
// show the tree-structure
foreach ($parents as $id => $row){
$pid = $row['parentid'];
while ($pid != 0){ // iterate through all parents until top is reached
echo $parents[$pid]['name'].' -> ';
$pid = $parents[$pid]['parentid'];
}
echo $parents[$id]['name'].' - ';
echo '<br>';
}要回答你的评论:
$parents = array();
$parents[2] = array('ID'=>2,'name'=>'General','parentid'=>0);
$parents[3] = array('ID'=>3,'name'=>'Gadgets','parentid'=>2);
$parents[4] = array('ID'=>4,'name'=>'iPhone','parentid'=>3);
foreach ($parents as $id => $row){
$pid=$id;
$arrTmp= array();
do { // iterate through all parents until top is reached
$arrTmp[]=$pid;
$pid = $parents[$pid]['parentid'];
}while ($pid != 0);
$arrTmp = array_reverse($arrTmp);
foreach($arrTmp as $id){
echo $parents[$id]['name'].' -> ';
}
echo '<br>';
}打印出来:
通用-> 通用->小工具-> 通用->小工具-> iPhone ->
发布于 2009-08-04 13:11:51
与其让PHP将项目组织到树中,不如让数据库为您做这件事呢?我发现这个关于分层数据的文章非常好,并且示例几乎与您的相同。
编辑
使用邻接模型获取完整树的SQL并不理想。正如本文所解释的,即使是一个较小的层次结构,它也需要相当多的连接。您不可能使用嵌套的Set方法吗?无论层次结构的大小如何,SQL都保持不变,插入和删除也不太困难。
发布于 2009-08-04 15:19:14
也许用OOP更容易。只需按parentId排序查询
注意: listChildren方法和底部的打印输出就在那里,以显示它是正确列出的。我没有解释这个问题,显示是重要的。
class Element {
public $id;
public $name;
public $parent = null;
public $children = array();
public function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
}
public function addChild($element)
{
$this->children[$element->id] = $element;
$element->setParent($this);
}
public function setParent($element)
{
$this->parent = $element;
}
public function hasChildren()
{
return !empty($this->children);
}
public function listChildren()
{
if (empty($this->children)) {
return null;
}
$out = array();
foreach ($this->children as $child) {
$data = $child->id . ':' . $child->name;
$subChildren = $child->listChildren();
if ($subChildren !== null) {
$data .= '[' . $subChildren . ']';
}
$out[] = $data;
}
return implode(',', $out);
}
}
$elements = array();
$noParents = array();
while ($row = $connector->fetchArray($result)) {
$elements[$row['id']] = $element = new Element($row['id'], $row['name']);
if (isset($elements[$row['parent']])) {
$elements[$row['parent']]->addChild($element);
} else {
$noParents[] = $element;
}
}
foreach ($noParents as $element) {
if ($element->hasChildren()) {
echo "Element {$element->id} has children {$element->listChildren()}.\n";
} else {
echo "Element {$element->id} has no children.\n";
}
}https://stackoverflow.com/questions/1227399
复制相似问题