Php 使用codeigniter使用jQuery解码json
我需要知道如何使用jquery追加数组。下面是我的代码 codeigniter中的模型Php 使用codeigniter使用jQuery解码json,php,jquery,codeigniter,Php,Jquery,Codeigniter,我需要知道如何使用jquery追加数组。下面是我的代码 codeigniter中的模型 public function getranking(){ $this->db->select('*')->from('tour_ranking'); $this->db->join('teams','teams.team_id = tour_ranking.team_id'); $query = $this->db->get();
public function getranking(){
$this->db->select('*')->from('tour_ranking');
$this->db->join('teams','teams.team_id = tour_ranking.team_id');
$query = $this->db->get();
return $query->result();
}
这会使输出像
[
{
"Rank_Id": "1",
"Tour_id": "1",
"Team_id": "1",
"match_palyed": "1",
"Points": "100",
"Rating": "1",
"match_loss": "0",
"match_win": "1",
"is_display": "1",
"team_id": "1",
"team_name": "pakistan",
"is_active": "0",
"short_name": "PK",
"image_ex": "Pk.png"
}
]
我使用的jquery是
$.getJSON('http://localhost/main/ranking/',function(data){ count=0;
$('#rank-table').append('<tr class="rankingTr"><td class="rankingTd" id="rank-teamName">'+data'team_name'+' </td><td class="rankingTd" id="rank-matchplayed">'+data.match_palyed+'</td><td class="rankingTd" id="rank-points">'+data.Points+'</td><td class="rankingTd" id="rank-rating">'+data.Rating+'</td></tr>')
});
$.getJSON('http://localhost/main/ranking/,函数(数据){count=0;
$(“#排名表”).append(“+data”团队名称“+”+data.match\u palyed+”+data.Points+“+data.Rating+”)
});
我可以在我的控制台点击中看到结果,但当我附加它时,它会给我未定义的,因为您的输出是对象数组,所以您需要使用,如下所示:
您接收的是数组而不是对象
[]
和{}
分别是数组和对象,请确保不要混淆它的两个关系
您可以通过将数据从数组转换为对象来修复它
$.getJSON('http://localhost/main/ranking/',function(data){
data = data[0]; // just adding this line and you will get the object from first item in array.
count=0;
$('#rank-table').append('<tr class="rankingTr"><td class="rankingTd" id="rank-teamName">'+data'team_name'+' </td><td class="rankingTd" id="rank-matchplayed">'+data.match_palyed+'</td><td class="rankingTd" id="rank-points">'+data.Points+'</td><td class="rankingTd" id="rank-rating">'+data.Rating+'</td></tr>')
});
$.getJSON('http://localhost/main/ranking/,函数(数据){
data=data[0];//只要添加这一行,就可以从数组中的第一项获得对象。
计数=0;
$(“#排名表”).append(“+data”团队名称“+”+data.match\u palyed+”+data.Points+“+data.Rating+”)
});
$.getJSON('http://localhost/main/ranking/',function(data){
data = data[0]; // just adding this line and you will get the object from first item in array.
count=0;
$('#rank-table').append('<tr class="rankingTr"><td class="rankingTd" id="rank-teamName">'+data'team_name'+' </td><td class="rankingTd" id="rank-matchplayed">'+data.match_palyed+'</td><td class="rankingTd" id="rank-points">'+data.Points+'</td><td class="rankingTd" id="rank-rating">'+data.Rating+'</td></tr>')
});