0

I have an array with data and I have to put them into a table. Array:

array(
 0=>1
 1=>Jon
 2=>[email protected]
 3=>2
 4=>Doe
 5=>[email protected]
 6=>3
 7=>Foo
 8=>[email protected])

So Table head is:

<table>
    <head>
       <tr>
         <th>ID</th>
         <th>Name</th>
         <th>Email</th>
       </tr>
    </thead>
</table>

If I go and loop through the array:

 <table>
    <thead>
       <tr>
         <th>ID</th>
         <th>Name</th>
         <th>Email</th>
       </tr>
    </thead>
    <tbody>
       @foreach($array as $data)
           <tr>
             <td>
               {{$data}}
             </td>
           </tr> 
       @endforeach
    <tbody>
</table>

Current Output:

Id   Name  Email
1   Jon  [email protected] 2 Doe [email protected]  3  Foo [email protected]

But My desired output is:

Id   Name  Email
 1   Jon   [email protected]
 2   Doe   [email protected]
 3   Foo   [email protected]

I tried to chunk array into arrays with array_chunk() but the result was the same.

I am on laravel but I can go with plain php also. Any tips would be much apreciated.

5 Answers 5

3
<tbody>
   @foreach(array_chunk($array, 3) as $data)
       <tr>
         <td>
           {{ $data[0] }}
         </td>
         <td>
           {{ $data[1] }}
         </td>
         <td>
           {{ $data[2] }}
         </td>
       </tr> 
   @endforeach
<tbody>
Sign up to request clarification or add additional context in comments.

7 Comments

Didn't know about chunk method :)
This gives me undefined offset [0]
@Davis Try: array_chunk($array, 3) in the place of collect($array)->chunk(3).
@SandOfVega Wow! This works. One more thing. I will never know how many <td></td> I will have so I would have to make it as dynamic as possible. Any tips on that? So in other word it would have to count <td>`s itself
@Davis read: HTML Tables. This tutorial may help you.
|
2

Hope this will help you.

<table>
    <thead>
       <tr>
         <th>ID</th>
         <th>Name</th>
         <th>Email</th>
       </tr>
    </thead>
    <tbody>
       @for($i = 0; $i < count($array); $i= $i+3)
           <tr>
             <td>
               {{ $array[$i] }}
             </td>
             <td>
               {{ $array[$i + 1] }}
             </td>
             <td>
               {{ $array[$i + 2] }}
             </td>
           </tr> 
       @endforeach
    <tbody>
</table>

Comments

0

First of all no way your current code will give that output you're showing. It should be vertical, each element in its separate row. But let's fix the problem.

You don't have proper objects to work on so here condition is that after 3 iterations, new data is coming. So you can take mod of 3 and make it work like this:

@foreach ( $array as $key => $value )

// you have a new dataset
@if ( ($key % 3) == 0 )
<tr>
    @endif
<td>{{ $value }}</td>
@if ( ($key % 3) == 0 )
</tr>
    @endif

@endforeach

I have not tested this code above but it should work. Though I would always prefer to use proper List of objects of User class or something else.

EDIT: I would prefer @Sand Of Vega's answer. Its better.

1 Comment

The problem is that I cant have a proper object because the data has been sent to me over stored procedure and it returns array. However this almoust works. Now it returns ID Name Email 1 [email protected] Joe 2Doe doe@email Doe
0

Try This

Re-place your with following.

 <table>
    <thead>
       <tr>
         <th>ID</th>
         <th>Name</th>
         <th>Email</th>
       </tr>
    </thead>
    <tbody>   
        <tr>
           @foreach($array as $key => $value)
               if($key % 3 == 0){
                  echo "</tr>";
                }
                echo "<td>".$value."</td>";
           @endforeach
    <tbody>
</table>

Comments

0

I am not so familiar with laravel butI hope following works for you.

<table>
  <thead>
    <tr>
      <th>ID</th>
      <th>Name</th>
      <th>Email</th>
    </tr>
  </thead>
    <tbody>
      $length = (int) count($array)/3;
      @for($i = 0; $i < $length; $i+=3)
        <tr>
          <td>{{$data[$i]}}</td>
          <td>{{$data[$i+1]}}</td>
          <td>{{$data[$i+2]}}</td>
        </tr> 
       @endfor
    <tbody>
</table>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.