PHP / JS / JSON值传递错误

时间:2015-10-04 09:56:16

标签: javascript php json angularjs

我运行了一个PHP服务器,我正在使用ionic / Angular.js作为我的移动应用程序。该应用程序正在向服务器发送请求,服务器响应一些json。现在我有一个奇怪的错误。

这是我的JS代码:

RowLayout

服务器将返回以下内容:

  .controller('StatisticPlanDetailCtrl', function($scope, $stateParams, $http, SERVER) {
      planID = $stateParams.planID;
      time = $stateParams.time;

    $http({
      method: 'GET',
      url: SERVER.url + 'listener.php?request=getHistoryEntry&planID=' + planID + '&time=' + time
    }).then(function(data){

      $scope.historyExercises = angular.copy(data.data);

    });
  })

到目前为止一切正常。如您所见,没有必要设置historyValue。 但是这个故事中的奇怪之处在于:当我将断点设置为data.data赋给historyExercises的行时,数据对象只包含planValues而不是一个historyValue,尽管设置了一个“Gewicht”,你可以看到在JSON输出中。

这是Safari调试控制台的屏幕截图:

enter image description here

为什么planValue传递的历史值没有任何意义

编辑listener.php

[  
   {  
      "exerciseID":5,
      "exerciseName":"Beinpresse",
      "parameters":{  
         "Gewicht":{  
            "historyValue":"90 kg",
            "planValue":"80 kg"
         },
         "Wiederholungen":{  
            "historyValue":null,
            "planValue":"3 x 20 Whd"
         }
      }
   },
   {  
      "exerciseID":9,
      "exerciseName":"Awesome Oberarm \u00dcbung",
      "parameters":{  
         "Gewicht":{  
            "historyValue":null,
            "planValue":"20 kg"
         },
         "Wiederholungen":{  
            "historyValue":null,
            "planValue":"2 x 10 Whd"
         },
         "Hantel":{  
            "historyValue":null,
            "planValue":"Gro\u00df"
         },
         "Stufe":{  
            "historyValue":null,
            "planValue":"Hart"
         }
      }
   }
]

和service.php的一部分..:

    }else if($request == "getHistoryEntry"){
        $planID = $_GET['planID'];
        $time = $_GET['time'];
        echo $services->getHistoryEntry($planID, $time);
    }

循环前练习: 阵列

public function getHistoryEntry($planID, $time){
    $sql = "SELECT te.ExerciseID AS 'exerciseID',e.name AS 'exerciseName',  p.Name AS 'parameter', te.value AS 'planValue', the.value AS 'historyValue'
            FROM TrainingplanHistory th
            INNER JOIN TrainingplanExercises te ON(te.TrainingPlanID = th.TrainingPlanID)
            INNER JOIN Exercise e ON (e.ExerciseID = te.ExerciseID)
            INNER JOIN Parameter p oN(p.ParameterID = te.ParameterID)
            LEFT OUTER JOIN TrainingplanHistoryExercises the ON(the.TrainingplanID = th.TrainingPlanID AND the.Time = th.time AND
            the.ExerciseID = te.ExerciseID AND the.ParameterID = te.ParameterID)
            WHERE th.TrainingplanID = ? AND th.time = ?";

    $bindings = array($planID, $time);

    $exercises = $this->simplePDO->get_results($sql, $bindings);

    $newExercises = array();

    $lastExercise = array();
    for ($i = 0; $i < count($exercises); $i++) {
        $exercise = $exercises[$i];


        if($lastExercise['exerciseID'] == $exercise->exerciseID){
            $lastExercise['parameters'][$exercise->parameter]['historyValue'] =  $exercises->historyValue;
            $lastExercise['parameters'][$exercise->parameter]['planValue'] =  $exercises->planValue;



            if($i == (count($exercises)-1)){
                //last element has to be added
                array_push($newExercises, $lastExercise);
            }
        }else{
            //new exercise
            if($lastExercise != null)
                array_push($newExercises, $lastExercise);


            $lastExercise = array();

            $lastExercise ['exerciseID'] = $exercise->exerciseID;
            $lastExercise['exerciseName'] = $exercise->exerciseName;
            $lastExercise['parameters'] = array();
            $lastExercise['parameters'][$exercise->parameter]['historyValue'] =  $exercises->historyValue;
            $lastExercise['parameters'][$exercise->parameter]['planValue'] =  $exercise->planValue;


            if($i == (count($exercises)-1)){
                //last element has to be added
                array_push($newExercises, $lastExercise);
            }
        }
    }


    return json_encode($newExercises);

}

循环后练习:

(
    [0] => stdClass Object
        (
            [exerciseID] => 5
            [exerciseName] => Beinpresse
            [parameter] => Gewicht
            [planValue] => 80 kg
            [historyValue] => 90 kg
        )

    [1] => stdClass Object
        (
            [exerciseID] => 5
            [exerciseName] => Beinpresse
            [parameter] => Wiederholungen
            [planValue] => 3 x 20 Whd
            [historyValue] => 
        )

    [2] => stdClass Object
        (
            [exerciseID] => 9
            [exerciseName] => Awesome Oberarm Ãœbung
            [parameter] => Gewicht
            [planValue] => 20 kg
            [historyValue] => 
        )

    [3] => stdClass Object
        (
            [exerciseID] => 9
            [exerciseName] => Awesome Oberarm Ãœbung
            [parameter] => Wiederholungen
            [planValue] => 2 x 10 Whd
            [historyValue] => 
        )

    [4] => stdClass Object
        (
            [exerciseID] => 9
            [exerciseName] => Awesome Oberarm Ãœbung
            [parameter] => Hantel
            [planValue] => Groß
            [historyValue] => 
        )

    [5] => stdClass Object
        (
            [exerciseID] => 9
            [exerciseName] => Awesome Oberarm Ãœbung
            [parameter] => Stufe
            [planValue] => Hart
            [historyValue] => 
        )

)

这是视图中的最终输出:

enter image description here

这是代码:

Array
(
    [0] => Array
        (
            [exerciseID] => 5
            [exerciseName] => Beinpresse
            [parameters] => Array
                (
                    [Gewicht] => Array
                        (
                            [planValue] => 80 kg
                            [historyValue] => 90 kg
                        )

                    [Wiederholungen] => Array
                        (
                            [planValue] => 3 x 20 Whd
                            [historyValue] => 
                        )

                )

        )

    [1] => Array
        (
            [exerciseID] => 9
            [exerciseName] => Awesome Oberarm Ãœbung
            [parameters] => Array
                (
                    [Gewicht] => Array
                        (
                            [planValue] => 20 kg
                            [historyValue] => 
                        )

                    [Wiederholungen] => Array
                        (
                            [planValue] => 2 x 10 Whd
                            [historyValue] => 
                        )

                    [Hantel] => Array
                        (
                            [planValue] => Groß
                            [historyValue] => 
                        )

                    [Stufe] => Array
                        (
                            [planValue] => Hart
                            [historyValue] => 
                        )

                )

        )

)

0 个答案:

没有答案