【发布时间】:2015-10-23 07:28:12
【问题描述】:
我有一个库 angularJS 应用程序和一个 JSON 数据文件,其中包含这样的书籍信息数组
[
{
"name" : "test1",
"pages": 0,
"author": "author1",
"year": 1940,
"section": "history",
"current": 0,
"description": "bla bla bla",
"bookUrl": "data/book.pdf"
},
{
"name" : "test1",
"pages": 0,
"author": "author1",
"year": 1940,
"section": "history",
"current": 0,
"description": "bla bla bla",
"bookUrl": "data/book.pdf"
}
]
“当前”是我正在阅读的当前书籍的当前页面
并且在阅读视图中有一个“下一个”和“上一个”按钮
当我按“下一步”时,它会将“+1”添加到“当前”页面
问题是 (如何使用 PHP 将此 +1 发送到 JSON 文件中的“当前”?)
我有这个 PHP 代码:
<?php
$jsonString = file_get_contents('library.json');
$data = json_decode($jsonString, true);
$data[0]['current'] = 3;
$newJsonString = json_encode($data);
file_put_contents('library.json', $newJsonString);
?>
看到 ($data[0]) 是第一本书的索引,我如何将当前书的索引发送到 PHP,以便它更新当前书的“当前”数据? 这是“下一个”功能:
scope.goNext = function() {
if (scope.pageToDisplay >= pdfDoc.numPages) {
return;
}
scope.pageNum = parseInt(scope.pageNum) + 1;
$http.get("data/insert.php")
.success(function(data, status, headers, config) {
console.log("data inserted successfully");
});
};
这里是阅读控制器:
app.controller('read', ['$scope','books', '$routeParams',function($scope,books, $routeParams) {
books.success(function(data){
$scope.book = data[$routeParams.bookId]
$scope.pdfUrl = data[$routeParams.bookId].bookUrl;
$scope.pdfName = data[$routeParams.bookId].name;
});
//the pdf viewer options
//$scope.pdfUrl = 'data/tohfa12.pdf';
$scope.scroll = 0;
$scope.loading = 'Loading file please wait';
$scope.getNavStyle = function(scroll) {
if(scroll > 100) {
return 'pdf-controls fixed';
} else {
return 'pdf-controls';
}
};
$scope.onError = function(error) {
console.log(error);
};
$scope.onLoad = function() {
$scope.loading = '';
};
$scope.onProgress = function(progress) {
console.log(progress);
};
$scope.currentBookIndex = parseInt($routeParams.bookId);
}]);
我知道这很复杂,但我真的需要它,谢谢。
【问题讨论】:
-
你要实现分页吗?
标签: javascript php json angularjs