Milan AM
Milan AM

Reputation: 155

Table outputting extra <td>'s on View

I am trying to output a multidimensional array in my view, but am getting some wacky output on the table. Here is a screen grab of what I have:

Here is the code I am using:

    </tr>
  @endforeach



  </tbody>
</table>

I think it has something to do with my if else statement, but have been stuck on this problem for some time now. Any help would be much appreciated! Thanks

EDIT: As requested, here is my data structure and it's var_dump:

Upvotes: 1

Views: 77

Answers (1)

whoacowboy
whoacowboy

Reputation: 7447

It is hard to tell without your data structure, but I imagine the issue is with your data structure and your nested foreach.

When you are checking the $status you loop through the array. The first time you loop through the array $status == 1, but a td is being written every time you check the status.

@foreach ($count as $status => $c)

    @if($status == 2)
        <td><a href="#">{{$c}}</a></td>
    @else
        <td>&nbsp;</td>
    @endif

    ...

@endforeach

So on the first pass assuming $status = 1; You will get

<td>&nbsp;</td>
<td><a href="#">{{$c}}</a></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>

on the second pass, assuming $status = 2 you will get:

<td><a href="#">{{$c}}</a></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>

but you are still on the first person.

What you might consider is changing the structure of your data to something like this. This is psuedo code so please modify as necessary.

$person = [
    $totals => [
        $qualifiedProspectCount => 2,
        $unqualifiedProspectCount => 2,
        $contactedCount => 2,
        $qualifiedProspectCount => 2,
        $convertedToAccountCount => 2,
        $pendingIntegrationCount => 2,
        $revenueGeneratingCountCount => 2
    ]
];

Then only gling through your data once like this. I extracted the into a partial, but you could continue to do it inline.

page.blade.php

<table class="table tablesorter" id="tblAffs">
    <thead>
        <tr class="tblhead">
            <th>AM</th>
            <th>Qualified Prospect</th>
            <th>Unqualified Prospect</th>
            <th>Contacted</th>
            <th>Converted To Account</th>
            <th>Pending Integration</th>
            <th>Revenue Generating</th>
        </tr>
    </thead>
    <tbody>

        $totalCount should be a collection of $person
        @foreach ($totalCount as $name => $totals)

        <tr>
          <td>
            {{$name}}
            <br>
            <a href="#">Closed</a>
        </td>

        @include('partial.item', 'count' => $totals->qualifiedProspectCount)
        @include('partial.item', 'count' => $totals->unqualifiedProspectCount)
        @include('partial.item', 'count' => $totals->contactedCount)
        @include('partial.item', 'count' => $totals->convertedToAccountCount)
        @include('partial.item', 'count' => $totals->pendingIntegrationCount)
        @include('partial.item', 'count' => $totals->revenueGeneratingCountCount)

    </tr>
    @endforeach



</tbody>

partial/item.blade.php

@if($count > 0)
    <td><a href="#">{{{$count}}}</a></td>
@else
    <td>&nbsp;</td>
@endif

EDIT

So this is going to get you into all kinds of trouble later on, but here you go.

@foreach ($totalCount as $name => $count)

    <tr>
        <td>
            {{$name}}
            <br>
            <a href="#">Closed</a>
        </td>

        @if(isset($count[2]))
            <td><a href="#">{{ $count[2] }}</a></td>
        @else
            <td>&nbsp;</td>
        @endif
        @if(isset($count[1]))
            <td><a href="#">{{ $count[1] }}</a></td>
        @else
            <td>&nbsp;</td>
        @endif

        // and so on ...

    </tr>
@endforeach

I'd recommend checking out Laravel's Documentation, there are many ways to do what you are trying to do that are far easier than how you are going about it. Even though it feels faster. It isn't.

Upvotes: 1

Related Questions