im trying to achieve above condition.
i have successful list parent and multi-level child title in tab title section
Now in Tab Content im trying to list content for the title listed in Tab title In Tab Content if parent have multi-level child then only last child content should display but if parent does have any child then parent content should display
But im getting content for all title parent as well as nested child here i want last child content only but if parent does have child then parent content should be displayed
how to check if parent have any child, if parent have child then display content for last child only, if parent doesn’t have child then display parent content
Code for displaying Multi-level-nested child Title in Tab Title
Working with 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 display last child content if parent have multi-level-nested child and if parent does have any child display parent content but its display content for all nested-child and parent content too.
//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 check to for 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
}