Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping row by specified column from array

I have this array

   Array
(
    [0] => stdClass Object
        (
            [TransactionNo] => OPM-EC-K-LFMCD2-21.4
            [StokOpnameTransNo] => OPM-EC-K-LFMCD2-21
            [ItemCode] => BBG150400005
            [ItemName] => BIG BAG
            [OriginalPrice] => 289900.00
            [Price] => 289900.00
            [QtyBegining] => 0
            [QtyIn] => 4
            [QtySales] => 0
            [QtyMutasi] => 0
            [QtyStock] => 4
            [QtyOpname] => 0
            [QtyAdjust] => -4
            [TotalAdjust] => -1159600.00
            [rak] => 
            [line] => 
            [TransDate] => 2017-03-31 10:46:00.000
            [CustomerCode] => K-LFMCD2
            [CreatedBy] => 1208496.
            [Remark] => re
            [Remark2] => re
            [SOTerakhir] => 
            [TglSoTerakhir] => 
        )

    [1] => stdClass Object
        (
            [TransactionNo] => OPM-EC-K-LFMCD2-21.5
            [StokOpnameTransNo] => OPM-EC-K-LFMCD2-21
            [ItemCode] => BBG150500063
            [ItemName] => BIG BAG
            [OriginalPrice] => 479900.00
            [Price] => 479900.00
            [QtyBegining] => 0
            [QtyIn] => 0
            [QtySales] => 0
            [QtyMutasi] => 0
            [QtyStock] => 0
            [QtyOpname] => 1
            [QtyAdjust] => 0
            [TotalAdjust] => .00
            [rak] => SS
            [line] => SS
            [TransDate] => 2017-03-31 10:46:00.000
            [CustomerCode] => K-LFMCD2
            [CreatedBy] => 1208496.
            [Remark] => re
            [Remark2] => re
            [SOTerakhir] => 
            [TglSoTerakhir] => 
        )

    [2] => stdClass Object
        (
            [TransactionNo] => OPM-EC-K-LFMCD2-21.5
            [StokOpnameTransNo] => OPM-EC-K-LFMCD2-21
            [ItemCode] => BBG150500063
            [ItemName] => BIG BAG
            [OriginalPrice] => 479900.00
            [Price] => 479900.00
            [QtyBegining] => 0
            [QtyIn] => 1
            [QtySales] => 0
            [QtyMutasi] => 0
            [QtyStock] => 1
            [QtyOpname] => 0
            [QtyAdjust] => 0
            [TotalAdjust] => .00
            [rak] => 
            [line] => 
            [TransDate] => 2017-03-31 10:46:00.000
            [CustomerCode] => K-LFMCD2
            [CreatedBy] => 1208496.
            [Remark] => re
            [Remark2] => re
            [SOTerakhir] => 
            [TglSoTerakhir] => 
        )

    [3] => stdClass Object
        (
            [TransactionNo] => OPM-EC-K-LFMCD2-21.6
            [StokOpnameTransNo] => OPM-EC-K-LFMCD2-21
            [ItemCode] => BBG150500081
            [ItemName] => BIG BAG
            [OriginalPrice] => 229900.00
            [Price] => 229900.00
            [QtyBegining] => 0
            [QtyIn] => 0
            [QtySales] => 0
            [QtyMutasi] => 0
            [QtyStock] => 0
            [QtyOpname] => 1
            [QtyAdjust] => 0
            [TotalAdjust] => .00
            [rak] => SS
            [line] => SS
            [TransDate] => 2017-03-31 10:46:00.000
            [CustomerCode] => K-LFMCD2
            [CreatedBy] => 1208496.
            [Remark] => re
            [Remark2] => re
            [SOTerakhir] => 
            [TglSoTerakhir] => 
        )

)

My array(s) comming from the query result . Then i'm trying to put it to HTML Table so i try this way.

