Skip to content
Advertisement

How to add blank table cell if no match from for loop?

Table Generated

I would like for the table cell value to be in the right column based on the column value at the top. As seen, it currently does not align correctly. When I use the else condition, it duplicates the empty multiple times. Any help would be much appreciated.

Here is the current blade code

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

Here is the current Laravel Eloquent code:

$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]);
    

Advertisement

Answer

how about this?

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

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement