dqalnwuci494308
2017-08-18 03:24
浏览 23
已采纳

从数组创建laravel刀片水平表视图

I have an Laravel DB::Query that give me result like this

 arrayResult = array('nip' => '12345678', 'nama' => 'rachmat', 'month' => '1', 'sum' => 13'),
               array('nip' => '12345678', 'nama' => 'rachmat', 'month' => '3', 'sum' => 10'),
               array('nip' => '12345678', 'nama' => 'rachmat', 'month' => '8', 'sum' => 9'));

then i create multidimensional array like this

foreach ($pegawai as $key ) {
            $peg[$key->nip]['nama'] = $key->nama;
            $peg[$key->nip]['nip'] = $key->nip;
            $peg[$key->nip]['month'][$key->month] = $key->sum;

        }

the result of the array is like this

array(12345678
    ('nama' => 'Rachmat', 
     'nip' => '12345678', 
     Month(1 => 13,
           3 => 10,
           8 => 9)));

then i view it in the blade table like this :

@foreach($peg as $st)
   <tr>
       <td style="text-align: center;">{{$no++}}</td>
       <td>{{$st['nama']}}<br>{{$st['nip']}}</td>    
       @foreach($st['bulan'] as $a => $value)     
       <td style="text-align: center;"> {{ $value }}   </td>
       @endforeach
       </tr>
@endforeach

the problem is the $value data shown in a sequence, not in accordance with the month of the data.

my view :

<table id="table" class="table table-striped table-bordered table-hover" >
                <thead>
                  <tr class="tableheader">
                  <th style="width:10px" rowspan="2" style="text-align: center; vertical-align: middle;">#</th>
                  <th rowspan="2" style="text-align: center; vertical-align: middle;">Nama Pegawai</th>
                  <th colspan="12" style="text-align: center;">Bulan</th>
                  </tr> 
                  <tr>
                    <th style="text-align: center;">January</th>
                    <th style="text-align: center;">February</th>
                    <th style="text-align: center;">March</th>
                    <th style="text-align: center;">April</th>
                    <th style="text-align: center;">May</th>
                    <th style="text-align: center;">June</th>
                    <th style="text-align: center;">July</th>
                    <th style="text-align: center;">August</th>
                    <th style="text-align: center;">Sept</th>
                    <th style="text-align: center;">Okt</th>
                    <th style="text-align: center;">Nop</th>
                    <th style="text-align: center;">Dec</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                      <td style="text-align: center;">1</td>
                      <td>Rachmat<br>123456</td>             
                    <td style="text-align: center;"> 13   </td>
                    <td style="text-align: center;">  10 </td>
                    <td style="text-align: center;"> 9  </td>
                    <td style="text-align: center;">   </td>
                    <td style="text-align: center;">   </td>
                    <td style="text-align: center;">   </td>
                    <td style="text-align: center;">   </td>
                    <td style="text-align: center;">  </td>
         
                  </tr>

                </tbody>
                </table>

what i want :

<table id="table" class="table table-striped table-bordered table-hover" >
                <thead>
                  <tr class="tableheader">
                  <th style="width:10px" rowspan="2" style="text-align: center; vertical-align: middle;">#</th>
                  <th rowspan="2" style="text-align: center; vertical-align: middle;">Nama Pegawai</th>
                  <th colspan="12" style="text-align: center;">Bulan</th>
                  </tr> 
                  <tr>
                    <th style="text-align: center;">January</th>
                    <th style="text-align: center;">February</th>
                    <th style="text-align: center;">March</th>
                    <th style="text-align: center;">April</th>
                    <th style="text-align: center;">May</th>
                    <th style="text-align: center;">June</th>
                    <th style="text-align: center;">July</th>
                    <th style="text-align: center;">August</th>
                    <th style="text-align: center;">Sept</th>
                    <th style="text-align: center;">Okt</th>
                    <th style="text-align: center;">Nop</th>
                    <th style="text-align: center;">Dec</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                      <td style="text-align: center;">1</td>
                      <td>Rachmat<br>123456</td>             
                    <td style="text-align: center;"> 13   </td>
                    <td style="text-align: center;">   </td>
                    <td style="text-align: center;"> 10  </td>
                    <td style="text-align: center;">   </td>
                    <td style="text-align: center;">   </td>
                    <td style="text-align: center;">   </td>
                    <td style="text-align: center;">   </td>
                    <td style="text-align: center;"> 9  </td>
         
                  </tr>

                </tbody>
                </table>

all help appriciated

</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongtan9518 2017-08-18 03:53
    已采纳

    Try this one

    @foreach($peg as $st)
       <tr>
           <td style="text-align: center;">{{$no++}}</td>
           <td>{{$st['nama']}}<br>{{$st['nip']}}</td>  
    
           @for($i = 1, $i <= 12, $i++)
              <td style="text-align: center;"> {{ isset($st['month'][$i]) ? $st['month'][$i] : "" }}   </td>
           @endfor
    
       </tr>
     @endforeach
    
    点赞 打赏 评论

相关推荐 更多相似问题