php-来自数组的分页数据 [英] php- Paginate data from array

查看:116
本文介绍了php-来自数组的分页数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个WordPress插件以与我们的图书馆系统集成.我正在尝试创建一个带有搜索表单的页面来搜索数据库.我已经在我们的图书馆服务器(位于我们的本地网络上)上创建了一个API,以便该站点可以与我们的图书馆后端进行交互.

I am making a WordPress plugin to integrate with our library system. I am trying to create a page with a search form to search the database. I have created an API on our library server (it is on our local network) so the site can interface with our library back end.

我正在尝试对从API中提取的结果进行分页,数据位于json中.我创建了以下代码,以从API中获取json并将其转换为PHP数组,但尚未实现搜索:

I am trying to paginate results pulled from the API, the data is in json. I have created the following code to get the json from the API and turn it into a PHP array, it does not implement search, yet:

add_shortcode("book_search_form", "book_search_form");
function book_search_form() {

    $api_url = get_option('api_url');
    $api_key = get_option('api_key');
    $data = file_get_contents("$api_url/book/index.php/name/awesome/$api_key");

    $data = (array)json_decode($data, true);

echo '<pre>';
var_dump($data);
echo '</pre>';


} 

这是输出:

array(2) {
  [0]=>
  array(22) {
    ["barcode"]=>
    string(12) "000015427928"
    ["name"]=>
    string(74) "Janice VanCleave's 201 awesome, magical, bizarre & incredible experiments."
    ["author"]=>
    string(12) "Janice Pratt"
    ["author_last"]=>
    string(9) "VanCleave"
    ["publisher"]=>
    string(11) "Wiley: 1994"
    ["year_pub"]=>
    string(0) ""
    ["edition"]=>
    string(0) ""
    ["genre"]=>
    string(0) ""
    ["checkout"]=>
    string(5) "false"
    ["series"]=>
    string(0) ""
    ["callnum"]=>
    string(5) "507.8"
    ["fiction"]=>
    string(5) "false"
    ["in_house"]=>
    string(5) "false"
    ["timestamp"]=>
    string(10) "1374711656"
    ["outto"]=>
    string(0) ""
    ["duedate"]=>
    string(1) "0"
    ["ISBN"]=>
    string(10) "0585339376"
    ["media_type"]=>
    string(0) ""
    ["print"]=>
    string(5) "false"
    ["BOXID"]=>
    string(0) ""
    ["uid"]=>
    string(1) "0"
    ["printed"]=>
    string(4) "true"
  }
  [1]=>
  array(22) {
    ["barcode"]=>
    string(12) "000015429634"
    ["name"]=>
    string(50) "Our Awesome Earth: Its Mysteries and Its Splendors"
    ["author"]=>
    string(4) "Paul"
    ["author_last"]=>
    string(6) "Martin"
    ["publisher"]=>
    string(35) "Natl Geographic Society: March 1994"
    ["year_pub"]=>
    string(0) ""
    ["edition"]=>
    string(0) ""
    ["genre"]=>
    string(0) ""
    ["checkout"]=>
    string(5) "false"
    ["series"]=>
    string(0) ""
    ["callnum"]=>
    string(3) "050"
    ["fiction"]=>
    string(5) "false"
    ["in_house"]=>
    string(5) "false"
    ["timestamp"]=>
    string(10) "1382550052"
    ["outto"]=>
    string(0) ""
    ["duedate"]=>
    string(1) "0"
    ["ISBN"]=>
    string(10) "0870445456"
    ["media_type"]=>
    string(0) ""
    ["print"]=>
    string(5) "false"
    ["BOXID"]=>
    string(0) ""
    ["uid"]=>
    string(1) "0"
    ["printed"]=>
    string(5) "false"
  }
}

我正在尝试创建分页,因为某些查询包含50本书或更多书籍.我之前已经使用mysql完成了此操作,但无法弄清楚如何使用数组.

I am trying to create pagination, because some of the queries contain 50 or more books. I have done this before with mysql but I cannot figure out how to do it with an array.

谢谢!

推荐答案

以下是用于分页的代码原型,只需对其进行更改即可获得所需的结果.

Following is the prototype of the code you would use for pagination ,just alter it to get the desired results.

if(isset($_GET['page']) && isset($_GET['paramsfordataonnextpage']))
{
$page=$_GET['page'];   // get the value of the page from your url
$recordsPerPage=10; // number of records you want on your page
$array=//from your service
$index=($page*$recordsPerPage)-1;
$recordsToBeDisplayed = array_slice($array,$index,$recordsPerPage);// this array contains all the records you would want to display on a page;



    $total_pages=ceil(count($array)/$recordsPerPage);
    }
else { 

//use default values

}

<html>...<body><div id="records"><!-- use the array created above to display records -->
    </div>
<div id="pagination">
for($j=1;$j<=$total_pages;$j++){
                    if($j==$page)
                   {?>

                   <li style="display: inline;"><a href="/yourpaginationpage.php?paramsfordataonnextpage=&page=<?=$j?>"><u><?=$j?></u></a></li>
                   <?}else{?>
                        <li style="display: inline;"><a href="/yourpaginationpage.php?paramsfordataonnextpage=&page=<?=$j?>"><?=$j?></a></li>


                   <?}}?>
</div>
</body>
...
</html>

这篇关于php-来自数组的分页数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