2015-09-06 92 views
0

我有下表,它從我的數據庫查詢用戶,然後在每個用戶下面添加14個玩家。我在同一個數據庫表中添加了一些列,我試圖讓列回顯出來,並且拋出了一個未定義的索引錯誤,但我不完全確定我可以在這裏定義它的位置。添加一組數據庫列到數組的問題

這是我試圖讓新列回聲的部分。我有14個玩家,所以這個數組通過它們遍歷每個用戶。我加入了相同數量的「位置」所以這將是PLAYER1位置1,player2位置2,等等

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

的我試圖做到這一點,這就是爲什麼我得到的錯誤...

foreach ($userPlayerStore as $userPlayer) { 
    echo '<td><div class="draftBorder">' . $userPlayer['player' . ' - ' . 'position' . $playerNum] . '</div></td>'; 

我還可以如何格式化,以便我可以在玩家旁邊顯示位置。就像這樣:

PLAYER1 - 位置1

全碼:

<?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> 
+0

什麼是你的數據庫表結構?你目前的輸出是什麼樣的? – Kuya

回答

0

的$ 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>'; 
}