2012-12-09 21 views
5

在過去的幾個小時,我一直不成功試圖找出PHP代碼分三路顯示一個列表,以便它有這個順序如何使用php顯示三列中的列表?

A D G 
B E H 
C F I 

但我真的失去了。誰能幫我這個? 我目前只有代碼順序

A B C 
D E F 
G H I 

,列出這是我當前的代碼:

echo '<table><tr>'; 
foreach ($categories as $k => $category) { 
    if ($k % 3 == 0 && $k ! = 0) { 
     echo '</tr><tr>'; 
    } 
    echo '<td><a href="category.php?category='.$category["id"].'">'.$category["category"].'</a></td>'; 
} 
echo '</table>'; 
+0

這些將始終是使用的數字? –

+0

實際上,在使用中,它們只會是字母。 –

+0

看看[stackoverflow.com。COM /問題/ 6938094 /數據從數據庫與 - 的foreach從頂至下#6938762(http://stackoverflow.com/questions/6938094/data-from-database-with-foreach-from機頂到下#6938762)。 – Sean

回答

4

試試這個:

$columns = 3; 
$rows = ceil(count($categories)/$columns); 

echo '<table>'; 

for ($row = 0; $row < $rows; $row++) { 
    echo '<tr>'; 

    foreach ($categories as $k => $category) { 
     if ($k % $rows == $row) { 
      echo '<td><a href="category.php?category='.$category["id"].'">'.$category["category"].'</a></td>'; 
     } 
    } 

    echo '</tr>'; 
} 

echo '</table>'; 

這不是很有效的,但現在我想不到更好的做法。

+0

非常感謝!這很好用!我的數據集不是那麼大,所以它應該是好的。 –

4

如果你想擁有列表列呈現所示,你可以在邏輯順序生成它,只是使用CSS columns屬性列設置:

ul { 
    -moz-column-count: 2; 
    -webkit-column-count: 2; 
    column-count: 2; 
} 

壞消息是IE 9和更早版本不支持這種方式,但像css3-multi-column.js這樣的列填充在簡單的情況下可能工作得很好(儘管它們有侷限性和問題)。

3

我有同樣的問題,我張貼我的答案,原因如下這個老問題:

  1. 我的回答是更普遍,容易讓別人適應。
  2. 我的數組是關聯的,並帶有一個字符串鍵。順便說一句,我的答案將適用於關聯和索引數組。
  3. 我不想複雜的CSS解決方案,就像這個主題的其他答案一樣。實際上,我提出的解決方案非常簡單 - 在一張巨大的桌子內部使用帶有style =「float:left」的多個標籤。雖然我懷疑在一個表中有多個tbody標籤會通過HTML驗證,但事實上它沒有錯誤地通過。

需要注意以下幾點:

  • $數numCols是你想要的列數。
  • 由於我們是浮動項目,您可能需要設置父元素的寬度和最小寬度和/或根據您的情況添加一些< br style =「clear:both」/ >。
  • 替代排序方法,請參閱http://php.net/manual/en/array.sorting.php

這裏是我完整的答案:

function sortVertically($data = array()) 
{ 
    /* PREPARE data for printing */ 
    ksort($data);  // Sort array by key. 
    $numCols = 3; // Desired number of columns 
    $numCells = is_array($data) ? count($data) : 1 ; 
    $numRows = ceil($numCells/$numCols); 
    $extraCells = $numCells % $numCols; // Store num of tbody's with extra cell 
    $i   = 0; // iterator 
    $cCell  = 0; // num of Cells printed 
    $output  = NULL; // initialize 


    /* START table printing */ 
    $output  .= '<div>'; 
    $output  .= '<table>'; 

    foreach($data as $key => $value) 
    { 
     if($i % $numRows === 0) // Start a new tbody 
     { 
      if($i !== 0)   // Close prev tbody 
      { 
       $extraCells--; 
       if ($extraCells === 0) 
       { 
        $numRows--;  // No more tbody's with an extra cell 
        $extraCells--; // Avoid re-reducing numRows 
       } 
       $output .= '</tbody>'; 
      } 

      $output .= '<tbody style="float: left;">'; 
      $i = 0;     // Reset iterator to 0 
     } 
     $output .= '<tr>'; 
      $output .= '<th>'.$key.'</th>'; 
      $output .= '<td>'.$value.'</td>'; 
     $output .= '</tr>'; 

     $cCell++;     // increase cells printed count 
     if($cCell == $numCells){ // last cell, close tbody 
      $output .= '</tbody>'; 
     } 

     $i++; 
    } 

    $output .= '</table>'; 
    $output .= '</div>'; 
    return $output; 
} 

我希望你能找到這個答案非常有用。

相關問題