使用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 :
The end goal is to produce something as below :
Any help much appreciated. :)
答
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