setQuery($sql); $rows = $db->loadObjectList(); $list = array(); // Get the record from the data first"/> setQuery($sql); $rows = $db->loadObjectList(); $list = array(); // Get the record from the data first">

Home  >  Article  >  Backend Development  >  PHP code PHP code that outputs content according to the hierarchical relationship between upper and lower levels

PHP code PHP code that outputs content according to the hierarchical relationship between upper and lower levels

WBOY
WBOYOriginal
2016-07-29 08:43:191451browse

Copy code The code is as follows:


function getSubComments($parent = 0, $level = 0) {
$db = &JFactory::getDBO();
$sql = "..."; // SQL for querying records
$db->setQuery($sql);
$rows = $db->loadObjectList();
$list = array();
// First get the record set from the data, and then compare the records Add level, 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 call
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 content hierarchically by level
if ($row->level) {
$pre = '';
for ($n = 0; $n < $row->level; $n++)
$pre .= '----';
echo $pre . '|- ';
}
echo strip_tags($row->content);

The above introduces the PHP code that outputs content according to the hierarchical relationship between upper and lower levels, including the content of PHP code. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn