Search code examples
phphtmlarrayscodeigniterforeach

is it possible to combine 3 same value in different array into one?


I have a couple of arrays contains values just like this :

Array
(
[0] => Array
    (
        [NAMA_MDS] => AGUNG RUDIYANTO               
        [Call] => 182
        [RO] => 151
        [NAMA_SGRUP] => Kanzler Singles               
        [EC] => 247
        [OT] => 57
        [Ratio] => 4
        [SBQ] => 6586
    )

[1] => Array
    (
        [NAMA_MDS] => AGUNG RUDIYANTO               
        [Call] => 182
        [RO] => 151
        [NAMA_SGRUP] => Yogurt Squeeze 120 GR         
        [EC] => 271
        [OT] => 103
        [Ratio] => 2
        [SBQ] => 9449
    )

[2] => Array
    (
        [NAMA_MDS] => AGUNG RUDIYANTO               
        [Call] => 182
        [RO] => 151
        [NAMA_SGRUP] => Yoghurt Drink 250 ML          
        [EC] => 271
        [OT] => 106
        [Ratio] => 2
        [SBQ] => 4960
    )

and because of that, when I foreach the data to my table the result gives me like this :

table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}
<div class="table-responsive">
          <table class="table-striped" style="width: 100%;">
            <thead>
              <tr>
                <th rowspan="2">Nama Sales</th>
                <th rowspan="2" width="20%">Nama Barang</th>
                <th colspan="2">EC</th>
                <th colspan="2">OT</th>
                <th rowspan="2">Ratio</th>
                <th rowspan="2">Sales QTY</th>
                <th colspan="2">Drop Size</th>
              </tr>
              <tr>
                <!-- <th width="100%">Nama Barang</th> -->
                <th>total</th><th width="10%">% vs Call</th>
                <th>total</th><th width="10%">% vs Call</th>
<!--                 <th>Ratio</th>
                <th>Sales QTY</th> -->
                <th>Per-EC</th><th>Per-OT</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>AGUNG RUDIYANTO</td>
                <td><small>Kanzler Singles</small></td>
                <td>247</td>
                <td>100%</td>
                <td>57</td>
                <td>38%</td>
                <td>4</td>
                <td>6,586</td>
                <td>27</td>
                <td>116</td>
              </tr>
              <tr>
                <td>AGUNG RUDIYANTO</td>
                <td><small>Yogurt Squeeze 120 GR</small></td>
                <td>271</td>
                <td>100%</td>
                <td>103</td>
                <td>69%</td>
                <td>2</td>
                <td>9,449</td>
                <td>35</td>
                <td>92</td>
              </tr>
              <tr>
                <td>AGUNG RUDIYANTO</td>
                <td><small>Yoghurt Drink 250 ML</small></td>
                <td>271</td>
                <td>100%</td>
                <td>106</td>
                <td>71%</td>
                <td>2</td>
                <td>4,960</td>
                <td>19</td>
                <td>47</td>
              </tr>
             </tbody>
            </table>
           </div>

and what I want to achieve is to have table like this :

table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}
<div class="table-responsive" style="overflow: scroll;height: 500px;">
          <table class="table-striped" style="width: 100%;">
            <thead>
              <tr>
                <th rowspan="2">Nama Sales</th>
                <th rowspan="2" width="20%">Nama Barang</th>
                <th colspan="2">EC</th>
                <th colspan="2">OT</th>
                <th rowspan="2">Ratio</th>
                <th rowspan="2">Sales QTY</th>
                <th colspan="2">Drop Size</th>
              </tr>
              <tr>
                <!-- <th width="100%">Nama Barang</th> -->
                <th>total</th><th width="10%">% vs Call</th>
                <th>total</th><th width="10%">% vs Call</th>
<!--                 <th>Ratio</th>
                <th>Sales QTY</th> -->
                <th>Per-EC</th><th>Per-OT</th>
              </tr>
            </thead>
            <tbody>
            <tr>
                <td rowspan="3">AGUNG RUDIYANTO</td>
                <td><small>Kanzler Singles</small></td>
                <td>247</td>
                <td>100%</td>
                <td>57</td>
                <td>38%</td>
                <td>4</td>
                <td>6,586</td>
                <td>27</td>
                <td>116</td>
              </tr>
              <tr>
                <td><small>Yogurt Squeeze 120 GR</small></td>
                <td>271</td>
                <td>100%</td>
                <td>103</td>
                <td>69%</td>
                <td>2</td>
                <td>9,449</td>
                <td>35</td>
                <td>92</td>
              </tr>
              <tr>
                <td><small>Yoghurt Drink 250 ML</small></td>
                <td>271</td>
                <td>100%</td>
                <td>106</td>
                <td>71%</td>
                <td>2</td>
                <td>4,960</td>
                <td>19</td>
                <td>47</td>
              </tr>

and I guess and correct me if I'm wrong, I have to make the arrays looks like this to achieve the table that I want :

