如果for循环不匹配,如何添加空白表格单元格?

Posted

技术标签:

【中文标题】如果for循环不匹配,如何添加空白表格单元格?【英文标题】:How to add blank table cell if no match from for loop? 【发布时间】:2021-05-31 16:16:22 【问题描述】:

我希望表格单元格值根据顶部的列值位于右列中。如图所示,它目前没有正确对齐。当我使用 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','rebates.client_id','=','clients.id')
        ->whereDate('rebatedate','>=', $newDateString0)
        ->whereDate('rebatedate','<=', $newDateString1)
        ->orderBy('company')
        ->groupBy('company')
        ->get();

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

【问题讨论】:

【参考方案1】:

这个怎么样?

<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>

【讨论】:

我刚刚测试过,现在可以正常工作了。非常感谢! 很高兴听到这个消息

以上是关于如果for循环不匹配,如何添加空白表格单元格?的主要内容,如果未能解决你的问题,请参考以下文章

请教JAVA使用POI导出excel处理空白单元格的问题

如何根据ID更新另一张纸上的行而不用空白单元格覆盖?

匹配两列中的单元格值,如果匹配,则将另一个值复制到空白单元格

滚动表格视图时单元格变为空白(swift)

表格使用vlookup函数时出现内容是空白的,但是函数还在也没显示错误??

求助excel单元格不同自动添加3个空白行