<table class="table ">
                    <thead>
                        <tr>
                            <th>No</th>
                            <th>ItemCode</th>
                            <th>ItemName</th>
                            <th>TransNo</th>
                            <th>Original Price</th>
                            <th>Price</th>
                             <th>QtyBeginning</th>
                            <th>QtyIn</th>
                            <th>QtySales</th>
                            <th>QtyMutasi</th>
                            <th>QtyStock</th>
                            <th>QtyOpname</th>
                            <th>QtyAdjust</th>
                            <th>TotalAdjust</th>
                            <th>Rak</th>
                            <th>Line</th>
                        </tr>
                    </thead>
                    <tbody> <?php $x=0; foreach ($datadetail as $data) { $x++;?>
                                 <tr>
                                    <td><?=$x;?></td>
                                    <td><?=$data->ItemCode;?></td>
                                    <td><?=$data->ItemName;?></td>
                                    <td><?=$data->StokOpnameTransNo;?></td>
                                    <td><?=$data->OriginalPrice;?></td>
                                    <td><?=$data->Price;?></td>
                                    <td><?=$data->QtyBegining;?></td>
                                    <td><?=$data->QtyIn;?></td>
                                    <td><?=$data->QtySales;?></td>
                                    <td><?=$data->QtyMutasi;?></td>
                                    <td><?=$data->QtyStock;?></td>
                                    <td><?=$data->QtyOpname;?></td>
                                    <td><?=$data->QtyAdjust;?></td>
                                    <td><?=currency($data->TotalAdjust);?></td>
                                    <td><?=$data->rak;?></td>
                                    <td><?=$data->line;?></td>
                                </tr>
                            <?php } ?> 

                    </tbody>

            </table>

the result is just a simple table like this. There is no problem when showing my table . But, I want to change the result(in HTML) to something like this

enter image description here

it just like grouping by ItemCode. SO after every itemcode I want to sum QtyBeginning,QtyIn,QtyOut etc.

like image 268
YVS1102 Avatar asked Mar 24 '17 08:03

YVS1102


1 Answers

i dont know your table structure but in my opinion you should be able to organize your data from your db

either way i try to show you a way how to do it (a fulltestscript)

TestData:

$arrTestData = [];

$obj = new stdClass();
$obj->TransactionNo = "OPM-EC-K-LFMCD2-21.4";
$obj->StokOpnameTransNo = "OPM-EC-K-LFMCD2-21";
$obj->ItemCode = "BBG150400005";
$obj->ItemName = "BIG BAG";
$obj->OriginalPrice = "289900.00";
$obj->Price = "289900.00";
$obj->QtyBegining = "0";
$obj->QtyIn = "4";
$obj->QtySales = "0";
$obj->QtyMutasi = "0";
$obj->QtyStock = "4";
$obj->QtyOpname = "2";
$obj->QtyAdjust = "-4";
$obj->TotalAdjust = "-1159600.00";
$obj->rak = "";
$obj->line = "";
$obj->TransDate = "2017-03-31 10:46:00.000";
$obj->CustomerCode = "K-LFMCD2";
$obj->CreatedBy = "1208496.";
$obj->Remark = "re";
$obj->Remark2 = "re";
$obj->SOTerakhir = "";
$obj->TglSoTerakhir = "";
$arrTestData[] = $obj;

$obj = new stdClass();
$obj->TransactionNo = "OPM-EC-K-LFMCD2-21.4";
$obj->StokOpnameTransNo = "OPM-EC-K-LFMCD2-21";
$obj->ItemCode = "BBG150400005";
$obj->ItemName = "BIG BAG";
$obj->OriginalPrice = "289900.00";
$obj->Price = "289900.00";
$obj->QtyBegining = "0";
$obj->QtyIn = "2";
$obj->QtySales = "0";
$obj->QtyMutasi = "0";
$obj->QtyStock = "4";
$obj->QtyOpname = "4";
$obj->QtyAdjust = "-4";
$obj->TotalAdjust = "-1159600.00";
$obj->rak = "";
$obj->line = "";
$obj->TransDate = "2017-03-31 10:46:00.000";
$obj->CustomerCode = "K-LFMCD2";
$obj->CreatedBy = "1208496.";
$obj->Remark = "re";
$obj->Remark2 = "re";
$obj->SOTerakhir = "";
$obj->TglSoTerakhir = "";
$arrTestData[] = $obj;

