美文网首页
php array_size自定义分页2022-01-13

php array_size自定义分页2022-01-13

作者: 阿然学编程 | 来源:发表于2022-01-12 09:31 被阅读0次
    /**
     * array_size自定义分页
     * @param $data
     * @param int $currentPage
     * @param int $perPage
     * @return array
     */
    function get_page($data, $currentPage = 1, $perPage = 10)
    {
        $totalItems = count($data);
        $currentPage = max(1, $currentPage);
        $offset = ($currentPage - 1) * $perPage;
        $totalPages = ceil($totalItems / $perPage);
        $pagedData = array_slice($data, $offset, $perPage);
        return [
            'totalItems' => $totalItems,
            'currentPage' => $currentPage,
            'perPage' => $perPage,
            'totalPages' => $totalPages,
            'data' => $pagedData
        ];
    }
    
    /**
     * 自定义分页利用array_slice函数对数组进行分页
     * @param $info  需要分页的数组
     * @param $page  当前页数
     * @param int $pageSize 每页条数
     * @return mixed
     */
    function doPage($info, $page = 1, $pageSize = 10)
    {
        $info = array_values((array)$info);
        $start = ($page - 1) * $pageSize;
        $data['total'] = count($info);
        $data['current_page'] = $page;
        $data['per_page'] = $pageSize;
        $data['last_page'] = ceil($data['total'] / $pageSize);
        $data['data'] = array_slice($info, $start, $pageSize);
        return $data;
    }
    

    相关文章

      网友评论

          本文标题:php array_size自定义分页2022-01-13

          本文链接:https://www.haomeiwen.com/subject/racncrtx.html