【问题标题】:Sending specific data to json file from angularJS app using PHP使用 PHP 从 angularJS 应用程序将特定数据发送到 json 文件
【发布时间】: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


【解决方案1】:

您希望您的应用程序表现如何?

您需要将bookIdpageNum 与您的请求一起发送!

scope.goNext = function() {
  if (scope.pageToDisplay >= pdfDoc.numPages) {
    return;
  }
  scope.pageNum = parseInt(scope.pageNum) + 1;
    $http.get("data/insert.php", {
            param : { 
                bookId: $scope.bookId,
                pageNum: $scope.pageNum
            }
        })
        .success(function(data, status, headers, config) {
            console.log("data inserted successfully");
        });
};

顺便说一句。通过 REST 设计,GET http 请求永远不应更改资源。 GET 用于阅读。如果你想更新一个资源,你应该使用 POST、PUT 或 DELETE

【讨论】:

  • 嘿,非常感谢......我想知道我正在阅读的最后一页是什么,所以我可以从那里继续,所以当我按“下一页”时,它会移动到下一页并添加 +1到当前页面然后将此 +1 发送到 json 文件.. 我对 PHP 了解不多.. 几乎一无所知.. sso.. 我如何将参数放在 PHP 代码中?我是否像这样使用它们 $data[$scope.bookId]['current'] = $scope.pageNum; ?
猜你喜欢
  • 2017-01-25
  • 2019-06-15
  • 2012-05-29
  • 2015-04-11
  • 1970-01-01
  • 2011-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多