Jump to content

Adding two columns of different rows


Recommended Posts

please help me out...

 

How to get this (img.jpg) type of result.

 

I have 4 columns namely, ID, NEW, AMOUNT, TOTAL.

please see my image attachment to understand easily.

 

for first row, TOTAL= AMOUNT,

for 2nd row, TOTAL=  AMOUNT(ROW1) + AMOUNT(ROW2) OR AMOUNT(ROW2) + TOTAL(ROW1),

for 3rd row, TOTAL=  AMOUNT(ROW1) + AMOUNT(ROW2)+AMOUNT(ROW3) OR AMOUNT(ROW3) + TOTAL(ROW2).

and so on...

means loop will be upto the end of table.

 

I hope u understood my doubt.

 

please help me out...

 

THANK YOU.

post-164592-0-01206100-1375787587_thumb.jpg

Link to comment
https://forums.phpfreaks.com/topic/280886-adding-two-columns-of-different-rows/
Share on other sites

As you loop through the result set, add the amount for the current row to a total variable and save that variable to the row. eg:

$total = 0;
$allRows = array();
while ($row=$result->fetch()){
   $total += $row['amount'];
   $row['total'] = $total;

   $allRows[] = $row;
}
Then when you output the data in your table you'll have the total field in each row to output.

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.