【问题标题】:How to add blank table cell if no match from for loop?如果for循环不匹配,如何添加空白表格单元格?
【发布时间】: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]);
    

【问题讨论】:

    标签: php laravel for-loop eloquent laravel-blade


    【解决方案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>
    
    

    【讨论】:

    • 我刚刚测试过,现在可以正常工作了。非常感谢!
    • 很高兴听到这个消息
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 2019-03-25
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 2018-05-06
    相关资源
    最近更新 更多