2015-04-03 80 views
1

我使用這個代碼來創建一個無限的表我的MySQL查詢:如何製作MySQL無限數組表?

<table cellspacing='0'> <!-- cellspacing='0' is important, must stay --> 

    <!-- Table Header --> 
    <thead> 
     <tr> 
      <th>User</th> 
      <th>SteamID</th> 
      <th>Banned by</th> 
      <th>Admin SteamID</th> 
      <th>Time Banned (min)</th> 
      <th>Reason</th> 
     </tr> 
    </thead> 
    <!-- Table Header --> 

    <!-- Table Body --> 
    <tbody> 
<?php 

echo '<tr>'; 

for($i = 0; $bans = mysqli_fetch_array($query2); $i = ($i+1)%3){ 
    echo '<td>'.$bans['name'].'</td>'; 
    echo '<td>'.$bans['steamid'].'</td>'; 
    echo '<td>'.$bans['nameAdmin'].'</td>'; 
    echo '<td>'.$bans['steamidAdmin'].'</td>'; 
    echo '<td>'.$bans['time'].'</td>'; 
    echo '<td>'.$bans['reason'].'</td>'; 
    if($i == 2) 
     echo '</tr><tr>'; 
} 

echo '</tr>'; 

?> 

</tbody> 

我從Mysql fetch array, table results

它正常工作的代碼,但它不能正確去進一步下跌超過6行。其他行無論出於何種原因放置在我最後一列的右側,如此截圖所示: http://puu.sh/h0qZF/a12de1dd87.png

我該如何解決這個問題?我的代碼有問題嗎?爲什麼會發生?

+0

',而(真)回聲「永不落幕」;' – adeneo 2015-04-03 21:05:03

+2

你所說的‘無限表’?的意思是,當你說,一個無限循環在我心中觸發,爲什麼你會想要那個? – CodeGodie 2015-04-03 21:05:56

+0

我只是表示該表的行將與我選擇的數據庫表中的行數相同。這是在這裏發現的概念:http://stackoverflow.com/questions/6757082/mysql-fetch-array-table-results – Aris 2015-04-03 21:07:52

回答

1

那麼,你的循環是沒有意義的。使用$i來注入新行,就像在這裏完成一樣,不是必需的;你能剛剛超過每行循環,然後輸出它作爲一個行:

<table> 
    <!-- <thead>...</thead> --> 
    <tbody> 
<?php while ($bans = mysqli_fetch_array($query2)): ?> 
     <tr> 
      <td><?php echo $bans['name'] ?></td> 
      <td><?php echo $bans['steamid'] ?></td> 
      <td><?php echo $bans['nameAdmin'] ?></td> 
      <td><?php echo $bans['steamidAdmin'] ?></td> 
      <td><?php echo $bans['time'] ?></td> 
      <td><?php echo $bans['reason'] ?></td> 
     </tr> 
<?php endwhile ?> 
    </tbody> 
</table> 
+0

謝謝!粘貼後完美工作! (儘管你把$ band而不是$ bans) – Aris 2015-04-03 21:21:17

0

按照說明使用while循環here。所以像這樣:

$result = $conn->query($sql); 

while($bans = $result->fetch_assoc()) { 
    echo '<td>'.$bans['name'].'</td>'; 
    echo '<td>'.$bans['steamid'].'</td>'; 
    echo '<td>'.$bans['nameAdmin'].'</td>'; 
} 
0

您正在製作兩列。

你的代碼,將打印出來的表格行的末尾每兩組數據:

if($i == 2) 
    echo '</tr><tr>'; 

這應該只是echo '</tr><tr>';