// 准备分类原始数据
$cate_arr = [
    ['id'=>1,'name'=>'电脑','pid'=>0],
    ['id'=>2,'name'=>'手机','pid'=>0],
    ['id'=>3,'name'=>'戴尔','pid'=>1],
    ['id'=>4,'name'=>'联想','pid'=>1],
    ['id'=>5,'name'=>'苹果','pid'=>2],
    ['id'=>6,'name'=>'华为','pid'=>2],
    ['id'=>7,'name'=>'戴尔XPS','pid'=>2],
    ['id'=>8,'name'=>'苹果12','pid'=>2],
    ['id'=>9,'name'=>'苹果13','pid'=>2],
    ['id'=>10,'name'=>'苹果142','pid'=>1],
    ['id'=>11,'name'=>'苹果15','pid'=>1],
    ['id'=>12,'name'=>'苹果16','pid'=>1],
    ['id'=>13,'name'=>'苹果17','pid'=>1],
    ['id'=>14,'name'=>'苹果18','pid'=>2],
    ['id'=>15,'name'=>'苹果19','pid'=>2]
];

function getTree($items, $pid = 0, $pidField = 'pid', $children = 'children')
{
    $tree = [];
    foreach ($items as $key => $item) {
        if ($item[$pidField] == $pid) {
            $child = getTree($items, $item['id'], $pidField);
            if (count($child)) {
                $item[$children] =  $child;
            }
            $tree[] = $item;
        }
    }

    return $tree;
}


// 递归处理无限级分类
function getTrees ($cate_arr = [] , $pid = 0 , $level = 0)
{
    static $cate_tree_arr = [];
    foreach ($cate_arr as $key=>$value) {
        if ($value['pid'] == $pid) {
            $value['level'] = $level;
            $cate_tree_arr[] = $value;
            getTrees($cate_arr,$value['id'],$level+1);
        }
    }
    return $cate_tree_arr;
}

// 显示无限级分类菜单
$cate_tree_arr = getTree($cate_arr);
/*foreach ($cate_tree_arr as $key=>$value) {
    echo str_repeat("|----",$value['level']).$value['name'].'<br>';
}*/




/*function getCommlist($arr,$videoid,$parent_id = 0,&$result = array()){
    //$arr =  Db::name('video_comment')->where("parent_id = '".$parent_id."'")->where('videoid',$videoid)->where('status',2)->order("createtime desc")->select();
    if(empty($arr)){
        return array();
    }
    foreach ($arr as $cm) {
        $thisArr=&$result[];
       // $cm["createtime"] = $cm['createtime'] ? date("Y-m-d H:i:s",$cm['createtime']) : '--';
        $cm["children"] = getCommlist($arr,$videoid,$cm["pid"],$thisArr);
        $thisArr = $cm;
    }
    return $result;
}*/




 $page = !empty($_GET['cpage']) ? intval($_GET['cpage']) : 1;//默认第一页
$pagedata= arrayPage($cate_tree_arr, 1, '', $page, 'cpage');
var_dump($pagedata);
/*
* 数组分页
* $arr         php 数组
* $rowsPerPage 每页显示的数量
* $url         页面地址
* $curPage     当前页
* return       $ret 结果数组,直接放在前端循环输出; $pageNumString 处理的分页,直接放在前端使用
*/
function arrayPage($arr, $rowsPerPage, $url, $curPage = 1, $param = 'curpage'){
// 数组总数
    $totalPage = count($arr);
// 首页
    $begin     = ($curPage - 1) * $rowsPerPage;
// 每页显示的记录
    $ret       = array_slice($arr, $begin, $rowsPerPage);
// 总页数
    $totalPage = ceil($totalPage / $rowsPerPage);
// 存储页面字符串
    $pageNumString = '';

    if ($curPage <= 3) {
        $begin = 1;
        $end = $totalPage >= 5 ? 5 : $totalPage;
    } else {
        $end = $curPage + 3 > $totalPage ? $totalPage : $curPage + 3;
        $begin = $end - 4 <= 1 ? 1 : $end - 4;
    }
// 上一页
    $prev = $curPage - 1 <= 1 ? 1: $curPage - 1;
    $pageNumString .= "<li><a href='$url?$param=1'>首页</a></li>";
    $pageNumString .= "<li><a href='$url?$param=$prev'>&laquo;</a></li>";
//根据起始页与终止页将当前页面的页码显示出来
    for ($i = $begin; $i <= $end;$i ++) {
//使用if实现高亮显示当前点击的页码
//这是 bootstrap的全局样式
        if ($curPage == $i) {
            $pageNumString .= "<li class='active'><a href='$url?$param=$i'>$i</a></li>";
        } else {
            $pageNumString .= "<li><a href='$url?$param=$i'>$i</a></li>";
        }
    }

//实现下一页
    $next = $curPage + 1 >= $totalPage ? $totalPage : $curPage + 1;
    $pageNumString .= "<li><a href='$url?$param=$next'>&raquo;</a></li>";
    $pageNumString .= "<li><a href='$url?$param=$totalPage'>尾页</a></li>";

    return ['ret' => $ret, 'pageNumString' => $pageNumString];
}

插入代码结束参考网站:
PHP 无限级分类方法 :https://www.cnblogs.com/ibos/p/14516060.html
php运用无限级分类实现评论及回复功能 :https://blog.51cto.com/u_13940603/5886193
PHP 实现无限极分类生成分类树的方法:http://blog.liuguofeng.com/p/3758
PHP 无限级分类:https://blog.csdn.net/qq_32450471/article/details/127006522
php数组分页:https://blog.csdn.net/sktmarin/article/details/120237554
PHP添加评论+回复:https://blog.csdn.net/m0_67401417/article/details/123628567

发表评论 已发布 0

还可以输入 800 个字
 
 
打印