2014-02-19 34 views
0

大家好我對編程不是很有經驗,所以這可能很容易實現。多行之後的新行

我從一個MySQL表使用PHP拉數據我想顯示像這樣的輸出:

$行[1] $行[2]線斷裂

$行[3] $行[4]線路斷線

$行[5] $行[6]號線打破

等等

任何幫助,將不勝感激感謝

$user_table = select_table("users", "user_name ASC"); 
$rows = array(); 

while($row = mysqli_fetch_assoc($user_table)) { 
    $rows[] = $row; 
} 

$user_groups = array_chunk($rows, 2); 

foreach ($user_groups as $user_group) { 

    echo "<tr>"; 

    foreach($user_group as $row) { 

     echo "<td class=\"user-box\">{$row["user_name"]}</td>"; 
    } 
    echo "</tr>"; 
} 

這是我的工作代碼。感謝蒂姆

+0

你有什麼到目前爲止的例子嗎?如果你使用mysql來拉數據,我相信你知道'echo'命令。 – thatidiotguy

+0

你怎麼「拉數據」? – EdCapetti

+0

'$ user_table = select_table(「users」,「user_name ASC」); while($ user = mysqli_fetch_array($ user_table)){ \t echo「

some layout of the users data
」; }' – Benwhittaker25

回答

0

遍歷你的結果,並開始計數後再次2. 這裏是一個使用簡單的數組和表,所以你可以看到它是如何工作

<?php 

$results = array('one','two','three','four'); 

echo '<table border="1">'; 

foreach(array_chunk($results,2) as $row) { 

    echo '<tr>'; 

    foreach($row as $value) { 
     echo '<td>'.$value.'</td>'; 
    } 

    echo '</tr>'; 
} 

echo '</table>'; 
+0

這很好用,我用我的工作代碼更新了我的第一篇文章。 – Benwhittaker25