首页 > php教程 > PHP源码 > 正文

用于生成一个表格树

PHP中文网
发布: 2016-05-25 17:08:20
原创
1183人浏览过

生成的例子如下: 

好吧,不知道要怎么上传图片。。。 

<?php
 
/**
 * 树形展示
 * #-------------------------------#
 * #   a   | b | c | d | e | f | g #
 * #-------------------------------#
 * #  a-1  |   |   |   |   |   |   #
 * #-------|   |   |   |   |   |   #
 * #a-2|a-3|   |   |   |   |   |   #
 * #-------------------------------#
 * and open the template in the editor.
 *
 * PHP version 5.3.8
 *
 * @category TreeTable
 * @package  Tree
 * @author   然 <384750321@qq.com>
 *
 */
 
/**
 * 生成表格树
 *
 * PHP version 5.3.8
 *
 * @author 然 <384750321@qq.com>
 */
class TreeTableCross
{
 
    private $_arr, $_rows;
 
    /**
     * 初始化TreeTable数据
     * @param array 2维数组
     * array(
     *      1 => array('id'=>'1','parentid'=>0,'name'=>'一级栏目一'),
     *      2 => array('id'=>'2','parentid'=>0,'name'=>'一级栏目二'),
     *      3 => array('id'=>'3','parentid'=>1,'name'=>'二级栏目一'),
     *      4 => array('id'=>'4','parentid'=>1,'name'=>'二级栏目二'),
     *      5 => array('id'=>'5','parentid'=>2,'name'=>'二级栏目三'),
     *      6 => array('id'=>'6','parentid'=>3,'name'=>'三级栏目一'),
     *      7 => array('id'=>'7','parentid'=>3,'name'=>'三级栏目二')
     *      )
     */
    public function init($arr)
    {
        if (!is_array($arr)) {
            return false;
        }
        $this->_arr = $arr;
        $this->get_allChilds();
        $this->get_allParent();
        $this->get_depth();
        $this->get_max();
    }
 
    /**
     * 获取最深的树
     *
     */
    public function get_depth()
    {
        $depth = 0;
        foreach ($this->_arr as $k => $v) {
            if ($v['arrparentids']) {
                $v['depth'] = count(explode(',', $v['arrparentids'])) + 1;
                if ($v['depth'] > $depth) {
                    $depth = $v['depth'];
                }
            } else {
                $v['depth'] = 1;
            }
            $this->_arr[$k] = $v;
        }
        $this->_rows = $depth;
    }
 
    /**
     * 获取每个id的最广树
     */
    public function get_max()
    {
        foreach ($this->_arr as $id => $v) {
            $_arr = $this->_arr[$id];
            $depth = array();
            $num = 0;
            // 获取id的最低级的所有节点,根据行数排序
            if ($_arr['arrchildids']) {
                $arrchildids = explode(',', $_arr['arrchildids']);
                foreach ($arrchildids as $v) {
                    if (!$this->_arr[$v]['childids']) {
                        $depth[$this->_arr[$v]['depth']][$v] = $v;
                    }
                }
            } else {
                $depth[$_arr['depth']][$id] = $id;
            }
            ksort($depth);
            foreach ($depth as $k => $v) {
                $count = count($v);
                $num += $count;
            }
            $this->_arr[$id]['max'] = $num;
        }
    }
 
    /**
     * 根据ID获取所有的父级
     *
     * @param type $id
     * @return type
     */
    public function get_parent($id)
    {
        $arrparents = array();
        $parentid = $this->_arr[$id]['parentid'];
        while ($parentid) {
            $arrparents[$parentid] = $this->_arr[$parentid];
            $parentid = $this->_arr[$parentid]['parentid'];
        }
        return $arrparents;
    }
 
    /**
     * 获取所有的父级
     *
     * @return type
     */
    public function get_allParent()
    {
        $arrparents = array();
        foreach ($this->_arr as $k => $v) {
            $parentid = $v['parentid'];
            // 循环出所有的父级
            if ($parentid) {
                while ($parentid) {
                    $arrparents[$k][$parentid] = $v;
                    $parentid = $this->_arr[$parentid]['parentid'];
                }
            }
        }
        foreach ($arrparents as $k => $v) {
            ksort($v);
            $this->_arr[$k]['arrparentids'] = implode(',', array_keys($v));
        }
    }
 
