PHP模型不返回结果
我有一个递归模型函数:PHP模型不返回结果,php,opencart,Php,Opencart,我有一个递归模型函数: 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->quer
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\u 40
当我在模型中回显变量$path
时,它确实显示了正确的值,但当我通过控制器ie调用函数时:
$result=$this->model\u catalog\u category->buildpath(40)代码>
$result
返回空值
你知道为什么会发生这种情况吗?你有一个if流控制,返回到else,试着把otelse放在外面
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;
感谢您的回答:
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;
}
您在最后一个else语句中只返回了return
ing-如果($query->row['parent\u id']!=0):
,则不会返回任何内容。正确的,它是一个递归函数,在达到parent\u id 0之前调用自身,然后将$output数组格式化为字符串并返回$path。在那之前什么都不能退。好的,我明白了。。。我感到困惑,因为通常我不会在else语句中返回。工作完美现在谢谢!你能用你修改过的代码添加一个答案,并在可以的时候接受它吗?