複製程式碼 程式碼如下:
function getSubComments($parent = 0, $level = 0) {
Factory = &J$parent = 0, $level = 0) {
Factory ::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 level; $n++)
$pre .= '----';
echo $pre . '|- ';
}
echo strip_tags($row->content);
以上就介紹了php程式碼 依上下層級關係輸出內容的PHP程式碼,包括了php程式碼方面的內容,希望對PHP教學有興趣的朋友有幫助。