Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I generated some Json data from Mysql Database with PHP as below:

equipments.php

<?php

    require("config.inc.php");

    //initial query
    $query = "Select * FROM equipment";

    //execute query
    try {
          $stmt   = $db->prepare($query);
          $result = $stmt->execute($query_params);
         }
    catch (PDOException $ex) {
          $response["success"] = 0;
          $response["message"] = "Database Error!";
          die(json_encode($response));
         }

     // Finally, we can retrieve all of the found rows into an array using fetchAll 
     $rows = $stmt->fetchAll();


     if ($rows) {
         $response["success"] = 1;
         $response["message"] = "Equipment Available!";
         $response["equipments"]   = array();

     foreach ($rows as $row) {
         $post             = array();
    $post["EquipmentId"]  = $row["EquipmentId"];
         $post["Name"] = $row["Name"];
         $post["Ip"]    = $row["Ip"];
         $post["Brand"]  = $row["Brand"];
         $post["Location"]  = $row["Location"];

         //update our repsonse JSON data
         array_push($response["equipments"], $post);
        }


         // echoing JSON response
         echo json_encode($response);

       } else {
                $response["success"] = 0;
                $response["message"] = "No Equipment Available!";
                die(json_encode($response));
           }

       ?>

this returned the following data:

//localhost/equip/equipments.php (on my local apache server)

{
 success: 1,
 message: "Equipment Available!",
 equipments: [
   {
    EquipmentId: "1",
    Name: "UCH-NET",
    Ip: "172.16.32.4",
    Brand: "Engenius",
    Location: "Top of ITD"
   },

   {
     EquipmentId: "2",
     Name: "UCH-PHOUSE",
     Ip: "172.16.32.5",
     Brand: "Mikrotik",
     Location: "Top of ITD"
   },

   {
     EquipmentId: "3",
     Name: "UCH-SON",
     Ip: "172.16.32.9",
     Brand: "MIkrotik",
     Location: "SON"
   },

  {
   EquipmentId: "4",
   Name: "UCH-GERIATRIC",
   Ip: "172.16.32.10",
   Brand: "Mikrotik",
   Location: "Geriatric"
  }
  ]
  }

But when i try to use the returned Json like this in my AngularJS application no data is returned in the web page

services.js

'use strict';

 var equipServices = angular.module('equipServices', ['ngResource']);

 equipServices.factory('Equip', ['$resource',
            function($resource){
            return $resource( '/equip/equipments.php/');

       }]);

equipment.js

    function EquipmentsCtrl ($scope, Equip) {
    $scope.setActive('equipments');

    $scope.sidebarURL = 'partials/equipment.html';
    $scope.currentEquipment = null;

        $scope.setEquipment = function (EquipmentId) {
        $scope.currentEquipment = $scope.equipments[EquipmentId];
    };

    $scope.equipments = Equip.query();

    }


}

index.html

    <title>IP Library</title>
        <script type="text/javascript" src="js/lib/angular.min.js"></script>
        <script type="text/javascript" src="js/lib/angular-resource.min.js"></script>
    <script type="text/javascript" src="js/controllers/app.js"></script>
    <script type="text/javascript" src="js/controllers/equipments.js"></script>
    <script type="text/javascript" src="js/controllers/admins.js"></script> 
    <script type="text/javascript" src="js/app.js"></script>    
    <script type="text/javascript" src="js/services.js"></script>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="css/bootstrap-responsive.min.css">
 </head>
 <body>
    <div class="container" ng-controller="AppCtrl" >
     <h1>IP Library</h1>
     <ul class="nav nav-pills">
        <li ng-class="equipmentsActive">
            <a href="#equipments" >Equipments</a>
         </li>
        <li ng-class="adminsActive">
           <a href="#/admins">Administrators</a>
       </li>
   </ul>

   <div ng-view></div>
 </div>

</body>
</html>

Is there something wrong with the way I am calling the php generated Json in my services.js? Thanks

share|improve this question
    
or is there a better way to generate json from database in Linux OS for use in AngularJS application, in Windows OS i would have used ASP.NET web api. –  Femzy Jan 30 at 20:52
add comment

1 Answer

Use Equip.get() instead of Equip.query() because you are getting from server an object, not an array:

Equip.get(function(result){
    if(result.success === 1) // isn't better to return boolean?
        $scope.equipments = result.equipments;
    // else handle error
});
share|improve this answer
    
Thanks your suggestion fixed it. Would appreciate if you could give me hint on the best way to save new equipment back to mysql server from the angularjs app. –  Femzy Jan 30 at 21:19
    
@Femzy If you have another question, please ask it by clicking the Ask Question button. –  Stewie Jan 30 at 23:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.