【问题标题】:How to display a list in three columns using php?如何使用php在三列中显示列表?
【发布时间】:2012-11-26 20:58:27
【问题描述】:

在过去的几个小时里,我一直没有成功地试图找出 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>';

【问题讨论】:

标签: php html html-table


【解决方案1】:

试试这个:

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

这不是很有效,但现在我想不出更好的方法。

【讨论】:

  • 非常感谢!这很好用!我的数据集不是很大,所以应该不错。
【解决方案2】:

如果您希望在列中呈现列表,您可以按逻辑顺序生成它,然后使用 CSS columns 属性在列中设置:

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

坏消息是 IE 9 及更早版本不支持此功能,但像 css3-multi-column.js 这样的 Columns polyfill 在简单的情况下可能工作得很好(尽管存在局限性和问题)。

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,出于以下原因,我发布了对这个老问题的回答:

    1. 我的回答更笼统,更容易让其他人适应。
    2. 我的数组是关联的,带有一个字符串键。顺便说一句,我的答案适用于关联数组和索引数组。
    3. 我不想要一个复杂的 CSS 解决方案,就像关于这个主题的其他一些答案一样。实际上,我想出的解决方案非常简单——在一张巨大的桌子内使用多个 style="float:left" 的标签。虽然我怀疑在单个表中包含多个 tbody 标记是否会通过 HTML 验证,但实际上它确实通过且没有错误。

    注意事项:

    • $numCols 是您想要的列数。
    • 由于我们是浮动项,您可能需要根据您的情况设置父元素的宽度和最小宽度和/或添加一些
    • 有关其他排序方法,请参阅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;
    }
    

    希望这个答案对您有用。

    【讨论】:

      猜你喜欢
      • 2017-02-09
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 2013-09-20
      • 1970-01-01
      • 2014-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多