|
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);
php技術:按上下級層次關系輸出內容的PHP代碼,轉載需保留來源!
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。