php - 在递归函数 php 中重新启动第一个循环

标签 php mysql recursion pdo while-loop

我正在尝试创建一系列 <ul><li>创建一个目录/文件结构来导航从 dB 中的表创建的一些文件。表 (tb_lib_manual) 包含文件和文件夹。

如果记录的 fileID 为空条目,则它是文件夹而不是文件。每条记录都有一个 parentID 来显示哪个文件夹是父文件夹,对于根目录中的文件和文件夹,这是 0。

PHP 代码如下:

class library_folders extends system_pageElement 
{
    private $html = '';
    private $i = 0;
    private $stmtArray = array();
    private $objectArray = array();

    function __construct() 
    {
        parent::__construct();
        $this->nextList();
    }


    function nextList($parentID = 0) 
    {
        $qSQL = 'SELECT * FROM tb_lib_manual WHERE parentID=:parentID';
        $stmtArray[$this->i] = $this->dbConnection->prepare($qSQL);
        $stmtArray[$this->i]->bindValue(':parentID', $parentID, PDO::PARAM_INT);
        $stmtArray[$this->i]->execute();
        if($stmtArray[$this->i]->rowCount() > 0)
        {
            $display ='';
            if($parentID != 0)
            {
                $display = ' style="display:none"';
            }
            $this->html .= '<ul' . $display . '>';
        }


        while ($this->objectArray[$this->i] = $stmtArray[$this->i]->fetchObject()) 
        {
            $this->html .= '<li>' . $this->objectArray[$this->i]->title;
            if($this->objectArray[$this->i]->fileID == null)
            {
                //we have a folder!
                $manualID = $this->objectArray[$this->i]->manualID;
                $this->i ++;
                $this->nextList($manualID);
                $this->i--;
            }
            $this->html .= '</li>';
        }


        if($stmtArray[$this->i]->rowCount() > 0)
        {
            $this->html .= '</ul>';
        }   

        echo $this->html;
    }   
function __destruct()
    {
        parent::__destruct();
    }

}

问题是,当代码在调用自身后返回到 while 循环时,它会重新启动循环,而不是从中断的地方继续执行,从而导致在子文件夹中重复。有没有更好的方法来做到这一点,还是我做错了什么!?

表格如下所示:

enter image description here

输出如下: '

  • 手册
  • 手册
  • 文件夹1
  • Nasa exercise
  • 一本手册
  • 一本手册
  • 文件夹1
  • Nasa练习
'

最佳答案

修正了代码,非常愚蠢的错误,在错误的地方回显...

class library_folders extends system_pageElement 
{
    private $html = '';
    private $i = 0;
    private $stmtArray = array();
    private $objectArray = array();

    function __construct() 
    {
        parent::__construct();

        $this->nextList();

        echo $this->html;

    }


    function nextList($parentID = 0) 
    {
        $qSQL = 'SELECT * FROM tb_lib_manual WHERE parentID=:parentID';
        //echo $this->i;        
        $stmtArray[$this->i] = $this->dbConnection->prepare($qSQL);
        $stmtArray[$this->i]->bindValue(':parentID', $parentID, PDO::PARAM_INT);
        $stmtArray[$this->i]->execute();
        if($stmtArray[$this->i]->rowCount() > 0)
        {
            $this->html .= '<ul>';
        }

        while ($this->objectArray[$this->i] = $stmtArray[$this->i]->fetchObject()) 
        {
            $this->html .= '<li>' . $this->objectArray[$this->i]->title;
            if($this->objectArray[$this->i]->fileID == null)
            {
                //we have a folder!
                $manualID = $this->objectArray[$this->i]->manualID;
                $this->i ++;
                $this->nextList($manualID);
                $this->i--;
            }
            $this->html .= '</li>';
        }


        if($stmtArray[$this->i]->rowCount() > 0)
        {
            $this->html .= '</ul>';
        }   

    }   

    function __destruct()
    {
        parent::__destruct();
    }

}

关于php - 在递归函数 php 中重新启动第一个循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30708613/

相关文章:

javascript - 如何使用计划日期和时间触发此示例?

c# - 为什么 PHP 不需要中间层,而其他语言则需要

java - getObject(index, Long) 对于 null 返回 0

PHP mysqli_query 未显示在数据库中

php - self 加入并仅在一个表/一侧忽略空值

mysql - 包含 sql 列表中的字符串

javascript - 调试从 jQuery 调用的 PHP 脚本

python - 计算递归调用 - 汉诺塔

算法分析 : Expected Running Time of Recursive Function Based on a RNG

python - 同时递归两个链表