I'm sending an array to jQuery and then looping through those values and displaying them, but I can get it to work right. Can someone point me in the right direction -
PHP - the only part that matters on it - echo json_encode($tR);
jQuery -
$(document).on('click', '.chooseStat', function(){
var id = $(this).attr('id');
$.post('chooseStat.php', {id:id}, function(data){
console.log(data);
var open = '';
for(var i = 0; i < data.length; i++) {
if (i % 2 == 0) {
open+="<tr class='alt'>";
open+="<td>"+data[i]+"</td>";
open+="<td>"+data[i]+"</td>";
open+="<td>"+data[i]+"</td>";
open+="</tr>";
}
else {
open+="<tr>";
open+="<td>"+data[i]+"</td>";
open+="<td>"+data[i]+"</td>";
open+="<td>"+data[i]+"</td>";
open+="</tr>";
}
}
$('#statDisplayBody').html(open);
});
});
Heres what the array looks like when it gets sent to the jQuery script - http://prntscr.com/24wa68
Heres what it looks like when I try to display them with the method in the script I have - http://prntscr.com/24wadk
My new jquery -
$(document).on('click', '.chooseStat', function(){
var id = $(this).attr('id');
$.post('chooseStat.php', {id:id}, function(data){
console.log(data);
var open = '<th>Rank</th><th>Username</th><th>Stat</th>';
for(var i = 0; i < data.length; i++) {
if (i % 2 == 0) {
open+="<tr class='alt'>";
open+="<td>"+data[i]["rank"]+"</td>";
open+="<td>"+data[i]["username"]+"</td>";
open+="<td>"+data[i]["score"]+"</td>";
open+="</tr>";
}
else {
open+="<tr>";
open+="<td>"+data[i]["rank"]+"</td>";
open+="<td>"+data[i]["username"]+"</td>";
open+="<td>"+data[i]["score"]+"</td>";
open+="</tr>";
}
}
$('#statDisplayBody').html(open);
}, "json");
});
And that returns a proper array now, but the way I'm looping through apparently doesn't seem to be working because now this is happening - http://prntscr.com/24wcuz (its only displaying the last row of the array).