【问题标题】:Paginating my JSON Results对我的 JSON 结果进行分页
【发布时间】:2014-04-12 15:05:08
【问题描述】:

在使用我的 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);
?>

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

我对此尝试了多种变体,并在 Google 上进行了相当多的搜索,但其中大部分都与 MySQL 有关。

我正在使用 API 而不是数据库来获取数据,并且“page”和“total_pages”是 JSON 响应的一部分,所以我有点不确定如何去做。

干杯,

戴夫

【问题讨论】:

  • 是否有可能获得您使用的“json”数组的副本?然后我就可以运行你的代码了!开玩笑的! :-) 然后我可以整理出“分页”代码。您需要“var_export”并将其放到网上,以便我可以使用其中一个“在线剪贴板”。我对“cUrl”的东西无能为力。
  • Ryan 给你,希望这是你想要的:wepaste.com/haart
  • 知道了,已将其转换为数组,它看起来像数组一样合理。我稍后会玩它。
  • 谢谢伙计。欣赏它。
  • 您能否发送您用于此目的的 css 文件。我已经有一个我不久前创建的类,用于处理“页面”选择器(开始 prev 3 4 5 next end)。我有代码工作,根据所选页面,每页显示如此多的“列表”。 (我对“Hector”感到厌烦!)我为当前页面计算了“页面选择器”数组。我目前正在查看“页面选择器”的 HTML 生成。

标签: php json api pagination


【解决方案1】:

2012 年 12 月 4 日答案的第二版。

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

它允许您:

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 装饰的“插槽”。

【讨论】:

  • 这看起来很棒。感谢您为此付出的努力。你想从这里做什么?继续在这里工作还是离线工作?为迟到的回复道歉,正在享受一个难得的周末假期! :)
猜你喜欢
  • 1970-01-01
  • 2017-07-02
  • 2018-04-03
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-20
相关资源
最近更新 更多