将我的JSON结果分页

时间:2014-03-31 09:39:03

标签: php json api pagination

在使用我的API时遇到一些问题。这是我当前的代码,它不起作用(根本没有显示任何分页):

<?php
//functions relating to wordpress go here:
//----------------------------------------
$bg_colors = array('green', 'orange', 'blue', 'yellow', 'red', 'black');
//----------------------------------------
//End functions relating to wordpress

// Start PetRescue PHP/API Code
//----------------------------------------
// Open CuRL/JSON Stuff

$ch = curl_init();
$category=$_GET['category'];
$url="http://www.myapisite.com.au/api/listings?token=f716909f5d644fe3702be5c7895aa34e&group_id=10046&species=".$category;
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'X-some-API-Key: f716909f5d644fe3702be5c7895aa34e',
));
$json = json_decode(curl_exec($ch), true);

// Pagination
foreach($json['total_pages'] as $total_pages){
$pagenow=$total_pages['page'];
if($pagenow) 
        $start = ($page - 1) * $limit;
    else
        $start = 0;

if ($pagenow == 0) $page = 1;
    $prev = $pagenow - 1;
    $next = $pagenow + 1;
    $lastpage = ceil($total_pages/$limit);
    $lpm1 = $lastpage - 1;
    $pagination = "";

if($lastpage > 1)
    {   
        $pagination .= "<div class=\"ac-pages-wrapper\">";
        if ($pagenow > 1) 
            $pagination.= "<a href=\"$url?page=$prev\">&lt; Previous</a>";
        else
            $pagination.= "<span class=\"first\">&lt; Previous</span>"; 

        if ($lastpage < 7 + ($adjacents * 2))
        {   
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $pagenow)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= "<a href=\"$url?page=$counter\">$counter</a>";                    
            }
        }
        elseif($lastpage > 5 + ($adjacents * 2))
        {
            if($pagenow < 1 + ($adjacents * 2))     
            {
                for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                {
                    if ($counter == $pagenow)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$url?page=$counter\">$counter</a>";                    
                }
                $pagination.= "...";
                $pagination.= "<a href=\"$url?page=$lpm1\">$lpm1</a>";
                $pagination.= "<a href=\"$url?page=$lastpage\">$lastpage</a>";      
            }

            elseif($lastpage - ($adjacents * 2) > $pagenow && $pagenow > ($adjacents * 2))
            {
                $pagination.= "<a href=\"$url?page=1\">1</a>";
                $pagination.= "<a href=\"$url?page=2\">2</a>";
                $pagination.= "...";
                for ($counter = $pagenow - $adjacents; $counter <= $pagenow + $adjacents; $counter++)
                {
                    if ($counter == $pagenow)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
                }

                $pagination.= "...";
                $pagination.= "<a href=\"$url?page=$lpm1\">$lpm1</a>";
                $pagination.= "<a href=\"$url?page=$lastpage\">$lastpage</a>";      
            }

            else
            {
                $pagination.= "<a href=\"$url?page=1\">1</a>";
                $pagination.= "<a href=\"$url?page=2\">2</a>";
                $pagination.= "...";
                for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                {
                    if ($counter == $pagenow)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$url?page=$counter\">$counter</a>";                    
                }
            }
        }

        //next button
        if ($pagenow < $counter - 1) 
            $pagination.= "<a href=\"$url?page=$next\">next �</a>";
        else
            $pagination.= "<span class=\"current\">next �</span>";
        $pagination.= "</div>\n";       
    }


// Functions relating to the Echo Code