Array
(
[NAMA_MDS] => AGUNG RUDIYANTO =>
   [0] => Array
    (              
        [Call] => 182
        [RO] => 151
        [NAMA_SGRUP] => Kanzler Singles               
        [EC] => 247
        [OT] => 57
        [Ratio] => 4
        [SBQ] => 6586
    )

[1] => Array
    (             
        [Call] => 182
        [RO] => 151
        [NAMA_SGRUP] => Yogurt Squeeze 120 GR         
        [EC] => 271
        [OT] => 103
        [Ratio] => 2
        [SBQ] => 9449
    )

[2] => Array
    (          
        [Call] => 182
        [RO] => 151
        [NAMA_SGRUP] => Yoghurt Drink 250 ML          
        [EC] => 271
        [OT] => 106
        [Ratio] => 2
        [SBQ] => 4960
    )

or something similar but i have no idea how to achieve the table that i want. can someone tell me how i can have that table that im try to achieve it with changing the array shape or without changing the arrays shape ?

here is my Controller:

public function test(){
    $data['db_default'] =  $this->db_default->default();
    $data['report2'] = $this->m_report->table2($data['db_default'][0]['databasename']);
    $data['content'] = 'tempelates/BKL/Report_B';
    $this->load->view('Tempelate',$data);
}

and this is my code in my view:

            <tbody>
          <?php foreach($report2->result_array() as $row){
            $Call = $row['Call'];
            $vsc = ceil(($row['EC']*100)/$Call);
            $RO = $row['RO'];
            $vsr = ceil(($row['OT']*100)/$RO)?>
          <tr>
            <td><?=$row['NAMA_MDS']?></td>
            <td><small><?=$row['NAMA_SGRUP']?></small></td>
            <td><?=$row['EC']?></td>
            <td><?php if($vsc >= 100){echo '100%';}else{echo $vsc.'%';}?></td>
            <td><?=$row['OT']?></td>
            <td><?php if($vsr >= 100){echo '100%';}else{echo $vsr.'%';}?></td>
            <td><?=$row['Ratio']?></td>
            <td><?=number_format($row['SBQ'])?></td>
            <td><?=ceil($row['SBQ']/$row['EC'])?></td>
            <td><?=ceil($row['SBQ']/$row['OT'])?></td>
          </tr>
        <?php }?>
        </tbody>

Solution

  • <?php
    
    $report = 
    array(
    
       array
        (   'NAMA_MDS' =>' AGUNG RUDIYANTO' ,           
            'Call' => 182,
            'RO' => 151,
            'NAMA_SGRUP' =>   'kkkk'  ,           
            'EC' => 247,
            'OT' => 57,
            'Ratio' => 4,
            'SBQ' => 6586,
        ),
       array
        (   'NAMA_MDS' =>' AGUNG RUDIYANTO' ,           
            'Call' => 182,
            'RO' => 151,
            'NAMA_SGRUP' =>   'kkkk'  ,           
            'EC' => 247,
            'OT' => 57,
            'Ratio' => 4,
            'SBQ' => 6586,
        ),
        );
    
    $html = '';//here
    ?>
    <div class="table-responsive">
              <table class="table-striped" style="width: 100%;">
                <thead>
                  <tr>
                    <th rowspan="2">Nama Sales</th>
                    <th rowspan="2" width="20%">Nama Barang</th>
                    <th colspan="2">EC</th>
                    <th colspan="2">OT</th>
                    <th rowspan="2">Ratio</th>
                    <th rowspan="2">Sales QTY</th>
                    <th colspan="2">Drop Size</th>
                  </tr>
                  <tr>
                    <!-- <th width="100%">Nama Barang</th> -->
                    <th>total</th><th width="10%">% vs Call</th>
                    <th>total</th><th width="10%">% vs Call</th>
    <!--                 <th>Ratio</th>
                    <th>Sales QTY</th> -->
                    <th>Per-EC</th><th>Per-OT</th>
                  </tr>
                </thead>
              
     <tbody>
              <?php foreach($report as $row){
                $Call = $row['Call'];
                $vsc = ceil(($row['EC']*100)/$Call);
                $RO = $row['RO'];
                $vsr = ceil(($row['OT']*100)/$RO);
              
    ?>
              <tr>
    <?php if($html != $row['NAMA_MDS']){?>//and here
                <td rowspan="3"><?=$row['NAMA_MDS']?></td>
    <?php }
    $html = $row['NAMA_MDS'];?>
    
                <td><small><?=$row['NAMA_SGRUP']?></small></td>
                <td><?=$row['EC']?></td>
                <td><?php if($vsc >= 100){echo '100%';}else{echo $vsc.'%';}?></td>
                <td><?=$row['OT']?></td>
                <td><?php if($vsr >= 100){echo '100%';}else{echo $vsr.'%';}?></td>
                <td><?=$row['Ratio']?></td>
                <td><?=number_format($row['SBQ'])?></td>
                <td><?=ceil($row['SBQ']/$row['EC'])?></td>
                <td><?=ceil($row['SBQ']/$row['OT'])?></td>
              </tr>
            <?php }?>
           </tbody>
    

    if you want to make row span count dynamic use the last array you listed with count of index and same way