首页 文章

如何在最后调用递归函数时返回变量?

提问于
浏览
0

我有一个递归函数 . 如何在上次调用时从此递归函数返回值?

public function deneme($parent_id = 0, $sub_mark = 0, $str = '') {
    $this->db->select('*');
    $this->db->from('categories');
    $this->db->where('parent_id = ' . $parent_id);
    $this->db->order_by('id', 'ASC');
    $query = $this->db->get();
    if($query->num_rows() > 0) {
        $str .= '<ul>';
        foreach($query->result_array() as $row) {
            if ($parent_id == 0) {
                $str .= '<li class="active">';
            } else {
                $str .= '<li>';
            }
            $str .= '<a href="index.html">' . $row['name'] . '</a> </li> '; 
            $sub_mark++;
            $this->deneme($row['id'], $sub_mark, $str);
        }
        $str .= '</ul>';
    }
}

1 回答

  • 0

    看起来好像需要进行一些更改,首先从最后的例程中返回构建的字符串...

    }
            $str .= '</ul>';
        }
        return $str;
    }
    

    第二个是你递归调用例程的地方,你需要将返回值设置为你正在生成的字符串...

    $str = $this->deneme($row['id'], $sub_mark, $str);
    

相关问题