实现子菜单分类功能

原始数据:

INSERT INTO `cunwu`.`t_navi` (`id`, `type`, `name`, `pid`, `app_name`, `module_name`, `action_name`, `data`, `remark`, `url`, `often`, `ordid`, `display`) VALUES ('1', '0', '首页', '0', 'Home', 'Index', 'index', '', '', '', '0', '1', '1');
INSERT INTO `cunwu`.`t_navi` (`id`, `type`, `name`, `pid`, `app_name`, `module_name`, `action_name`, `data`, `remark`, `url`, `often`, `ordid`, `display`) VALUES ('2', '0', '村屋', '0', 'Home', 'Lists', 'index', 'id/1', '', '', '0', '1', '1');
INSERT INTO `cunwu`.`t_navi` (`id`, `type`, `name`, `pid`, `app_name`, `module_name`, `action_name`, `data`, `remark`, `url`, `often`, `ordid`, `display`) VALUES ('4', '0', '资讯', '2', 'Home', 'News', 'index', '', '', '', '0', '1', '1');
INSERT INTO `cunwu`.`t_navi` (`id`, `type`, `name`, `pid`, `app_name`, `module_name`, `action_name`, `data`, `remark`, `url`, `often`, `ordid`, `display`) VALUES ('5', '0', '土地', '0', 'Home', 'Lists', 'index', 'id/2', '', '', '0', '1', '1');

实现以下结果:

Array
(
    [0] => Array
        (
            [id] => 1
            [type] => 0
            [name] => 首页
            [pid] => 0
            [app_name] => Home
            [module_name] => Index
            [action_name] => index
            [data] => 
            [remark] => 
            [url] => 
            [often] => 0
            [ordid] => 1
            [display] => 1
        )

    [1] => Array
        (
            [id] => 2
            [type] => 0
            [name] => 村屋
            [pid] => 0
            [app_name] => Home
            [module_name] => Lists
            [action_name] => index
            [data] => id/1
            [remark] => 
            [url] => 
            [often] => 0
            [ordid] => 1
            [display] => 1
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [type] => 0
                            [name] => 资讯
                            [pid] => 2
                            [app_name] => Home
                            [module_name] => News
                            [action_name] => index
                            [data] => 
                            [remark] => 
                            [url] => 
                            [often] => 0
                            [ordid] => 1
                            [display] => 1
                        )

                )

        )

    [2] => Array
        (
            [id] => 5
            [type] => 0
            [name] => 土地
            [pid] => 0
            [app_name] => Home
            [module_name] => Lists
            [action_name] => index
            [data] => id/2
            [remark] => 
            [url] => 
            [often] => 0
            [ordid] => 1
            [display] => 1
        )

)

思路方法:
1、查询出当前的菜单数据
2、设计递归算法实现计算
3、得出具有二级数组的父子数据
4、嵌套到模板就可以实现

以下是递归算法:

#递归函数
    public  function recur($data,$pid=0){
           $list = [];
          if(is_array($data)){
            $total = count($data);
            foreach($data  as $k=>$v){
                if($v['pid']==$pid){
                  $v['children']=$this->recur($data,$v['id']);
                  if($v['children']==null){
                   unset($v['children']);
                  }
                  $list[] = $v;
                }
            }
          }
          return $list;
    }

以下是应用的示例代码:

$navi_data =M('navi')->where(array('status'=>1))->order('ordid desc')->select();
$navi_list=$this->recur($navi_data);
print_r($navi_list);

下面看实现的效果吧:

<?php
$text="阡陌建站\r\n广州专业建站团队 \r\n企业官网建设\r\n公众号开发\r\n小程序开发";
$d=str_replace("\r\n", "\n", $text);
echo $d;
?>

**这是输出结果:**
阡陌建站
广州专业建站团队 
企业官网建设
公众号开发
小程序开发

<?php
$text='阡陌建站\r\n广州专业建站团队 \r\n企业官网建设\r\n公众号开发\r\n小程序开发';
$d=str_replace("\r\n", "\n", $text);
echo $d;
?>
**这是输出结果:**
阡陌建站\r\n广州专业建站团队 \r\n企业官网建设\r\n公众号开发\r\n小程序开发

通过以上两个的验证:单引号无法对rn解析,双引号才支持nr的识别。

如果数据是赋值或从数据库查询处理的,要进行这个处理。(笨鱼验证过,从数据查询出来,经过多番验证和亲测,代码要做以下修改才能识别)。

$d=str_replace("\\r\\n", "\n", $text);

C:\Windows\System32\drivers\etc  hosts文件中添加以下代码
127.0.0.1   www.tongda2000.com
127.0.0.1   tongda2000.com
127.0.0.1     down.tongda2000.com
127.0.0.1   un.tongda2000.com
127.0.0.1   www.tongdayun.com
127.0.0.1   tongdayun.com
127.0.0.1    cloud.tongda2000.com
127.0.0.1   yun.tongda2000.com
127.0.0.1   www.tongda2000.net
127.0.0.1   tongda2000.net
127.0.0.1   www.tongda2000.org
127.0.0.1   tongda2000.org