Output the content of the PHP code in a hierarchy

  • 2020-03-31 20:56:52
  • OfStack


function getSubComments($parent = 0, $level = 0) { 
$db = &JFactory::getDBO(); 

$sql = "..."; //Query the SQL of the record
$db->setQuery($sql); 
$rows = $db->loadObjectList(); 

$list = array(); 

//Get the recordset from the data first, then add the level to the record, the parent level = 0, its subordinate level = 1, and so on
foreach ($rows as $row) { 
$row->level = $level; 
$list[] = $row; 

$tmpArr = getSubComments($row->id, $level + 1); //Recursive calls
if (count($tmpArr)) { 
foreach ($tmpArr as $tmpRow) { 
$list[] = $tmpRow; 
} 
} 
} 

return $list; 
} 

$list = array(); 
foreach ($tmpList as $row) { 
$row->level = 0; 
$list[] = $row; 
$tmpList2 = getSubComments($row->id, 1); 
foreach ($tmpList2 as $row2) { 
$list[] = $row2; 
} 
} 

//Output is layered by level
if ($row->level) { 
$pre = ''; 
for ($n = 0; $n < $row->level; $n++) 
$pre .= '----'; 

echo $pre . '|- '; 
} 
echo strip_tags($row->content);

Related articles: