搜索
首页php教程PHP源码用于生成一个表格树

用于生成一个表格树

May 25, 2016 pm 05:08 PM
表格树

生成的例子如下: 

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

<?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(&#39;id&#39;=>&#39;1&#39;,&#39;parentid&#39;=>0,&#39;name&#39;=>&#39;一级栏目一&#39;),
     *      2 => array(&#39;id&#39;=>&#39;2&#39;,&#39;parentid&#39;=>0,&#39;name&#39;=>&#39;一级栏目二&#39;),
     *      3 => array(&#39;id&#39;=>&#39;3&#39;,&#39;parentid&#39;=>1,&#39;name&#39;=>&#39;二级栏目一&#39;),
     *      4 => array(&#39;id&#39;=>&#39;4&#39;,&#39;parentid&#39;=>1,&#39;name&#39;=>&#39;二级栏目二&#39;),
     *      5 => array(&#39;id&#39;=>&#39;5&#39;,&#39;parentid&#39;=>2,&#39;name&#39;=>&#39;二级栏目三&#39;),
     *      6 => array(&#39;id&#39;=>&#39;6&#39;,&#39;parentid&#39;=>3,&#39;name&#39;=>&#39;三级栏目一&#39;),
     *      7 => array(&#39;id&#39;=>&#39;7&#39;,&#39;parentid&#39;=>3,&#39;name&#39;=>&#39;三级栏目二&#39;)
     *      )
     */
    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[&#39;arrparentids&#39;]) {
                $v[&#39;depth&#39;] = count(explode(&#39;,&#39;, $v[&#39;arrparentids&#39;])) + 1;
                if ($v[&#39;depth&#39;] > $depth) {
                    $depth = $v[&#39;depth&#39;];
                }
            } else {
                $v[&#39;depth&#39;] = 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[&#39;arrchildids&#39;]) {
                $arrchildids = explode(&#39;,&#39;, $_arr[&#39;arrchildids&#39;]);
                foreach ($arrchildids as $v) {
                    if (!$this->_arr[$v][&#39;childids&#39;]) {
                        $depth[$this->_arr[$v][&#39;depth&#39;]][$v] = $v;
                    }
                }
            } else {
                $depth[$_arr[&#39;depth&#39;]][$id] = $id;
            }
            ksort($depth);
            foreach ($depth as $k => $v) {
                $count = count($v);
                $num += $count;
            }
            $this->_arr[$id][&#39;max&#39;] = $num;
        }
    }
 
    /**
     * 根据ID获取所有的父级
     *
     * @param type $id
     * @return type
     */
    public function get_parent($id)
    {
        $arrparents = array();
        $parentid = $this->_arr[$id][&#39;parentid&#39;];
        while ($parentid) {
            $arrparents[$parentid] = $this->_arr[$parentid];
            $parentid = $this->_arr[$parentid][&#39;parentid&#39;];
        }
        return $arrparents;
    }
 
    /**
     * 获取所有的父级
     *
     * @return type
     */
    public function get_allParent()
    {
        $arrparents = array();
        foreach ($this->_arr as $k => $v) {
            $parentid = $v[&#39;parentid&#39;];
            // 循环出所有的父级
            if ($parentid) {
                while ($parentid) {
                    $arrparents[$k][$parentid] = $v;
                    $parentid = $this->_arr[$parentid][&#39;parentid&#39;];
                }
            }
        }
        foreach ($arrparents as $k => $v) {
            ksort($v);
            $this->_arr[$k][&#39;arrparentids&#39;] = implode(&#39;,&#39;, 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[&#39;parentid&#39;] == $id) {
                    $childs[$k] = $v;
                }
            }
        }
        return $childs;
    }
 
    /**
     * 获取所有的子集
     *
     * @return type
     */
    public function get_allChilds()
    {
        $childs = array();
        foreach ($this->_arr as $k => $v) {
            if ($v[&#39;parentid&#39;] === 0) {
                $childs[$k][$k] = $v;
            } else {
                // 循环判断父级所存在的数组,并把id加入到父级存在的数组中
                if ($childs) {
                    foreach ($childs as $ck => $cv) {
                        if (array_key_exists($v[&#39;parentid&#39;], $cv)) {
                            $childs[$ck][$k] = $v;
                        }
                    }
                }
                $childs[$v[&#39;parentid&#39;]][$k] = $v;
            }
        }
        foreach ($childs as $k => $v) {
            ksort($v);
            // 取出本身id
            unset($v[$k]);
            if ($v) {
                $this->_arr[$k][&#39;arrchildids&#39;] = implode(&#39;,&#39;, array_keys($v));
            }
        }
        // 获取二级id
        foreach ($this->_arr as $k => $v) {
            if ($childids = array_keys($this->get_childs($k))) {
                $this->_arr[$k][&#39;childids&#39;] = implode(&#39;,&#39;, $childids);
            }
        }
    }
 
    public function get_tableTree()
    {
        $_arr = array();
        $str = &#39;&#39;;
        $_save_childs = array();
        // 整理出对应的行数
        foreach ($this->_arr as $k => $v) {
            $_arr[$v[&#39;depth&#39;]][$k] = $v;
        }
        ksort($_arr);
        // 循环行数
        for ($i = 1; $i <= $this->_rows; $i++) {
            $str .= &#39;<tr>&#39;;
            // 对于第二行数据的排序显示,避免数据错乱
            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(&#39;,&#39;, $value[&#39;arrchildids&#39;]);
                            $str .= "<td width=&#39;100px&#39; colspan=&#39;{$value[&#39;max&#39;]}&#39;>{$value[&#39;name&#39;]}</td>";
                        }
                    } else {
                        // 计算需要合并的行数
                        $rows = $this->_rows - $i + 1;
                        $str .= "<td width=&#39;100px&#39; rowspan=&#39;{$rows}&#39;></td>";
                    }
                }
            } else {
                foreach ($_arr[$i] as $k => $v) {
                    if ($v[&#39;arrchildids&#39;]) {
                        $_save_childs[$k] = explode(&#39;,&#39;, $v[&#39;arrchildids&#39;]);
                    } else {
                        // 计算需要合并的行数
                        $rows = $this->_rows - $i + 1;
                    }
                    $str .= "<td width=&#39;100px&#39; colspan=&#39;{$v[&#39;max&#39;]}&#39; rowspan=&#39;{$rows}&#39;;>{$v[&#39;name&#39;]}</td>";
                }
            }
            $str .= &#39;</tr>&#39;;
        }
        return $str;
    }
 
}
 
$treearr = array(
    1 => array(&#39;id&#39; => &#39;1&#39;, &#39;parentid&#39; => 0, &#39;name&#39; => &#39;1&#39;),
    2 => array(&#39;id&#39; => &#39;2&#39;, &#39;parentid&#39; => 0, &#39;name&#39; => &#39;2&#39;),
    3 => array(&#39;id&#39; => &#39;3&#39;, &#39;parentid&#39; => 1, &#39;name&#39; => &#39;3&#39;),
    4 => array(&#39;id&#39; => &#39;4&#39;, &#39;parentid&#39; => 1, &#39;name&#39; => &#39;4&#39;),
    5 => array(&#39;id&#39; => &#39;5&#39;, &#39;parentid&#39; => 1, &#39;name&#39; => &#39;5&#39;),
    6 => array(&#39;id&#39; => &#39;6&#39;, &#39;parentid&#39; => 0, &#39;name&#39; => &#39;6&#39;),
);
$treeTable = new TreeTableCross();
$treeTable->init($treearr);
echo $treeTable->get_tableTree();
?>

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

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中