Jump to content

[SOLVED] SubTotal Help Please


Jezza22

Recommended Posts

I am trying to get a subtotal of a key from associated array and really need help..

 

The array looks like the following:-

 


Array
(
    [0] => Array
        (
            [0] => 7000
            [1] => CASH
            [2] => ALLOCATION
            [3] => 431.25
            [4] => 22/08/2009
            [5] => JS
        )

    [2] => Array
        (
            [0] => 7000
            [1] => CASH
            [2] => CASH
            [3] => -1006.25
            [4] => 22/08/2009
            [5] => JS
        )

    [3] => Array
        (
            [0] => 7000
            [1] => CASH
            [2] => ALLOCATION
            [3] => 1135.00
            [4] => 24/08/2009
            [5] => JS
        )

    [4] => Array
        (
            [0] => 7001
            [1] => CARDS
            [2] => SL_Receipts
            [3] => -431.25
            [4] => 23/08/2009
            [5] => JS
        )

    [5] => Array
        (
            [0] => 7003
            [1] => CHEQUES
            [2] => Chq 12458
            [3] => -2000.00
            [4] => 15/08/2009
            [5] => JS
        )

    [6] => Array
        (
            [0] => 7003
            [1] => CHEQUES
            [2] => Chq 122545
            [3] => -5000.00
            [4] => 24/08/2009
            [5] => JS
        )

    [7] => Array
        (
            [0] => 7005
            [1] => BANK
            [2] => BACS
            [3] => -1868.75
            [4] => 20/07/2009
            [5] => JS
        )

    [8] => Array
        (
            [0] => 7005
            [1] => BANK
            [2] => BASC
            [3] => -2000.00
            [4] => 21/07/2009
            [5] => JS
        )

)

 

 

What I would like to do, is get the subtotal of Key 0 which is a Payment Centre,  totalling Key 3 which is the Amount.

 

 

 

I have written a function that subtotals the whole array, but I need a grouped subtotal..

 

Here's the code,

 

<?php

function SubTotal($data, $key)
{
   $total = 0;
   foreach($data as $subarr)
      if (isset($subarr[$key]))
         $total += $subarr[$key];
   return $total;
}


foreach ($data as $dataRow)
{


if($dataRow[0]!=$lastRowID && $lastRowID != '')
{
	echo "Subtotal : ".SubTotal($data, 3);
	echo "<br />";
}
	echo "Centre ".$dataRow[0]." Centre Description ".$dataRow[1]." Amount ".$dataRow[3];
	echo "<br />";

$lastRowID = $dataRow[0];



}
?>

 

 

This produces the following: -

 

Centre 7000 Centre Description CASH Amount    431.25

Centre 7000 Centre Description CASH Amount -1006.25

Centre 7000 Centre Description CASH Amount  1135.00

Subtotal : -10740.00

Centre 7001 Centre Description CARDS Amount -431.25

Subtotal : -10740.00

Centre 7003 Centre Description CHEQUES Amount -5000.00

Centre 7003 Centre Description CHEQUES Amount -2000.00

Subtotal : -10740.00

Centre 7005 Centre Description BANK Amount -1868.75

Centre 7005 Centre Description BANK Amount -2000.00

 

The problem here is the subtotal is the whole amount, I need each centres subtotal..

 

Any Ideas would be much appreciated...??

Link to comment
https://forums.phpfreaks.com/topic/174782-solved-subtotal-help-please/
Share on other sites

Anything and everything is possible with programming :)

 

Something like this:

 

<?php

$subtotals[];

foreach($data as $transaction) {
    if(empty($subtotal[transaction[0]])) {
        $subtotal[transaction[0]] = 0;
    }
    $subtotal[$transaction[0]] += $transaction[3];
}

?>

 

And then do whatever you want with the subtotals.

Cracked it Guys!!

 

Here is the Final Code,

 

<?php

/function SubTotal($data, $key, $centre)
{
	$total = 0;
	   foreach($data as $subarr)
	    if (isset($subarr[$key]))
	       if ($subarr[0]==$centre)
                       $total += $subarr[$key];
	return $total;
    }


function displayArrayData($data)
{


	$lastRowID = '';

    //Iterrate through each record
    foreach ($data as $dataRow)
    {


	 $subTotal=0;
         //Subtotal if this record is different ID than last
        if($dataRow[0]!=$lastRowID && $lastRowID != '')
        {

				echo "SubTotal : ".SubTotal($data, 3, $lastRowID);
				echo "<br />";



        }  
        //Set last ID value
        $lastRowID = $dataRow[0];


	//Display current record
       echo "Centre ".$dataRow[0]." Centre Description ".$dataRow[1]." Amount ".$dataRow[3];
   echo "<br />";

}
    //Close the table and final subtotal
   echo "SubTotal : ".SubTotal($data, 3, $lastRowID);

}


displayArrayData($data)


 

This Display the following: -

 

Centre 7000 Centre Description CASH Amount -1525.00

Centre 7000 Centre Description CASH Amount -1135.00

Centre 7000 Centre Description CASH Amount -431.25

Centre 7000 Centre Description CASH Amount -431.25

Centre 7000 Centre Description CASH Amount 431.25

Centre 7000 Centre Description CASH Amount 1135.00

Centre 7000 Centre Description CASH Amount 1956.25

Centre 7000 Centre Description CASH Amount -100.00

Centre 7000 Centre Description CASH Amount 100.00

Centre 7000 Centre Description CASH Amount -1493.13

Centre 7000 Centre Description CASH Amount -1006.25

Centre 7000 Centre Description CASH Amount -517.50

Centre 7000 Centre Description CASH Amount -431.25

Centre 7000 Centre Description CASH Amount -431.25

Centre 7000 Centre Description CASH Amount -300.00

Centre 7000 Centre Description CASH Amount -150.00

Centre 7000 Centre Description CASH Amount -131.25

Centre 7000 Centre Description CASH Amount -200.00

Centre 7000 Centre Description CASH Amount -100.00

SubTotal : -4760.63

Centre 7001 Centre Description CARDS Amount -431.25

SubTotal : -431.25

Centre 7003 Centre Description CHEQUES Amount -5000.00

Centre 7003 Centre Description CHEQUES Amount -2000.00

SubTotal : -7000

Centre 7005 Centre Description BANK Amount -1868.75

Centre 7005 Centre Description BANK Amount -2000.00

SubTotal : -3868.75

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.