【发布时间】: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