    /**
     * 根据ID获取二级子级
     *
     * @param type $id
     * @return type
     */
    public function get_childs($id)
    {
        $childs = array();
        if (is_array($this->_arr)) {
            foreach ($this->_arr as $k => $v) {
                if ($v['parentid'] == $id) {
                    $childs[$k] = $v;
                }
            }
        }
        return $childs;
    }
 
    /**
     * 获取所有的子集
     *
     * @return type
     */
    public function get_allChilds()
    {
        $childs = array();
        foreach ($this->_arr as $k => $v) {
            if ($v['parentid'] === 0) {
                $childs[$k][$k] = $v;
            } else {
                // 循环判断父级所存在的数组,并把id加入到父级存在的数组中
                if ($childs) {
                    foreach ($childs as $ck => $cv) {
                        if (array_key_exists($v['parentid'], $cv)) {
                            $childs[$ck][$k] = $v;
                        }
                    }
                }
                $childs[$v['parentid']][$k] = $v;
            }
        }
        foreach ($childs as $k => $v) {
            ksort($v);
            // 取出本身id
            unset($v[$k]);
            if ($v) {
                $this->_arr[$k]['arrchildids'] = implode(',', array_keys($v));
            }
        }
        // 获取二级id
        foreach ($this->_arr as $k => $v) {
            if ($childids = array_keys($this->get_childs($k))) {
                $this->_arr[$k]['childids'] = implode(',', $childids);
            }
        }
    }
 
    public function get_tableTree()
    {
        $_arr = array();
        $str = '';
        $_save_childs = array();
        // 整理出对应的行数
        foreach ($this->_arr as $k => $v) {
            $_arr[$v['depth']][$k] = $v;
        }
        ksort($_arr);
        // 循环行数
        for ($i = 1; $i <= $this->_rows; $i++) {
            $str .= '<tr>';
            // 对于第二行数据的排序显示,避免数据错乱
            if ($_save_childs) {
                $_save_childs_back = $_save_childs;
                $_save_childs = array();
                // 对于上次循环数据的对应
                foreach ($_save_childs_back as $sk => $sv) {
                    $sv = array_intersect_key($_arr[$i], array_flip($sv));
                    if ($sv) {
                        foreach ($sv as $k => $v) {
                            $value = $v;
                            $_save_childs[$k] = explode(',', $value['arrchildids']);
                            $str .= "<td width='100px' colspan='{$value['max']}'>{$value['name']}</td>";
                        }
                    } else {
                        // 计算需要合并的行数
                        $rows = $this->_rows - $i + 1;
                        $str .= "<td width='100px' rowspan='{$rows}'></td>";
                    }
                }
            } else {
                foreach ($_arr[$i] as $k => $v) {
                    if ($v['arrchildids']) {
                        $_save_childs[$k] = explode(',', $v['arrchildids']);
                    } else {
                        // 计算需要合并的行数
                        $rows = $this->_rows - $i + 1;
                    }
                    $str .= "<td width='100px' colspan='{$v['max']}' rowspan='{$rows}';>{$v['name']}</td>";
                }
            }
            $str .= '</tr>';
        }
        return $str;
    }
 
}
 
$treearr = array(
    1 => array('id' => '1', 'parentid' => 0, 'name' => '1'),
    2 => array('id' => '2', 'parentid' => 0, 'name' => '2'),
    3 => array('id' => '3', 'parentid' => 1, 'name' => '3'),
    4 => array('id' => '4', 'parentid' => 1, 'name' => '4'),
    5 => array('id' => '5', 'parentid' => 1, 'name' => '5'),
    6 => array('id' => '6', 'parentid' => 0, 'name' => '6'),
);
$treeTable = new TreeTableCross();
$treeTable->init($treearr);
echo $treeTable->get_tableTree();
?>
登录后复制

以上就是用于生成一个表格树的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Notion AI
Notion AI

Notion是一款集成了笔记、知识库、数据表格、看板、日历等多种能力于一体的应用程序,它既可供个人使用,也可以与他人进行跨平台协作。

Notion AI 76
查看详情 Notion AI

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号