foreach($json['listings'] as $listing)
{
$short_personality=substr($listing['personality'],0,500);
$peturl="http://myurl/beta/pet-info/?petid=".$listing['id'];
$medium_photo=$listing['photos'][0]['large_340'];
$gallery_listing=$listing['photos'][5]['large_900'];
$gender_class=strtolower($listing['gender']);
$breed_class=strtolower($listing['species']);
$name=($listing['name']);
$unique_gallery_name="lightbox['.$inc.']";
    $inc++;
foreach($listing["photos"] as $photosthumb)
          {
            $photo_thumb_large=$photosthumb["large_340"];
            $photo_thumb_hidden=$photosthumb["small_80"];
          }
$rand_background = $bg_colors[array_rand($bg_colors)];

// General IF/AND/ELSE Statements to refine the Echo Output
if($listing['photos'] == null) {
    $medium_photo="http://myurl/beta/wp-content/themes/Archive/images/photo_coming_soon.png";
}
if($listing['desexed'] == "Yes") {
    $desexed="yes";
}
else {
    $desexed="no";
}
if($listing['vaccinated'] == "Yes") {
    $vaccinated="yes";
}
else {
    $vaccinated="no";
}
if($listing['wormed'] == "Yes") {
    $wormed="yes";
}
elseif($listing['wormed'] == "No") {
    $wormed="no";
}
else {
    $wormed="no";
}
if($listing['heart_worm_treated'] == "Yes") {
    $heart_worm_tested="yes";
}
elseif($listing['heart_worm_treated'] == "No") {
    $heart_worm_tested="no";
}
else {
    $heart_worm_tested="no";
}
if($listing['species'] == "Dog") {
    $adoption_enquiry_link="http://myurl/beta/pre-adoption-form-dogs/?dog_name=$name";
    $hwt="list-$heart_worm_tested";
}
elseif($listing['species'] == "Cat") {
    $adoption_enquiry_link="http://myurl/beta/pre-adoption-form-cats/?cat_name=$name";
    $hwt="list-hwt-hidden";
}

// Echo the output

echo'<div class="animal">
        <div class="animal-image">
<a class="size-thumbnail thickbox" rel="'.$unique_gallery_name.'" href="'.$medium_photo.'">
                <img src="'.$medium_photo.'" class="image-with-border" alt="">
                <div class="border" style="width: 340px; height: 340px;">
                    <div class="open"></div>
                </div>
            </a>
        <div class="item-title-bg '.$rand_background.'">
                <h2 class="entry-title">'.$listing['name'].'</h2>
                <div class="animal-adopt-button">
    <a href="'.$adoption_enquiry_link.'" style="background-color: #575757; border-color: #494949; background-position:5px 0;" class="button medium">Enquire about '.$name.'</a>
                </div>      
            </div>      
        </div>      
        <div class="animal-thumbnail hidden">
<a class="lightbox" rel="'.$unique_gallery_name.'" href="'.$photo_thumb_large.'">
                    <img class="animal-thumbnail" src="'.$photo_thumb_hidden.'" >
                    </a>
        </div>      
<div class="animal-content">
    <div class="animal-left">
        <ul class="animal-list">
            <li class="list-sex-'.$gender_class.'">'.$listing['gender'].'</li>
            <li class="list-breed-'.$breed_class.'">'.$listing['breeds_display'].'</li>
            <li class="list-age">'.$listing['age'].'</li>
            <li class="list-fee">'.$listing['adoption_fee'].'</li>
        </ul>
    </div>
<div class="animal-right">
        <ul class="animal-list">
            <li class="list-'.$desexed.'">Desexed?</li>
            <li class="list-'.$vaccinated.'">Vaccinated?</li>
            <li class="list-'.$wormed.'">Wormed?</li>
            <li class="'.$hwt.'">Heart Worm Tested?</li>
        </ul>
</div>
<div class="animal-full">
        <ul class="animal-list">
            <li class="list-description">'.$listing['personality'].'</li>
        </ul>
</div></div>
<div class="clearer"></div>
</div>
<div class="delimiter"></div>
<div class="ac-pages-wrapper">'.$pagination.'</div>';
// Close the CURL
}

}
curl_close($ch);
?>

对此的任何帮助将不胜感激。

我已经尝试了多种变体,谷歌搜索了一下,但其中大部分与MySQL有关。

我正在使用API​​来获取数据而不是数据库,“page”和“total_pages”是JSON响应的一部分,所以我有点不确定如何解决这个问题。

干杯,

戴夫

1 个答案:

答案 0 :(得分:0)

12/4/2012答案的第二个版本。

有一个网站,您可以看到分页和显示代码正常工作:

它允许您:

1) Change the number of items per page
2) Change the number of 'slots' in the page selector.

http://rfv123.eu.pn/pets/

我已完成'分页代码'。

您现在可以调整“页面选择器”代码中显示的“插槽”数量。

我对目前的“分页”代码感到满意。

“宠物”显示代码已转换为使用“Savant3”模板,该模板使用PHP作为“模板”语言。有用的部分是它可以轻松地将“显示”与“逻辑”代码分开。虽然仍然允许“视图”中提供PHP的所有功能。我更喜欢这个。

<强> download full code here (zip file)

我将为“分页”代码制作一个单独的存档,因为它实际上是“独立”代码。可以在任何地方使用。

分页代码由'常量'和提供的参数控制。格式化都是css。

当前网站使用您之前提供的“测试数据”。

任何人都可以免费使用“分页代码”。这是在任何地方重新发明的标准东西。包括我在内。它构造时使用类和“页面”数组,每个页面具有固定数量的项目。您可以设置“当前页码”,它将返回以下内容:

1)要在页面上显示的项目数组

2)“页面选择器”,其中包含用HTML修饰的可变数量的“插槽”。