$obj = new stdClass();

$obj->TransactionNo = "OPM-EC-K-LFMCD2-21.5";
$obj->StokOpnameTransNo = "OPM-EC-K-LFMCD2-21";
$obj->ItemCode = "BBG150500063";
$obj->ItemName = "BIG BAG";
$obj->OriginalPrice = "479900.00";
$obj->Price = "479900.00";
$obj->QtyBegining = "0";
$obj->QtyIn = "0";
$obj->QtySales = "0";
$obj->QtyMutasi = "0";
$obj->QtyStock = "0";
$obj->QtyOpname = "1";
$obj->QtyAdjust = "0";
$obj->TotalAdjust = ".00";
$obj->rak = "SS";
$obj->line = "SS";
$obj->TransDate = "2017-03-31 10:46:00.000";
$obj->CustomerCode = "K-LFMCD2";
$obj->CreatedBy = "1208496.";
$obj->Remark = "re";
$obj->Remark2 = "re";
$obj->SOTerakhir = "";
$obj->TglSoTerakhir = "";
$arrTestData[] = $obj;

$obj = new stdClass();

$obj->TransactionNo = "OPM-EC-K-LFMCD2-21.5";
$obj->StokOpnameTransNo = "OPM-EC-K-LFMCD2-21";
$obj->ItemCode = "BBG150500063";
$obj->ItemName = "BIG BAG";
$obj->OriginalPrice = "479900.00";
$obj->Price = "479900.00";
$obj->QtyBegining = "0";
$obj->QtyIn = "1";
$obj->QtySales = "0";
$obj->QtyMutasi = "0";
$obj->QtyStock = "1";
$obj->QtyOpname = "0";
$obj->QtyAdjust = "0";
$obj->TotalAdjust = ".00";
$obj->rak = "";
$obj->line = "";
$obj->TransDate = "2017-03-31 10:46:00.000";
$obj->CustomerCode = "K-LFMCD2";
$obj->CreatedBy = "1208496.";
$obj->Remark = "re";
$obj->Remark2 = "re";
$obj->SOTerakhir = "";
$obj->TglSoTerakhir = "";
$arrTestData[] = $obj;

$obj = new stdClass();

$obj->TransactionNo = "OPM-EC-K-LFMCD2-21.6";
$obj->StokOpnameTransNo = "OPM-EC-K-LFMCD2-21";
$obj->ItemCode = "BBG150500081";
$obj->ItemName = "BIG BAG";
$obj->OriginalPrice = "229900.00";
$obj->Price = "229900.00";
$obj->QtyBegining = "0";
$obj->QtyIn = "0";
$obj->QtySales = "0";
$obj->QtyMutasi = "0";
$obj->QtyStock = "0";
$obj->QtyOpname = "1";
$obj->QtyAdjust = "0";
$obj->TotalAdjust = ".00";
$obj->rak = "SS";
$obj->line = "SS";
$obj->TransDate = "2017-03-31 10:46:00.000";
$obj->CustomerCode = "K-LFMCD2";
$obj->CreatedBy = "1208496.";
$obj->Remark = "re";
$obj->Remark2 = "re";
$obj->SOTerakhir = "";
$obj->TglSoTerakhir = "";
$arrTestData[] = $obj;

You need an Item Collection or something like that which holds and structurize your data

class ItemCollection extends ArrayObject
{

    private $strCollectionCode;
    private $strCollectionName;

    public function addItem($objItem)
    {
        $this->strCollectionCode = $objItem->ItemCode;
        $this->strCollectionName = $objItem->ItemName;
        $this->append($objItem);
    }

    public function get($key)
    {
        return $this->$key;
    }

    public function getSummaries($strKey)
    {
        $floatNumber = 0;
        foreach($this AS $objItem)
        {
            $floatNumber += floatval($objItem->$strKey);
        }
        return $floatNumber;
    }
}

And a Wrapper should contain this Collections

class ItemCollectionWrapper
{
    private $arrCollections = array();

    public function __construct($arrData = false)
    {
        if (is_array($arrData))
        {
            $this->addItemsFromArray($arrData);
        }
    }

