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

从数组创建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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line