向数组添加一组数据库列的问题

时间:2022-01-11 02:38:56

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.

我有下表从我的数据库查询用户,然后在每个用户下添加14个玩家。我在同一个数据库表中添加了几个列,我试图让这些列回显,它抛出了一个未定义的索引错误,但我不确定我可以在这里定义它。

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.

这是我试图让新专栏得到响应的部分。我有14个玩家,所以这个数组为每个用户运行。我添加了相同数量的“位置”,所以它将是player1 position1, player2 position2,等等。

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

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>

1 个解决方案

#1


0  

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:

$userPlayer var是数据库表中的每个记录的反射,其中每个索引都是列名。下面的代码可以帮助您:

// 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>';
}

#1


0  

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:

$userPlayer var是数据库表中的每个记录的反射,其中每个索引都是列名。下面的代码可以帮助您:

// 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>';
}