    public function addItem($objItem)
    {
        if (isset($this->arrCollections[$objItem->ItemCode]))
        {
            $objCollection = $this->arrCollections[$objItem->ItemCode];
        }
        else
        {
            $objCollection = new ItemCollection();
            $this->arrCollections[$objItem->ItemCode] = $objCollection;
        }

        $objCollection->addItem($objItem);
    }

    public function addItemsFromArray($arrData)
    {
        foreach($arrData AS $obj)
        {
            $this->addItem($obj);
        }
    }

    public function getCollections()
    {
        return $this->arrCollections;
    }
}

and finally our view

<?php
    $obj = new ItemCollectionWrapper($arrTestData);

?>
<!DOCTYPE html> 
<html lang=en> 
<head> 
    <meta charset=utf-8> 
    <meta content="IE=edge" http-equiv=X-UA-Compatible> 
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
<style type="text/css">
    td.summaries 
    {
        background:#c6c600;
    }
</style>
<table class="table">
<thead>
    <tr>
        <th>No</th>
        <th>ItemCode</th>
        <th>ItemName</th>
        <th>TransNo</th>
        <th>Original Price</th>
        <th>Price</th>
         <th>QtyBeginning</th>
        <th>QtyIn</th>
        <th>QtySales</th>
        <th>QtyMutasi</th>
        <th>QtyStock</th>
        <th>QtyOpname</th>
        <th>QtyAdjust</th>
        <th>TotalAdjust</th>
        <th>Rak</th>
        <th>Line</th>
    </tr>
</thead>
<tbody>
<?php
foreach($obj->getCollections() AS $objCollection)
{
    $number = 1;
?>
    <?php
    foreach($objCollection AS $key => $objItem) :
    ?>
     <tr>
        <?php
        if ($key == 0)  :
        ?>
        <td rowspan="<?=$objCollection->count();?>"><?=$number;?></td>
        <td rowspan="<?=$objCollection->count();?>"><?=$objCollection->get("strCollectionCode");?></td>
        <td rowspan="<?=$objCollection->count();?>"><?=$objCollection->get("strCollectionName");?></td>
        <?php endif; ?>
        <td><?=$objItem->StokOpnameTransNo;?></td>
        <td><?=$objItem->OriginalPrice;?></td>
        <td><?=$objItem->Price;?></td>
        <td><?=$objItem->QtyBegining;?></td>
        <td><?=$objItem->QtyIn;?></td>
        <td><?=$objItem->QtySales;?></td>
        <td><?=$objItem->QtyMutasi;?></td>
        <td><?=$objItem->QtyStock;?></td>
        <td><?=$objItem->QtyOpname;?></td>
        <td><?=$objItem->QtyAdjust;?></td>
        <td><?=($objItem->TotalAdjust);?></td>
        <td><?=$objItem->rak;?></td>
        <td><?=$objItem->line;?></td>
    </tr>
    <?php
    endforeach;
    ?>
    <tr>
        <td colspan="6"></td>
        <td class="summaries"><?=$objCollection->getSummaries("QtyBegining");?></td>
        <td class="summaries"><?=$objCollection->getSummaries("QtyIn");?></td>
        <td class="summaries"><?=$objCollection->getSummaries("QtySales");?></td>
        <td class="summaries"><?=$objCollection->getSummaries("QtyMutasi");?></td>
        <td class="summaries"><?=$objCollection->getSummaries("QtyStock");?></td>
        <td class="summaries"><?=$objCollection->getSummaries("QtyOpname");?></td>
        <td class="summaries"><?=$objCollection->getSummaries("QtyAdjust");?></td>
        <td class="summaries"><?=$objCollection->getSummaries("TotalAdjust");?></td>
        <td colspan="2"></td>
    </tr>
<?php
    $number++;
}
?>
</tbody>
</table>
</body>
</html>

i hope it helps - if you study this code - you should be able to extend it in whatever way you like.

btw: You can put the classes ItemCollectionWrapper and ItemCollection below your model Code if you dont know how to do that - show me your model and your controller and i'll show you.

like image 151
sintakonte Avatar answered Oct 06 '22 00:10

sintakonte