0

I have written the following controller that gets info from my DB and displays in in my app:

Controller:

.controller('activityCtrl', function($scope, $http, $interval) {
    $http.get("user.php")
    .then(function (response) {$scope.names = response.data.records;});
})

user.php

<?php
//DB stuff goes here
$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "") {$outp .= ",";}
    $outp .= '{"Regid":"'  . $rs["Regid"] . '",';
    $outp .= '"Fname":"'   . $rs["fname"]        . '",';
    $outp .= '"Lname":"'   . $rs["lname"]        . '",';
    $outp .= '"activity":"'   . $rs["activity"]        . '",';
    $outp .= '"ID":"'. $rs["id"]     . '"}'; 
}
$outp ='{"records":['.$outp.']}';
$conn->close();
//echo($outp);
?>

Now Im trying to modify the controller so it runs getData function and updates the view at a predefined interval and Im having some trouble incorporating the necessary portions for the update with what I already have working. here is the controller as it has been modified:

.controller('activityCtrl', function($scope, $http, $timeout) {
$scope.getData = function(){
$http.get("user.php")
.success(function(data,status,headers,config){
//The next line needs to be modified to work with the new code
//.then(function (response) {$scope.names = response.data.records;});
console.log("data fetched");
        });
    };
$scope.intervalFunction = function(){
    $timeout(function(){
        $scope.getData();
        $scope.intervalFunction();
    }, 1000)
};
$scope.intervalFunction(); 
});

Currently I have the console.log running for testing and everything is functional. I have my older line of code commented out; this is the line I need to rewrite in order to display the data as needed. Any help is very much appreciated.

The interval is working already; I dont know how to rewrite the .success function so it displays the results from the $http.

DataGuy
  • 1,695
  • 4
  • 22
  • 38

4 Answers4

0

Sorry your question is pretty unclear to me, as in what you are trying to achieve.

If you want to run your code repeatedly after a certain time frame then you can just use the $interval service. Can you be more clear on what you are trying to achieve and where is the problem?

  • Ive edited the post- I need to update my one line of code so it works with the rest on the new code – DataGuy Feb 27 '16 at 17:28
0

Use $interval instead of $timeout

    $interval(function(){
        $scope.getData();
    }, 1000);

See More : setTimeout or setInterval?

Community
  • 1
  • 1
Karthik VU
  • 561
  • 5
  • 17
0

While technically not an answer what are you doing in user.php? I think you're trying to create a JSON string but I question your methods.

<?php
  while($row = $result->fetch_array(MYSQLI_ASSOC)) {
    $data[] = $row; // or just use a fetch_all?
  }

  echo json_encode($data);
?>
Xorifelse
  • 7,878
  • 1
  • 27
  • 38
0

I fixed the issue by using $scope and setInterval instead. Here is the new controller in case anyone needs a reference:

.controller('activityCtrl', function($scope, $http) {
    $scope.value = 0;
    setInterval(function(){
   $http.get("user.php")
   .then(function (response) {$scope.names = response.data.records;});
    }, 1000);
});
DataGuy
  • 1,695
  • 4
  • 22
  • 38