Jump to content

Need help displaying multiple rows using ajax


st0ked56

Recommended Posts

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.