0

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

2
  • $st['bulan'] doesn't existed, do you mean $st['month'] ? Commented Aug 18, 2017 at 3:35
  • sorry, try to change the word to english... thx for the answer.. Commented Aug 18, 2017 at 6:09

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

should be {{ isset($st['month'][$i]) ? $st['month'][$i] : "" }}
1

how to convert this on vue js?

 @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

Comments

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.