如何将单个php文件中的1个以上json_encoded数组传递给angular js

时间:2017-11-28 09:26:47

标签: php angularjs

嗨朋友们,我已经学会了学习角js 我的问题是当我登录时,我的ajax reuest从lv_inbox_angular.php转到lv_inbox_angular.html。 在php文件中有3个表leave historyleave approvalovertime history 所以我需要对这3 array($lvhistory , $apprleave ,$othistory )进行编码,我需要在lv_inbox_angular.html中显示它(当我同时登录时)

我的问题是,当我尝试使用超过1 print_r(json_encode($lvhistory))print_r(json_encode($othistory))时,我的加班历史为空(因为我没有应用加班假),但它给出了[]同 控制台中的error undefined [ 当我在网络标签中看到我会得到这个

[{"st_date":"2017-11-08","end_date":"2017-11-09","co_date":"0000-00-00","co_date2":null,"co_type":null,"co_desc":"NA","lv_type":"EL","status":"submitted","rpt_mgr":"aaa","lv_trans_id":"271"},{"st_date":"2017-11-20","end_date":"2017-11-20","co_date":"0000-00-00","co_date2":null,"co_type":null,"co_desc":"NA","lv_type":"EL","status":"submitted","rpt_mgr":"aaa","lv_trans_id":"272"}][]

我的代码是lv_inbox_angular.html

<html lang="en">
   <head>
      <meta charset="utf-8"/>
      <meta name="robots" content="noindex"/>
      <title>angulrjs login page</title>
      <meta name="viewport" content="width=device-width, initial-scale=1"/>
      <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"/>
      <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet" id="main-css"/>
      <!--<link href="css/style.css" rel="stylesheet" id="main-css"/>-->
      <script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
      <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>  
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
      <style>
      .invalid {
      color:red;
      }

      </style>
   </head>
   <body ng-app="leave_inbox" ng-controller="inbox_controller">
      <div class="container">
      <button type="button" class="btn btn-success" >Apply Leave</button>
      <span>{{msg}}</span>
      <p><b>{{display}}</b></p>
      <table border = 1>
      <tr>
      <th>Startdate</th><th>EndDate</th><th>LeaveType</th><th>Reporting Manager</th><th>Status</th></tr>
      <tbody ng-repeat="i in records"> <!--loops within json result-->

          <tr>
          <td>{{i.st_date}}</td>
          <td>{{i.end_date}}</td>

          <td>{{i.lv_type}}</td>
          <td>{{i.rpt_mgr}}</td>
          <td>{{i.status}}</td>
       </tr>
    </tbody>
    </table>



      </div>
      <div ng-controller = 'othistory_controller'>
        <h3>{{name}}</h3>
      </div>

   </body>
</html>


   <!--Angular begins-->
   <script type = 'text/javascript'>
   var app = angular.module('leave_inbox', [])  
    .controller('inbox_controller', ['$scope', '$http', function($scope, $http) {        

            $http({
                    method: 'GET',
                    url: 'lv_inbox_angular.php?lvtype=lvhistory',

                    headers: {'Content-Type': 'application/json'}
                })


                .success(function(data) {
                alert(data);
                console.log(data);
                $scope.display = 'LEAVE HISTORY';   
                $scope.records = data;  


                  var session = JSON.parse(localStorage.getItem('session')); 
                   console.log(typeof(session));
                   var name = session.name;
                   console.log(name);

                   $scope.msg = 'Welcome  ' + name ;




                }) 


    enter code here
                       })







    $scope.msg = 'clicked';
  }])  //End of controller //











           .controller('othistory_controller', ['$scope', '$http', function($scope, $http) {


             $scope.name = 'othistory';


               $http({
                    method: 'GET',

                    url: 'lv_inbox_angular.php?lvtype=othistory',                       //getting the table othistory from lv_inbox_angular.php//
                    //data: { 'request_status' : 'overtime' },


                    headers: {'Content-Type': 'application/json'}
                })

                .success(function(data) {



                alert(data);
                console.log(data);
                });






             }]);

lv_inbox_angular.php

<?php 


    if(!isset($_SESSION)) 
   { 
        session_start(); 

   } 


    require_once("dataAccessController.php");//this is for database
    require_once("Controller.php");



    if ($_SESSION['roleNo']!=7)
    {

      $_SESSION['lvbal'];

    } 


    if (($_SESSION['roleNo']==5)||($_SESSION['roleNo']==7)||($_SESSION['roleNo']==8))
    {



     } 


         if(isset($_GET['lvtype'] ) == 'lvhistory')
          {

            $lvhistory = $objController->gtlvdtls($_SESSION['uId']);

            print_r(json_encode($lvhistory));

         }  









    if(isset($_GET['lvtype']) == 'othistory')
    {


        $othistory = $objController->gtotdtls($_SESSION['uId']);  

        print_r(json_encode($othistory));



    }








    if (isset($_GET['lvtype']) == 'approve_leave') 
    {


       $apprleave = $objController->gtapprlv($_SESSION['uId']);







    }

    ?>

请帮助我解决这个问题

1 个答案:

答案 0 :(得分:0)

打印多个JSON对象时,它将不再是有效的响应。

您应该将所有PHP响应合并,并将print_r作为单个JSON对象合并到文件末尾,然后让AngularJS将它们拆分。

PHP:

// Top of your file
// Create a global variable
$arr = [];

// In your first function
// Add data to global array
$arr['first']  = $lvhistory;

// In your second function
// Use same logic as above
$arr['second'] = $othistory;

// This can be repeated for every function

// End of file
// Convert your array and print object
// Notice the second param: true
print_r(json_encode($arr, true));

角:

$http({
    // Your request
}).success(function(data) {

    // Now you can split it in your Angular app
    var lvhistory = data.data.first;
    var othistory = data.data.second;
});