Need help displaying multiple MySQL rows using AJAX

Need help displaying multiple MySQL rows using AJAX

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.  I am trying to figure out how to print out each row before it loops to the next row.  Any help would be greatly appreciated!


Thank you,

Mike

 

Here is my ajax code:


  
  1. function displaySeason(teamID) {
  2. if (teamID > 0) {
  3. $.get(
  4. "getTeamResult.php?team_id=" + teamID,
  5. function(data) {
  6. var len = data.length;
  7. console.log(len);
  8. for (var i = 0; i < len; i++){
  9. $("#gamedateTeam").html(data[i][0]);
  10. $("#homeTeam").html(data[i][1]);
  11. $("#awayTeam").html(data[i][2]);
  12. $("#goalsforTeam").html(data[i][3]);
  13. $("#goalsagainstTeam").html(data[i][4]);
  14. $("#yellowCardsTeam").html(data[i][5]);
  15. $("#redCardsTeam").html(data[i][6]);
  16. }
  17. },
  18. 'json'
  19. )
  20. }
  21. }
PHP and MySQL Code:
   
  1. <?php
  2. require_once 'connect.php';

  3. // Retrieve game_id number to make sure we should the right stats
  4. $teamid = mysql_real_escape_string($_GET['team_id']);

  5. $sql = "SELECT gamedate, home, away, goalsfor, goalsagainst, yellowcards, redcards FROM `game` WHERE `team_id` = $teamid"; //query

  6. $r = mysqli_query($dbc, $sql) or die (mysqli_error($dbc));
  7. //if (mysqli_affected_rows($dbc) == 1){
  8. $results = array();
  9. while ($array = mysqli_fetch_array($r)){ //fetch result
  10. $results[] = $array;
  11. }

  12. echo json_encode($results);
  13. mysqli_close();
HTML the rows should print to
   
  1. <tr>
  2. <td id="gamedateTeam"></td>
  3. <td id="homeTeam"></td>
  4. <td id="awayTeam"></td>
  5. <td id="goalsforTeam"></td>
  6. <td id="goalsagainstTeam"></td>
  7. <td id="yellowCardsTeam"></td>
  8. <td id="redCardsTeam"></td>
  9. </tr>