按上下级层次关系输出内容的PHP代码
前端之家收集整理的这篇文章主要介绍了
按上下级层次关系输出内容的PHP代码,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<div class="codetitle"><a style="CURSOR: pointer" data="46183" class="copybut" id="copybut46183" onclick="doCopy('code46183')"> 代码如下:
<div class="codebody" id="code46183">
function getSubComments($parent = 0,$level = 0) {
$db = &JFactory::getDBO(); $
sql = "..."; //
查询记录的
sql $db->setQuery($
sql);
$rows = $db->loadObjectList(); $list = array(); // 先从数据得到记录集,再对记录
添加level,父层level = 0,它的下级level = 1,如此类推
foreach ($rows as $row) {
$row->level = $level;
$list[] = $row; $tmpArr = getSubComments($row->id,$level + 1); // 递归
调用 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;
}
} // 按level分层次
输出内容 if ($row->level) {
$pre = '';
for ($n = 0; $n < $row->level; $n++)
$pre .= '----'; echo $pre . '|- ';
}
echo strip_tags($row->content);