0

I have a Library angularJS application and a JSON Data file that contains an array of book's information like this

[
{
    "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"
}
]

"current" is for the current page of the current book that i'm reading
and there is a "next" and "prev" buttons in the reading view when i press "next" it adds "+1" to the "current" num page the question is (How to send this +1 to the "current" in JSON file with PHP?) I have this PHP code :

<?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);
?>

See the ($data[0]) is for the index of the first book, how do i send to PHP the index of the current book so it updates the "current" data of the current book? Here is the "next" function :

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");
        });
    };

And here is the reading Controller :

 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);
}]);

I know it's complicated but i really need that , thanks.

1
  • Do you want implement pagination ? Commented Oct 23, 2015 at 8:10

1 Answer 1

1

How do you expect your application to behave?

You need to send the bookId and the pageNum with your request!

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");
        });
};

BTW. By REST design a GET http request should never change a resource. GET is for READING. If you want to update a resource you should use POST, PUT or DELETE

Sign up to request clarification or add additional context in comments.

2 Comments

Hey, thanks alot .. I want to know what was the last page i was reading so i could continue from there, so when i press "next" it moves to the next page and add +1 to the current page then send this +1 to the json file .. and i don't know much about PHP .. almost nothing .. sso.. how do i put the param in the PHP code ? do i use them like this $data[$scope.bookId]['current'] = $scope.pageNum; ?
Then you should learn about PHP. See php.net/manual/en/reserved.variables.get.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.