PHP - Totaling a Table Column

Hey everyone;

I am pretty new to PHP coding, but was required to learn some for my Work Study job at college. Currently I am converting an HTML script from a Journal Report using the SOAP protocol. I was able to configure the information I needed into a table format, however I am not certain how to get the totals of the individual columns. Any help will be appreciated!!!

[php]

<?php $x=1; //Requestor Information $xml = simplexml_load_file("J1.xml"); $Info = $xml->children("soap", true)->Body-> children("ns3", true)->ReportResponse-> children()->Requestor; foreach ($Info as $info) { echo $info->ID, "
" , $info->Name, "
", $info->Email, "

"; } //Vendor Info $Report = $xml->children("soap", true)->Body-> children("ns3", true)->ReportResponse->children("ns3", true)->Report-> children("ns2", true)->Report->children("ns2", true)->Vendor ->children("ns2",true)->ID; foreach ($Report as $report) { echo $report, "
", "
"; } //Meat and Potatoes $Items = $xml->children("soap", true)->Body-> children("ns3", true)->ReportResponse->children("ns3", true)->Report-> children("ns2", true)->Report->children("ns2", true)->Customer-> children("ns2", true)->ReportItems; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; foreach ($Items as $items) { echo ""; $x++; } echo "
---Name------Type------Begin------End------HTML------PDF------TOTAL---
"; echo $items->children("ns2",true)->ItemName; echo " "; echo $items->children("ns2",true)->ItemDataType; echo " "; echo $items->children("ns2",true)->ItemPerformance->children("ns2",true)->Period->children("ns2",true)->Begin; echo " "; echo $items->children("ns2",true)->ItemPerformance->children("ns2",true)->Period->children("ns2",true)->End; echo " "; $Type=$items->children("ns2",true)->ItemPerformance->children("ns2",true)->Instance; foreach ($Type as $type) { echo $type->children("ns2",true)->Count; echo " "; } echo "
", "
"; echo "Total Number of Items: ", $x, "
"; ?>

[/php]

Hi,

In your last foreach loop, i noticed there is a $x++; statement. Did you use it for the purpose as a counter as you would for a normal for loop? If you are, it is not necessary. Its because foreach is intelligent enough to loop all the elements inside $Items variable.

To answer your question,

before the foreach loop, initialise a variable $total_count = 0;

I’m not sure which column you are trying to add here. But lets say the column value is $items->abc
Inside the foreach loop, what you can do is this:

$total_count += $items->abc;

After the foreach loop, you’ll get all the total from $items->abc totalled up in $total_count;

I hope you understand what i’m saying here. Good luck!

Regards,
developer.dex2908

Sponsor our Newsletter | Privacy Policy | Terms of Service