drkxgs9358 2014-10-16 11:52
浏览 32
已采纳

使用php创建自动生成的表

I am trying to create an auto generated table which is a part of a project I am trying to do. I am trying to create a duty roster for a group of 14 staff. The shifts are as follows :

Shift 1 : 4 staff Shift 2: 4 staff Shift 3: 4 staff Off duty: 2 staff

To ease this process I have assigned a number to each staff, starting from 1 to 14.

Here is the code so far, but it does not work just right.

 <table>
<tr>
    <th>Shift 1</th>
    <th>Shift 2</th>
    <th>Shift 3</th>
    <th>Off</th>
</tr>

@for ($i = 1; $i <= 30 ; $i++)
<tr>
    @for ($j = 1; $j <= 4 ; $j++)
        <th>
            @if ($j==1)
                @for ($h = 1; $h <= 4 ; $h++)
                    {{ $h }},
                @endfor
            @endif

            @if ($j==2)
                @for ($h = 5; $h <= 8 ; $h++)
                    {{ $h }},
                @endfor
            @endif

            @if ($j==3)
                @for ($h = 9; $h <= 12 ; $h++)
                    {{ $h }},
                @endfor
            @endif

            @if ($j==4)
                @for ($h = 13; $h <= 14 ; $h++)
                    {{ $h }},
                @endfor
            @endif

        </th>
    @endfor

</tr>

@endfor

The above code gives me a table as below : enter image description here

The end goal is to produce something as below : enter image description here Any help much appreciated. :)

  • 写回答

1条回答 默认 最新

  • duanlei1957 2014-10-16 12:32
    关注

    You should do it in PHP and set to Blade only data to display because in blade it's hard to assign variables without modifications or hack.

    This is what I did:

    <table>
    <tr>
        <th>Shift 1</th>
        <th>Shift 2</th>
        <th>Shift 3</th>
        <th>Off</th>
    </tr>
    
    {{-- */$last=1;/* --}}
    @for ($i = 1; $i <= 30 ; $i++)
    <tr>
    
    
        @for ($j = 0; $j < 14 ; ++$j)
          @if ($j %4 == 0)
            <th>
          @endif
    
            @if ($j + $last > 14)
              {{-- */$last= -$j + 1 ;/* --}}
            @endif
            {{ $j + $last }}
          @if ($j %4 == 3)
            </th>
          @endif
    
        @endfor
    
        </th>
    {{-- */
        $last=$j + $last -2;
        if ($last > 14) {
            $last = 1;
        }
    /* --}}
    
    
    </tr>
    @endfor
    

    Output is as in below screen

    Output for shifts

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像