php-从数组分页数据

标签 php mysql json wordpress pagination

我正在制作一个 WordPress 插件来与我们的图书馆系统集成。我正在尝试创建一个带有搜索表单的页面来搜索数据库。我在我们的图书馆服务器(它在我们的本地网络上)上创建了一个 API,因此该站点可以与我们的图书馆后端连接。

我正在尝试对从 API 提取的结果进行分页,数据在 json 中。我创建了以下代码以从 API 获取 json 并将其转换为 PHP 数组,它还没有实现搜索:

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 做过这个,但我不知道如何用数组来做。

谢谢!

最佳答案

以下是您将用于分页的代码原型(prototype),只需更改它即可获得所需的结果。

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-从数组分页数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19595404/

相关文章:

mysql - 如何使用 json 将元组数组传递给 sql 语句

php - 魔术方法(__get、__set)在扩展类中不起作用?

mysql - 当在可为空列上外部连接子查询时,为什么 mysql 提示选择列表包含非聚合列?

php - 存储 PHP DateInterval 的 MySQL 数据类型

mysql - 连接期间字段列表中的未知列

mysql - 如何让最小和最大日期更小等于今天mysql

xml - 从 JSON 字符串中读取数组元素的 XPath

java - 以[。]开头或结尾的object字段使elasticsearch bulk index中的对象分辨率不明确

php - FFMPEG 不能在 php 代码中工作

php - 如何通过 javascript (jquery) 将 html 元素的内容设置为 php 包含文件