按上下级层次关系输出内容的PHP代码
程序员文章站
2022-06-29 18:18:24
复制代码 代码如下:function getsubcomments($parent = 0, $level = 0) { $db = &jfactory::getdbo()...
复制代码 代码如下:
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);
下一篇: Centos下安装docker教程