【问题标题】:Creating a parentlist with recursive function error创建具有递归函数错误的父列表
【发布时间】:2016-12-05 23:10:13
【问题描述】:

我正在尝试为每个 fid 制作我的 pid 的父列表。

例子:

fid     pid
-----------
 1      0
 34     1
 35     34
 36     35

我尝试创建一个递归函数,但出现错误

致命错误:第 91 行 C:\xampp\htdocs\myproject\application\modules\admin\controllers\forums\Forum_management.php 中允许的内存大小为 134217728 字节已用尽(尝试分配 200704 字节)

致命错误:第 1 行 C:\xampp\htdocs\myproject\system\core\Exceptions.php 中允许的内存大小为 134217728 字节已用尽(尝试分配 32768 字节)

我想要达到的目标是

如果需要获取 fid = 36 的父列表,那么应该能够回显 35、34、1

问题:我怎样才能使用递归函数这样才能得到pid列表,例如fid = 36然后应该能够回显35、34、1

public function index() {
    $results = $this->make_parent_list('36');

    echo implode(',', $results);
}


public function make_parent_list($fid, $parents = array())
{
    $this->db->where('fid', $fid);
    $this->db->where('pid >', '0');
    $query = $this->db->get('forum');
   
    if ($query->num_rows() > 0) {
        
        foreach($query->result() as $row) {
            
            $parents[] = $row->pid;

            $this->make_parent_list($row->fid, $parents);

        }
    }   

   return $parents; 
} 

更新

我现在在下面尝试过这种方式,但是当我在索引中回显它时 Message: implode(): Invalid arguments passed

public function make_parent_list($fid)
{

    $sql = "SELECT * FROM forum WHERE fid  = '" . $fid . "'"; 
    
    $query = $this->db->query($sql);

    $arr = array();
    
    foreach ($query->result() as $row) {

        if ($row->pid) {

            $arr[] = $row->pid;

            $arr[] = $this->make_parent_list($row->pid);

        }
        
    }

    return $arr;

}

public function index() {
    $results = $this->make_parent_list('36');

    foreach ($results as $result) {
        echo implode(',', $result);
    }
}

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    解决方案

    我现在已经在谷歌搜索和阅读之后解决了这个问题

    在这里找到解决方案How to get all the child and grandchild categories of a parent category in codeigniter?

    public functio index() {
    
        $all_categories = $this->make_parent_list(36);
    
        $arr = array();
    
        foreach ($all_categories as $category) {
            $arr[] = $category['pid'];
        }
    
        echo implode(',', $arr);
    }
    
    function make_parent_list($fid) {
        $forum_data = array();
    
        $forum_query = $this->db->query("SELECT * FROM forum WHERE fid = '" . (int)$fid . "'");
    
        foreach ($forum_query->result() as $forum) {
    
            if ($forum->pid > 0) {
    
                $forum_data[] = array(
                    'pid' => $forum->pid,
                );
    
                $forum_children = $this->make_parent_list($forum->pid);
    
                if ($forum_children) {
                    $forum_data = array_merge($forum_children, $forum_data);
                }           
    
            }
        }
    
        return $forum_data;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-01
      • 1970-01-01
      • 2017-07-08
      • 2022-01-01
      • 1970-01-01
      • 2013-04-24
      • 2012-01-15
      相关资源
      最近更新 更多