PHP递归遍历多级分类时如何判断并仅输出最末子级内容
多级分类Tab内容区渲染需求
- 已完成功能:标签标题(tab title)区域已实现父级分类与多级嵌套子分类标题的列表渲染
- 待实现功能:标签内容(Tab Content)区域需匹配标签标题列表渲染对应内容,渲染规则:
- 父级分类存在多级嵌套子分类时,仅展示其最末级子分类的内容
- 分类不存在任何子分类时,展示该分类自身的内容
- 当前问题:现有代码会同时输出所有父级分类以及嵌套子级分类的内容,不符合预期。需要实现的核心逻辑为:判断分类是否存在子级,存在子级则仅输出其最末子级的内容,不存在子级则输出当前分类的内容。
已正常运行的标签标题区域代码
$categoryMulti = array( 'categories' => array(), 'parent_cats' => array() ); $sql = "SELECT `id`, `parent_id`, `name`, `content` FROM categories"; $query = $conn->query($sql); while ($row = $query->fetch(PDO::FETCH_ASSOC)) { $categoryMulti['categories'][$row['id']] = $row; $categoryMulti['parent_cats'][$row['parent_id']][] = $row['id']; } //title for tabs function listCategoryTree($parent, $category) { $css_class = ($parent == 0 ? "parent" : "child"); $html = ''; if (isset($category['parent_cats'][$parent])) { $html .= '<ul class="im-'.$css_class.'">'."\n"; foreach ($category['parent_cats'][$parent] as $cat_id) { if (!isset($category['parent_cats'][$cat_id])) { $html .= '<li id=""> <a href="" data-cap= "'.$css_class.'-'.$cat_id.'">' . $category['categories'][$cat_id]['name'] . '</a>'; '</li>'."\r"; } else { $html .= '<li id=""> <a href="" data-cap= "'.$css_class.'-'.$cat_id.'">' . $category['categories'][$cat_id]['name'] . '</a> <span>arrow icon </span>'."\r"; $html .= listCategoryTree($cat_id, $category); $html .= '</li>'."\r"; } } $html .= '</ul>'."\n"; } return $html; }
存在问题的标签内容区域代码
现有代码问题:会输出所有嵌套子级和父级的内容,未实现仅展示最末子级/无下级则展示自身的规则
//content for tabs $contentTab = array( 'contentCat' => array(), 'contentChild' => array() ); $sql = "SELECT `id`, `parent_id`, `name`, `content` FROM categories"; $query = $conn->query($sql); while ($row = $query->fetch(PDO::FETCH_ASSOC)) { $contentTab['contentCat'][$row['id']] = $row; $contentTab['contentChild'][$row['parent_id']][] = $row['id']; } function listContentTree($parent, $category) { $css_class = ($parent == 0 ? "parent" : "child"); $html = ''; if (isset($category['contentChild'][$parent])) { $html .= '<ul class="im-'.$css_class.'">'."\n"; foreach ($category['contentChild'][$parent] as $cat_id) { if (!isset($category['contentChild'][$cat_id])) { $html .= '<li id="'.$css_class.'-'.$cat_id.'"> <a href="" data-cap= "">im child content test---' . $category['contentCat'][$cat_id]['content'] . '</a>'; '</li>'."\r"; } else { $html .= '<li id="'.$css_class.'-'.$cat_id.'"> <a href="" data-cap= "">hello parent test------' . $category['contentCat'][$cat_id]['content'] . '</a>'."\r"; $html .= listContentTree($cat_id, $category); $html .= '</li>'."\r"; } } $html .= '</ul>'."\n"; } return $html; }
核心判断逻辑
if(has last child){ // 存在末级子节点,展示末级子节点内容 then display content of last child }else{ // 当前节点无下级,展示自身内容 //where parent doesnt have any child display content of parent }
修复后代码
修改listContentTree函数即可,核心改动:有子节点时不渲染当前节点内容,直接递归查找末级节点;仅在遍历到无任何子节点的末级分类时,才渲染对应内容,同时修复原代码中li标签拼接的语法错误。
function listContentTree($parent, $category) { $css_class = ($parent == 0 ? "parent" : "child"); $html = ''; if (isset($category['contentChild'][$parent])) { $html .= '<ul class="im-'.$css_class.'">'."\n"; foreach ($category['contentChild'][$parent] as $cat_id) { if (!isset($category['contentChild'][$cat_id])) { // 无下级,为末级节点,直接渲染自身内容 $html .= '<li id="'.$css_class.'-'.$cat_id.'"> <a href="" data-cap= "">im child content test---' . $category['contentCat'][$cat_id]['content'] . '</a> </li>'."\r"; } else { // 存在下级,不渲染当前节点内容,直接递归处理子节点 $html .= listContentTree($cat_id, $category); } } $html .= '</ul>'."\n"; } return $html; }
内容的提问来源于stack exchange,提问作者esu
相关产品推荐
相关产品推荐

