I have the following table that queries users from my database and then adds 14 players under each user. I added a couple of columns to the same database table and I am trying to get the columns to echo out and it is throwing an undefined index error, but I'm not entirely sure where I can define it in here.
This is the part I am trying to get the new columns to echo out at. I have 14 players, so this array runs through them all for each user. I am adding the same number of 'positions' So it will be player1 position1, player2 position2, etc.
for ($playerNum = 1; $playerNum <= $totalPlayerNumbers; $playerNum++) {
echo '<tr><td><div class="draftBorder">' . $playerNum . '</div></td>';
foreach ($userPlayerStore as $userPlayer) {
echo '<td><div class="draftBorder">' . $userPlayer['player' . $playerNum] . '</div></td>';
I tried to do this and this is why I am getting the errors...
foreach ($userPlayerStore as $userPlayer) {
echo '<td><div class="draftBorder">' . $userPlayer['player' . ' - ' . 'position' . $playerNum] . '</div></td>';
How else can I format this, so that I can get the position to show up next to the player. Like this:
Player1 - Position1
Full code:
<?php $userPlayerStore = array(); ?>
<table class="draft_border_table">
<tr>
<th>Rnd</th>
<?php
// Output usernames as column headings
$userResults = mysqli_query($con, 'SELECT * FROM user_players ORDER BY `id`');
while($userPlayer = mysqli_fetch_array($userResults)) {
$userPlayerStore[] = $userPlayer;
echo '<th><div>' . $userPlayer['username'] . '</div></th>';
}
?>
</tr>
<?php
// Output each user's player 1-14 in each row
$totalPlayerNumbers = 14;
for ($playerNum = 1; $playerNum <= $totalPlayerNumbers; $playerNum++) {
echo '<tr><td><div class="draftBorder">' . $playerNum . '</div></td>';
foreach ($userPlayerStore as $userPlayer) {
if (empty($userPlayer['player'.$playerNum])){
$extra_class = 'emptycell';
}else{
$extra_class = 'filledcell';
}
echo '<td class="draft_table_td">' . $userPlayer['player' . $playerNum] . '</td>';
}
echo '</tr>';
}
?>
</table>
The $userPlayer var is a reflection of each record in your database table with each index in this array being the column name. The code below should help you:
// Output each user's player 1-14 in each row
$totalPlayerNumbers = 14;
for ($playerNum = 1; $playerNum <= $totalPlayerNumbers; $playerNum++) {
// Start the player row
echo '<tr><td><div class="draftBorder">' . $playerNum . '</div></td>';
foreach ($userPlayerStore as $userPlayer) {
// Retrieve extra class name? Did you mean to use this?
$extraClass = empty($userPlayer['player' . $playerNum]) ? 'emptycell' : 'filledcell';
// Output player name and position
$playerName = $userPlayer['player' . $playerNum];
$playerPosition = $userPlayer['position' . $playerNum];
echo '<td class="draft_table_td">' . $playerName . ' - ' . $playerPosition . '</td>';
}
// End the player row
echo '</tr>';
}