如何使用PHP对JSON结果进行分页

时间:2017-02-13 23:41:04

标签: php json github pagination

我正在使用Github API来获取repositorie提交,但我想对结果进行分页。 数据是在JSON上,我想做这样的分页风格:

enter image description here

api resutls:http://api.github.com/repos/FenixHosting/docs.fenixcms.com/commits

编辑: 我有一个功能,但它不起作用:

function pagination($query, $per_page = 20,$page = 1, $url = '?', $custom){
        $git_docs = file_get_contents($query, false);
        $row = json_decode($git_docs, true);
        $total = count($row);
        $adjacents = "2"; 

        $page = ($page == 0 ? 1 : $page);  
        $start = ($page - 1) * $per_page;                               

        $prev = $page - 1;                          
        $next = $page + 1;
        $lastpage = ceil($total/$per_page);
        $lpm1 = $lastpage - 1;

        $pagination = "";
        if($lastpage > 1)
        {   
            $pagination .= "<ul class='pagination pagination-sm'>";
            if ($lastpage < 7 + ($adjacents * 2))
            {   
                for ($counter = 1; $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<li class='active'><a>$counter</a></li>";
                    else
                        $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";                    
                }
            }
            elseif($lastpage > 5 + ($adjacents * 2))
            {
                if($page < 1 + ($adjacents * 2))     
                {
                    for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                    {
                        if ($counter == $page)
                            $pagination.= "<li class='active'><a>$counter</a></li>";
                        else
                            $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";                    
                    }
                    $pagination.= "<li class='disabled'><span>...</span></li>";
                    $pagination.= "<li><a href='{$url}page=$lpm1'>$lpm1</a></li>";
                    $pagination.= "<li><a href='{$url}page=$lastpage'>$lastpage</a></li>";      
                }
                elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
                {
                    $pagination.= "<li><a href='{$url}page=1'>1</a></li>";
                    $pagination.= "<li><a href='{$url}page=2'>2</a></li>";
                    $pagination.= "<li class='disabled'><span>...</span></li>";
                    for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                    {
                        if ($counter == $page)
                            $pagination.= "<li class='active'><a>$counter</a></li>";
                        else
                            $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";                    
                    }
                    $pagination.= "<li class='disabled'><span>...</span></li>";
                    $pagination.= "<li><a href='{$url}page=$lpm1'>$lpm1</a></li>";
                    $pagination.= "<li><a href='{$url}page=$lastpage'>$lastpage</a></li>";      
                }
                else
                {
                    $pagination.= "<li><a href='{$url}page=1'>1</a></li>";
                    $pagination.= "<li><a href='{$url}page=2'>2</a></li>";
                    $pagination.= "<li class='disabled'><span>...</span></li>";
                    for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                    {
                        if ($counter == $page)
                            $pagination.= "<li class='active'><a>$counter</a></li>";
                        else
                            $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";                    
                    }
                }
            }

            if ($page < $counter - 1){ 
                $pagination.= "<li><a href='{$url}page=$next'>»</a></li>";
                $pagination.= "<li><a href='{$url}page=$lastpage'>Last</a></li>";
            }else{
                $pagination.= "<li class='disabled'><a>»</a></li>";
                $pagination.= "<li class='disabled'><a>Last</a></li>";
            }
            $pagination.= "</ul>\n";      
        }


        return $pagination;
    } 

1 个答案:

答案 0 :(得分:0)

使用其API的pagination功能 - https://developer.github.com/v3/#pagination

  

默认情况下,返回多个项目的请求将分页为30个项目。您可以使用?page参数指定更多页面。对于某些资源,您还可以使用?per_page参数将自定义页面大小设置为100。

例如 - https://api.github.com/repos/FenixHosting/docs.fenixcms.com/commits?page=1&per_page=3

要构建分页链接,请阅读:https://developer.github.com/guides/traversing-with-pagination/#constructing-pagination-links