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

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

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