Hi,
I am trying to display multiple rows using ajax. I currently am able to pass the json string back to ajax and loop through the array. Right now my ajax call only displays the last record in the table instead of all records (should be 2 in this case). The variable "len" is correct with the number 2. So it loops through twice but only displays the second row.
Here is my ajax code:
function displaySeason(teamID) {
if (teamID > 0) {
$.get(
"getTeamResult.php?team_id=" + teamID,
function(data) {
var len = data.length;
console.log(len);
for (var i = 0; i < len; i++){
$("#gamedateTeam").html(data[i][0]);
$("#homeTeam").html(data[i][1]);
$("#awayTeam").html(data[i][2]);
$("#goalsforTeam").html(data[i][3]);
$("#goalsagainstTeam").html(data[i][4]);
$("#yellowCardsTeam").html(data[i][5]);
$("#redCardsTeam").html(data[i][6]);
}
},
'json'
)
}
}
Here is my PHP and MySQL
<?php
require_once 'connect.php';
// Retrieve game_id number to make sure we should the right stats
$teamid = mysql_real_escape_string($_GET['team_id']);
$sql = "SELECT gamedate, home, away, goalsfor, goalsagainst, yellowcards, redcards FROM `game` WHERE `team_id` = $teamid"; //query
$r = mysqli_query($dbc, $sql) or die (mysqli_error($dbc));
//if (mysqli_affected_rows($dbc) == 1){
$results = array();
while ($array = mysqli_fetch_array($r)){ //fetch result
$results[] = $array;
}
echo json_encode($results);
mysqli_close();
and the HTML table it should be printing both rows out to.
<tr>
<td id="gamedateTeam"></td>
<td id="homeTeam"></td>
<td id="awayTeam"></td>
<td id="goalsforTeam"></td>
<td id="goalsagainstTeam"></td>
<td id="yellowCardsTeam"></td>
<td id="redCardsTeam"></td>
</tr>
I have been staring at this for hours. Any help would be greatly appreciated.
Thank you,
Mike