如果for循环中没有匹配项,如何添加空白表格单元格?

问题描述

Table Generated

我希望表格单元格值基于顶部的列值位于右列中。正如所见,它目前没有正确对齐。当我使用 else 条件时,它会多次重复空。任何帮助将不胜感激。

这是当前的刀片代码

<table class="table table-bordered">
        <tr>
        <th scope="col" style="width:100px;">Date</th>
@foreach($uniqueClients as $c)
<th>{{$c->company}}</th>
@endforeach
</tr>
@foreach($datesofmonth as $d)
@PHP
    $counterdata = 0;
@endPHP
<tr>
<td>{{$d}}</td>
@foreach($uniqueClients as $c => $value)
@foreach($clients as $cl)
@PHP
    $rebateFormatDate = date('d/m/Y',strtotime($cl->rebatedate));
@endPHP
@PHP
    if($uniqueClients[$c]['company']){
        $currentbusiness = $uniqueClients[$c]['company'];
    }
@endPHP
@if($rebateFormatDate == $d && $cl->company == $currentbusiness)
<td>{{$cl->company}}</td>
@endif
@endforeach
@endforeach
@endforeach
</tr>
</table>

这是当前的 Laravel Eloquent 代码

$newArray = Array();
        function daterange( $first,$last,$step = '+1 day',$format = 'd/m/Y' ) {
            $dates = array();
            $current = strtotime( $first );
            $last = strtotime( $last );
            while( $current <= $last ) {
                $dates[] = date( $format,$current );
                $current = strtotime( $step,$current );
            }
            return $dates;
        }

        $datesofmonth = daterange( 'first day of this month','last day of this month');
        $myDateTime0 = DateTime::createFromFormat('d/m/Y',$datesofmonth[0]);
        $newDateString0 = $myDateTime0->format('Y-m-d');
        $myDateTime1 = DateTime::createFromFormat('d/m/Y',end($datesofmonth));
        $newDateString1 = $myDateTime1->format('Y-m-d');

        $clients = Rebate::join('clients','rebates.client_id','=','clients.id')
        ->whereDate('rebatedate','>=',$newDateString0)
        ->whereDate('rebatedate','<=',$newDateString1)
        ->orderBy('rebatedate')
        ->get();

        $uniqueClients = Client::join('rebates',$newDateString1)
        ->orderBy('company')
        ->groupBy('company')
        ->get();

        return view('rebates',['datesofmonth' => $datesofmonth,'clients' => $clients,'uniqueClients' => $uniqueClients,'newArray' => $newArray]);
    

解决方法

这个怎么样?

<table class="table table-bordered">
        <tr>
        <th scope="col" style="width:100px;">Date</th>
@foreach($uniqueClients as $c)
<th>{{$c->company}}</th>
@endforeach
</tr>
@foreach($datesofmonth as $d)
@php
    $counterdata = 0;
@endphp
<tr>
<td>{{$d}}</td>
@foreach($uniqueClients as $c => $value)
<td>
@foreach($clients as $cl)
@php
    $rebateFormatDate = date('d/m/Y',strtotime($cl->rebatedate));
@endphp
@php
    if($uniqueClients[$c]['company']){
        $currentbusiness = $uniqueClients[$c]['company'];
    }
@endphp
@if($rebateFormatDate == $d && $cl->company == $currentbusiness)
{{$cl->company}}
@endif
@endforeach
</td>
@endforeach
@endforeach
</tr>
</table>