Home  >  Article  >  php教程  >  php父类与子类读取数据代码

php父类与子类读取数据代码

WBOY
WBOYOriginal
2016-05-25 16:42:421392browse

php父类与子类读取数据代码是一填二级分类的程序代码,该程序可以根据父的数据查找出其子类的数据,代码如下:

<?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 = &#39;&#39;;
    for ($n = 0; $n < $row->level; $n++) $pre.= &#39;----&#39;;
    echo $pre . &#39;|- &#39;;
}
echo strip_tags($row->content);


教程网址:

欢迎收藏∩_∩但请保留本文链接。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn