0

我有一个递归模型函数:

public function buildPaths ($category_id, $current_path = array()) {
    if (!empty ($current_path)):
        $output = $current_path;
    else:
        $output = array(0 => $category_id);
    endif;

    $query = $this->db->query ("
        SELECT parent_id 
        FROM {$this->prefix}category 
        WHERE category_id = '" . (int)$category_id . "' 
        AND status = '1'");

    if ($query->row['parent_id'] != 0):
        $output[] = $query->row['parent_id'];
        $this->buildPaths($query->row['parent_id'], $output);
    else:
        $output = array_reverse ($output);
        $path = implode ('_', $output);
        return (string)$path;
    endif;
}

例如,传入值 40 应返回以下内容:

3_40

当我$path在模型中回显变量时,它确实显示了正确的值,但是当我通过控制器调用函数时,即:

$result = $this->model_catalog_category->buildPaths(40);

$result返回空。

关于为什么会发生这种情况的任何想法?

4

2 回答 2

0

你有一个 if 流控制并返回 else 尝试退出 ot else

if ($query->row['parent_id'] != 0):
        $output[] = $query->row['parent_id'];
        $this->buildPaths($query->row['parent_id'], $output);
    else:
        $output = array_reverse ($output);
        $path = implode ('_', $output);
    endif;

        return (string)$path;
于 2013-09-01T16:57:05.073 回答
0

感谢andrewsi的回答:

public function buildPaths ($category_id, $current_path = array()) {
    if (!empty ($current_path)):
        $output = $current_path;
    else:
        $output = array(0 => $category_id);
    endif;

    $query = $this->db->query ("
        SELECT parent_id 
        FROM {$this->prefix}category 
        WHERE category_id = '" . (int)$category_id . "' 
        AND status = '1'");

    if ($query->row['parent_id'] != 0):
        $output[] = $query->row['parent_id'];
        return $this->buildPaths($query->row['parent_id'], $output);
    else:
        $output = array_reverse ($output);
        $path = implode ('_', $output);
        return (string)$path;
    endif;
}
于 2013-09-01T17:10:11.050 回答