I will post the whole php here but below is the part that I am having trouble with and focussing on, the actual population of the array.
$mysqli = new mysqli('localhost', 'root', '', 'dota_site_test');
if(mysqli_connect_errno()) {
echo "Connection Failed: " . mysqli_connect_errno();
exit();
}
$player_id = 123;//change later
if($stmt = $mysqli -> prepare("SELECT Match_ID,Hero,Result,GameMode,MMR FROM matches WHERE Player_ID = ?")) {
$stmt -> bind_param("i", $player_id);
$stmt -> execute();
$stmt -> bind_result($match_id, $hero, $result, $gamemode, $mmr);
//adds the table setup only when data is fetched ok (above)
echo "<table id='matches_table'>";
echo "<thead>
<tr>
<th>Hero</th>
<th>Result</th>
<th>Game Mode</th>
<th>MMR</th>
<th>Diff</th>
</tr>
</thead>";
$i = 1;
$mmr2 = 0;
$grapharray = array();
while($stmt -> fetch()){
//While there is still data being fetched, make the table rows below
$mmr1 = $mmr; //first mmr calc value = mmr from fetch
$diff = 0; //difference defined as 0
if ($mmr2 != 0){ //if the MMR2 has a value (from 2nd loop)
$diff = $mmr1-$mmr2;
}
$mmr2 = $mmr1; //so that both values can be present in the loop
echo "<tr><td>$hero</td>
<td>$result</td>
<td>$gamemode</td>
<td data-id='$i'>$mmr</td>
<td>$diff</td></tr>";
$i++;
$grapharray[$stmt['$hero']] = $stmt['$mmr'];
}
echo "</table>";
$stmt -> close();
}
$mysqli->close();
?>
Here is the part that I am focussing on, I am trying to use the while loop that populates a HTML table with data, and also use that to put the data cells (MMR and Hero name) into an array for use later creating graphs.
Here is that part more focussed:
while($stmt -> fetch()){
//While there is still data being fetched, make the table rows below
$mmr1 = $mmr; //first mmr calc value = mmr from fetch
$diff = 0; //difference defined as 0
if ($mmr2 != 0){ //if the MMR2 has a value (from 2nd loop)
$diff = $mmr1-$mmr2;
}
$mmr2 = $mmr1; //so that both values can be present in the loop
echo "<tr><td>$hero</td>
<td>$result</td>
<td>$gamemode</td>
<td data-id='$i'>$mmr</td>
<td>$diff</td></tr>";
$i++;
$grapharray[$stmt['$hero']] = $stmt['$mmr'];
}
I am not sure how one makes an array with this loop, since I get the error:
Fatal error: Cannot use object of type mysqli_stmt as array in C:\xampp\htdocs\Dota2HomePage\d2index.php on line 79
I guess I have done something completely wrong!