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 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 如何提取csv文件中需要的列,将其整合为一篇完整文档,并进行jieba分词(语言-python)
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题