duankun9280 2016-08-01 11:11
浏览 68
已采纳

如何将结果分成3列

I have a set of results (e.g. [1,2,3,4,5,6,7,8,9,10,11]).

Which I want to be displayed as 1 row with 3 columns

1|5|9
2|6|10
3|7|11
4|8|

What I get is 1 row with 4 columns

1|4|7
2|5|8
3|6|9

10|
11|

Until I add a 12th object then I get 1 row with 3 columns

1|5|9
2|6|10
3|7|11
4|8|12

My code in blade template

<!-- partials/tables/table_list.blade.php -->

@section('tables')
<?php $chunkSize = floor(count($tables) / 3); ?>
<section id="tables-overview">
    <div class="row">
        @foreach ($tables->chunk($chunkSize , 1) as $chunk)
        <div class="col-md-4">
            @include('partials.tables.table_chunk')
        </div>
        @endforeach
    </div>
</section>
@endsection

<!-- partials/tables/table_chunk.blade.php -->

<table class="table table-responsive">
<thead>
    <tr>
        <th class="text-center">@lang('table.identifier')</th>
        <th class="text-center">@lang('table.property')</th>
        <th class="text-center">
            @permission('manage-tables')
                <a href="{{ route('CreateTable') }}">@lang('action.create')</a>
            @endpermission
        </th>
    </tr>
</thead>
<tbody>
    @foreach ($chunk as $table)
    <tr>
        <td class="text-center">{{ $table->getId() }}</td>
        <td class="text-center">{{ $table->getProperty() }}</td>
        <td class="text-center">
            @permission('manage-tables')
                <a class="btn btn-primary" href="{{ route('UpdateTable', ['id' => $table->getId()]) }}">@lang('action.update')</a>
            @endpermission
        </td>
    </tr>
    @endforeach

</tbody>
</table>

When the number of $tables can be divided by 3 (number of columns I want) I get 3 chunks. If not I get 3 chunks + the remaining 1 or 2 objects which both get put in a 4th column. I can place them horizontally as done here but I find this to be 'odd'. When reading a list you read from top to bottom first and then left to right.

UPDATE I also tried using ceil() as suggested by Huzaib Shafi. But then I get

4 objects (funny looking)
1|3|
2|4|

5 objects (better looking)
1|3|5
2|4

which is also not 100% what I wanted but is pretty close to it. I will try the suggestion of Homam Alhaytham next.

  • 写回答

3条回答 默认 最新

  • dounie0889 2016-08-01 11:25
    关注

    When you floor the division result, you get the lower number.

    Explanation: floor(11/3) = 3. Hence your result gets chunked 3 at a time (resulting in; [3,3,3,2]), however we need [4,4,3].

    So you need to ceil it. Giving you 4, thus the result.

    <?php $chunkSize = ceil(count($tables) / 3); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类