1

I have an array with 38 records.

After iterating over the first 10 I want it to start on a new column.

This is very simple and an example is below, however, I need to add in HTML which makes it difficult:

@for($i = 0; $i < count($records); $i++)
    @if($i % 10 == 0)
        //start new column
    @endif

    <li><a href="#">{{ $records[$i]['name'] }}</a></li>
@endfor

What the HTML looks like without looping and what it should look like after looping properly:

<li class="col-sm-3">
    <li class="dropdown-header">
        Record Set
    </li>
    <li><a href="#">Record Name</a></li>
    <li><a href="#">Record Name</a></li>
    <li><a href="#">Record Name</a></li>
</li>

Problem is, after 10 records I need it to break out of col-sm-3 and start a new col-sm-3 without the dropdown-header but with each iteration's record name.

How can this be done? Please ask questions if clarification is needed.

1 Answer 1

2

If it's an array then you may use array_chunk:

@foreach(array_chunk($records, 10) as $ten_arrays)
    <li class="col-sm-3">
        @foreach($ten_arrays as $record)
           {{ $record['field_name'] }}
        @endforeach
    </li>
@endforeach

This will output lis like this:

<li class="col-sm-3">
    <!-- Ten Items -->
</li>

<li class="col-sm-3">
    <!-- Ten Items -->
</li>
Sign up to request clarification or add additional context in comments.

2 Comments

I'm getting undefined index for my keys in the array.
What you have in the array, what fields ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.