首页 >后端开发 >php教程 >PHP按上下级层次关系输出内容_PHP教程

PHP按上下级层次关系输出内容_PHP教程

WBOY
WBOY原创
2016-07-21 14:54:171348浏览

PHP按上下级层次关系输出内容

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 level; $n++)
$pre .= '----';

echo $pre . '|- ';
}
echo strip_tags($row->content);

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/364650.htmlTechArticlePHP按上下级层次关系输出内容 function getSubComments($parent = 0, $level = 0) { $db = JFactory::getDBO(); $sql = ...; // 查询记录的SQL $db-setQuery($sql); $rows = $...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn