Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从父-子(邻接)表中计算PHP中的分层目录_Php_Mysql_Hierarchical - Fatal编程技术网

如何从父-子(邻接)表中计算PHP中的分层目录

如何从父-子(邻接)表中计算PHP中的分层目录,php,mysql,hierarchical,Php,Mysql,Hierarchical,我正试图在一个邻接树模型(id,parent_id)中从MySQL数据库中计算/创建或生成一个PHP目录。到目前为止,这就是我在回应输出时所取得的成果 1. Category 1 1 Subcategory 1 2 Subcategory 2 3 Subcategory 3 2. Category 2 1. Subcategory 1 1. Subcategory Subcategory 1 2. Subcategory Subcategory

我正试图在一个邻接树模型(id,parent_id)中从MySQL数据库中计算/创建或生成一个PHP目录。到目前为止,这就是我在回应输出时所取得的成果

1. Category 1
   1 Subcategory 1
   2 Subcategory 2
   3 Subcategory 3
2.  Category 2
   1. Subcategory 1
      1. Subcategory Subcategory 1
      2. Subcategory Subcategory 2
   2 Subcategory 2
      1 Subcategory 1
      2 Subcategory 2
我非常接近,但我想要的输出是:

1. Category 1
    1.1 Subcategory 1
    1.2 Subcategory 2
    1.3 Subcategory 3
2.  Category 2
   2.1. Subcategory 1
      2.1.1. Subcategory Subcategory 1
      2.1.2. Subcategory Subcategory 2
   2.2 Subcategory 2
      2.2.1 Subcategory 1
      2.2.2 Subcategory 2
换句话说,我希望在多级层次结构中使用目录格式,如下所示:Chapter.Subchapter.Subchapter.Subchapter TITLE

我尝试过使用一个递归数组,它保留当前索引并连接到上一个索引,但最终在每个项之前添加一个奇怪的长数字,例如

0.11.2.11.2.3.4.5.6.7.11..11.2.3.4.5.6计算机

相反,它应该是:

2.7.6计算机

(其他编号为其他项目的编号)

这是我一直在研究的代码

renumber(0,0,1,0);

function renumber($parent_id,$level=0,$counter=1) {

    // Counter level keeps track of the current index number
    $counterlevel[$level]=$counter;

    $query = "SELECT defaultTitle, id, pid FROM defaultChapters  WHERE  pid=".$parent_id;

    $res = mysql_query($query) or die(mysql_error());

    // Exit if there are no tree leafs
    if(mysql_num_rows($res) == 0) {return;}

        while (list ($title, $id) = mysql_fetch_row($res))
    {   

        $leveltext[$level][$counterlevel[$level]] = $section.".".$counterlevel[$level];


        echo str_repeat("......",$level)." ".$counterlevel[$level]." ".$section." ".$title."<BR>";
        // Increase the counter of the current level    
        $counterlevel[$level]++;


        // Initialize the level counter                     
        if(!$counterlevel[$level+1]) {

        $counterlevel[$level+1] = 1;
         }

        // Start the function again to find children
        renumber($id,$level+1,$counterlevel[$level+1]);

    } // End While



}
重新编号(0,0,1,0);
函数重新编号($parent\u id,$level=0,$counter=1){
//计数器级别跟踪当前索引编号
$counterlevel[$level]=$counter;
$query=“从defaultChapters中选择defaultTitle、id、pid,其中pid=”.$parent\u id;
$res=mysql\u query($query)或die(mysql\u error());
//如果没有树叶,请退出
if(mysql_num_rows($res)==0){return;}
while(list($title,$id)=mysql\u fetch\u row($res))
{   
$leveltext[$level][$counterlevel[$level]]=$section.“.”$counterlevel[$level];
echo str_repeat(“…”,$level)。“..counterlevel[$level]”.“$section.”.“$title.”;
//增加当前级别的计数器
$counterlevel[$level]++;
//初始化液位计数器
如果(!$counterlevel[$level+1]){
$counterlevel[$level+1]=1;
}
//再次启动该函数以查找子项
重新编号($id、$level+1、$counterlevel[$level+1]);
}//结束时
}
我已经浏览了所有的技术支持论坛,包括这一个,似乎从来没有人发布过这方面的算法,只是在任何地方都找不到这方面的示例代码。有数百个教程和代码可以从mysql数据库中获取php中的层次结构树,而无需编号,但对于php中的层次结构目录的编号却一无所知


是否也可以使用SQL查询执行此操作?

我将对其进行一点重构,并将编号传递到当前调用:

function renumber($parent_id = 0, $level = 0, $prefix = '')
{
    // we don't need pid in the results
    $query = "SELECT defaultTitle, id
        FROM defaultChapters 
        WHERE pid=$parent_id";

    $res = mysql_query($query) or die(mysql_error());

    // Exit if there are no tree leafs
    if (mysql_num_rows($res) == 0) {
        return;
    }

    // start numbering at 1
    $nr = 1;
    while (list($title, $id) = mysql_fetch_row($res)) {
        // dropped section, not sure where it was used
        echo str_repeat("......", $level) . " $prefix.$nr $title<BR>";

        // Start the function again to find children
        renumber($id, $level + 1, strlen($prefix) ? "$prefix.$nr." : "$nr.");
        // advance list numbering
        ++$nr;
    }
}

renumber();
函数重新编号($parent\u id=0,$level=0,$prefix='')
{
//我们在结果中不需要pid
$query=“选择默认标题,id
来自默认章节
其中pid=$parent_id”;
$res=mysql\u query($query)或die(mysql\u error());
//如果没有树叶,请退出
if(mysql_num_rows($res)==0){
回来
}
//从1开始编号
$nr=1;
while(list($title,$id)=mysql\u fetch\u row($res)){
//删除的部分,不确定在何处使用
echo str_repeat(“…”,$level)。“$prefix.$nr$title
”; //再次启动该函数以查找子项 重新编号($id,$level+1,strlen($prefix)“$prefix.$nr.”:“$nr.”; //预先清单编号 ++$nr; } } 重新编号();
我会在MySQL用户定义函数中完成。或者,在函数中添加第5个参数,该参数只传递例程在上层找到的十进制数字字符串,并将其前置到
echo
。杰克,哇,非常感谢。它就像一个符咒。我想弄明白这件事,真是疯了。你想知道的$section部分是一个变量,你称之为$prefix,我之前有一行代码给它赋值,当我发布我的问题时,我无意中删除了它,当我试图编辑它时,我意识到我不能编辑问题,直到我有2000个学分。