Find last child in recursive function – PHP – SitePoint Forums

Find last child in recursive function – PHP – SitePoint Forums


i am trying to get the above condition.

I have a successful list of parent and child titles at various levels tab title section

Now inside Tab content I’m trying to list content for the title that appears in Tab title In Tab Content, if parents have multilevel children, only the last secondary content should be displayed, but if parents have any children, the main content should be displayed.

But I’m getting content for all the parents in the title, as well as for the child nested here, I just want the content of the last child, but if the parent has a child, the main content should be displayed

how to check if parents have children, if parents have children, show content only for last child, if parents have no children, show parent content

Code to display the multi-level secondary title in the tab title
Working with the solution provided by Drummin

$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;
}

ode trying to show the last secondary content if the parents have a child nested on several levels and if the parents have a child, show the main content but also its display content for all nested child and main content.

//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;
}

how to check the last child.

if(has last child){
//multi-level child if its 4level nested child then only 4th(last child) content should display
  then display content of last child
}else{
//where parent doesn't have any child
  display content of parent
}



Source link

Related post

Recreation and Sport Management Program to Launch New Outdoor Leadership Minor

Recreation and Sport Management Program to Launch New Outdoor…

Photo sent The College of Education and Health Professions’ sports and recreation management program…
Open House: What steps can be taken to check the rising cases of suicides among youths? : The Tribune India

Open House: What steps can be taken to check…

Education institution should set up helplines To begin with, it should be made clear that stress is a physical reaction to…
Here are the top 25 start-ups to work for in India

Here are the top 25 start-ups to work for…

It’s also great to see young professionals embracing India’s startup ecosystem, with 56% of all hires at the top 25 startups…

Leave a Reply

Your email address will not be published.