douhuo0884 2016-05-06 17:35
浏览 78

使用Angular.js访问php数组数据

I've already asked a question like this but i'm getting issues. I'm using angular.js to send data to a php file. This php file is gathering a list of data and storing this in an array. I'm then encoding this array and sending it back to angular on a success function. I need to display each array one after the other on each other.

Any suggestions ?

if($result){
 while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){


  $Participants = array(
            firstname => $row['firstname'],
            lastname => $row['lastname'],
            amount => $row['longitude']
        );


 }

}

echo json_encode($Participants);

My angular

     angular.forEach(response.data, function(value, key){

        var Participants = {};

        Participants = {

          firstname: value['firstname'],
          lastname: value['lastname'],
          amount: value['amount'], 
        };

        console.log(Participants);

        });
  • 写回答

1条回答 默认 最新

  • duanlipeng4136 2016-05-06 17:37
    关注

    Your array will only ever hold one participant. Declare it above the loop, and append to it within:

    $Participants = array();
    
    if($result) {
        while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
    
            $Participants[] = array(
                firstname => $row['firstname'],
                lastname => $row['lastname'],
                amount => $row['longitude']
            );
    }
    

    A similar issue exists in your client-side angular code:

    // Note this is now an array instead of plain object
    var Participants = []; 
    angular.forEach(response.data, function(value, key){
        Participants.push({
            firstname: value['firstname'],
            lastname: value['lastname'],
            amount: value['amount'], 
        });
    });
    
    
    console.log(Participants);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog