数据库结果分为3个相等的列

时间:2015-12-04 13:19:50

标签: php

我遇到了一个非常小的问题,但这是我想解决的问题,所以我正在寻找建议。

我正在使用以下PHP代码来回显数组中的数据。

Zend/zend_operators.c

2 个答案:

答案 0 :(得分:0)

只需将LIMIT 3添加到您的查询中,例如

SELECT * from wp_letter LIMIT 3 ORDER BY l_name ASC;

答案 1 :(得分:0)

您可以使用某些废话(例如):

$query = [1,2,3,4,5,6,7,8,9,10,11,12,13,14];    
$results = ['1col' => [], '2col' => [], '3col' => []];
foreach($query as $key => $result) {
    if (($key+1) % 2 === 0 && !(($key + 1) % 3 === 0)) {
        $results['2col'][] = $result;
    } elseif (($key+1) % 3 === 0) {
        $results['3col'][] = $result;
    } else {
        $results['1col'][] = $result;
    }

}

或者就像这样:

$results = array_chunk($query, ceil(count($query)/3));

链接:
array_chunk
ceil