【发布时间】:2016-04-12 15:10:12
【问题描述】:
我正在使用 jquery 鸟瞰示例进行移动地图搜索。我使用以下 php 代码将 mysql 数据转换为 json
header('Content-Type: application/json');
include('dbhconfig.inc.php');
$response = array();
$stmt = $dbh->prepare("SELECT * FROM venddata");
$stmt->execute();
//$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as &$r) { // with reference
// explode with comma, map with floatval
$r['latlon'] = array_map('floatval', explode(',', $r['latlon']));
}
echo json_encode(array('results' => $rows)); // json encode
它显示了 json 中的所有记录。但我想每页只显示 10 条记录,并在 json 中显示总计数和页数。请看下面的例子。
http://sbadb.herokuapp.com/v1/bizs 它显示了 10 条记录,在页面底部我在代码下方找到了
"meta": {
"page": 1,
"per_page": 10,
"count": 493,
"total_pages": 50
}
我想展示类似于上面的例子。为 mysql 获取的数据是
http://www.zesteve.com/jsondata.php
我对 json 了解不多。需要帮助
以下代码使用的是js文件。
response_params_pagination: {
page: function(data) {
return data.meta.page;
},
per_page: function(data) {
return data.meta.per_page;
},
total_pages: function(data) {
return data.meta.total_pages;
},
count: function(data) {
return data.meta.count;
}
},
结论是我想将 mysql 数据分页到 json 中,它也应该在 json 中包含上述元内容。
【问题讨论】: